想用JavaBean讀取數據庫的數據,顯示在JSP頁面上,JSP的代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.mmg.bean.classes.QuoteSelectBean"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="selectQuoteBean" class="com.mmg.bean.classes.QuoteSelectBean" scope="request">
</jsp:useBean><%String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<style type="text/css">
<!--
@import url("../style/cal/rightcal_style01.css");
-->
</style><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>報價單瀏覽</title>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
</head>
<body>
<div align="center">
<tr>
<td height="280" valign="top" bgcolor="#FFFFFF" class="cal_content02">
<FORM name="form1" action="" Method="post">
<table>
          <tr>
            <td height="85"> 
<%StringBuffer result = selectQuoteBean.selectQuote();%>
    <%=result%></td>
          </tr> </table>
</FORM>
</td>
<td width="12" align="center">
&nbsp;
</td>
</tr>
</div></body>
</html>
JavaBean的代碼如下:
package com.mmg.bean.classes;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mmg.bean.database.DBConnect;public class QuoteSelectBean {
String num1=null;

public StringBuffer selectQuote() {
        
String condition;
//condition = "SELECT Quote_UID ,MD_Company ,MD_QuoteD_No ,MD_CustNO ,MD_Currency ,MD_width ,MD_length ,MD_Type ,MD_Series ,MD_Model ,MD_Style ,MD_Return ,MD_Pocket ,MD_Qty ,MD_CustPO ,MD_ModelNO ,MD_Re ,MD_SaleNO ,MD_Weight ,MD_Suttle ,MD_Discount ,MD_UP ,MD_TotAmt ,MD_Netamt ,MD_Produced ,MD_Deleted ,MD_ContractNO ,MD_Count ,Created_Date ,Created_Time ,Modify_User ,Modify_Date FROM dbo.Mold_QMaster ORDER BY dbo.Mold_QMaster.Quote_UID  DESC"; 

if(num1!=null){
condition = "SELECT MD_Deleted ,Created_Date ,MD_QuoteD_No ,MD_CustNO ,MD_Type ,MD_width ,MD_length ,MD_Series ,MD_Return ,MD_ModelNO ,MD_Produced FROM dbo.Mold_QMaster ORDER BY dbo.Mold_QMaster.Quote_UID  DESC";
}else{        
condition = "SELECT MD_Deleted ,Created_Date ,MD_QuoteD_No ,MD_CustNO ,MD_Type ,MD_width ,MD_length ,MD_Series ,MD_Return ,MD_ModelNO ,MD_Produced FROM dbo.Mold_QMaster ORDER BY dbo.Mold_QMaster.Quote_UID  DESC";
} ResultSet rs = null;
Statement sql = null;
String deleted = "",produced = "";
String date = "", no = "",custno = "",type = "",modelno = "",w = "";
StringBuffer buffer = new StringBuffer();
try {
Connection conn = DBConnect.getConnection();
System.out.println(conn.isClosed());
sql = conn.createStatement();
rs = sql.executeQuery(condition);
int i = 1;
buffer.append("<table width=" + "100%" + " border=" + "0"
+ " cellpadding=" + "0" + " cellspacing=" + "1" + ">");
buffer.append("<tr align=" + "center" + ">");
buffer.append("<td width=" + "80" + "height=" + "25"
+ " class=" + "cal_td12" + ">狀態</td>");
buffer.append("<td width=" + "150" + "height=" + "25"
+ " class=" + "cal_td12" + ">報價日期</td>");
buffer.append("<td width=" + "180" + "height=" + "25"
+ " class=" + "cal_td12" + ">報價單編號</td>");
buffer.append("<td width=" + "120" + "height=" + "25"
+ " class=" + "cal_td12" + ">客戶編號</td>");
buffer.append("<td width=" + "180" + "height=" + "25"
+ " class=" + "cal_td12" + ">客戶名稱</td>");
buffer.append("<td width=" + "150" + "height=" + "25"
+ " class=" + "cal_td12" + ">模胚編號</td>");
buffer.append("<td width=" + "180" + "height=" + "25"
+ " class=" + "cal_td12" + ">客戶模號</td>");
buffer.append("<td width=" + "80" + "height=" + "25"
+ " class=" + "cal_td12" + ">轉單</td>");
buffer.append("</TR>");
while (rs.next()) {
buffer.append("<tr align=" + "center" + ">");
if (i % 2 == 1) {
deleted = rs.getString(1);
buffer.append("<TD width=" + 80 + " height=" + "20"
+ " class=" + "cal_td06" + ">" + deleted
+ "</TD>");
date = rs.getString(2);
buffer.append("<TD width=" + 150 + " height=" + "20"
+ " class=" + "cal_td06" + ">" + date
+ "</TD>");
no = rs.getString(3);
buffer.append("<TD width=" + 180 + " height=" + "20"
+ " class=" + "cal_td06" + ">" + no
+ "</TD>");
custno = rs.getString(4);
buffer.append("<TD width=" + 120 + " height=" + "20"
+ " class=" + "cal_td06" + ">" + custno
+ "</TD>");
type = rs.getString(5);
buffer.append("<TD width=" + 180 + " height=" + "20"
+ " class=" + "cal_td06" + ">" + type
+ "</TD>");
//w = rs.getString("MD_width");
buffer.append("<TD width=" + 150 + " height=" + "20"
+ " class=" + "cal_td06" + ">" + rs.getString("MD_width")
+ "</TD>");
} else {
deleted = rs.getString(1);
buffer.append("<TD width=" + 80 + " height=" + "20"
+ " class=" + "cal_td08" + ">" + deleted
+ "</TD>");
date = rs.getString(2);
buffer.append("<TD width=" + 150 + " height=" + "20"
+ " class=" + "cal_td08" + ">" + date
+ "</TD>");
no = rs.getString(3);
buffer.append("<TD width=" + 180 + " height=" + "20"
+ " class=" + "cal_td08" + ">" + no
+ "</TD>");
custno = rs.getString(4);
buffer.append("<TD width=" + 120 + " height=" + "20"
+ " class=" + "cal_td08" + ">" + custno
+ "</TD>");
type = rs.getString(5);
buffer.append("<TD width=" + 180 + " height=" + "20"
+ " class=" + "cal_td08" + ">" + type
+ "</TD>");
}
buffer.append("</TR>");
i++;
}
buffer.append("</Table>");
rs.close();
sql.close();
conn.close();
} catch (SQLException ex) {
System.out.println("TradeDB SQLException: " + ex.getMessage());
return new StringBuffer("查詢錯誤");
}
return buffer;

}
}錯誤提示
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:453)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause javax.servlet.ServletException
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.quote.QList_jsp._jspService(QList_jsp.java:113)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.OutOfMemoryError
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.

解决方案 »

  1.   

    順便請問一下,如果想用JavaBean讀取數據庫的數據,顯示在JSP頁面上,
    還有什麼更好的方法,謝謝!!!
      

  2.   

    显示你可以用struts的标签库,各人认为比较好用
      

  3.   

    root   cause  
    java.lang.OutOfMemoryError
    note   The   full   stack   trace   of   the   root   cause   is   available   in   the   Apache   Tomcat/5.5.17   logs. 
    报的错是内存不足,应该是你的查询结果里面的数据很多。验证的办法是构造一个查询结果会很小的查询语句。
    解决的办法是调大Java虚拟机的内存。
      

  4.   

    一种可能是数据量太大,解决方法一次少查一点.
    还一种可能数据库连接没释放.怎么确定是这种问题?如果你每次重新开机或者重启tomcat后头几次查询正常,后面不正常就是这种情况.就你的代码看是后一种情况,解决办法,把你所有close()语句写到finally里面,不要怕麻烦每个close()套一层try catch
      

  5.   

    謝謝各位的回答,的確是因為數據量太多造成的。順便請問一下,怎樣把日期型轉換成字符串:
    String strday;
    strday = QuoteSelectBean.DateToString(date);
    為什麼總是語法錯誤,在DateToString下面總有一個紅色的波浪。
      

  6.   

    你要是不便其他各式的话,直接toString就好了
      

  7.   

    主要是把日期型的轉換成string型~~~~~~
      

  8.   

    你的QuoteSelectBean里没有DateToString这个方法吧