求救,必给分!!!!

westwood 2001-07-31 10:34:25
数据库中有3条数据,为什么只能得到1条,count出来也是1,为什么?

<%@page contentType="text/html;charset=GB2312"%>
<jsp:useBean id="mybeen" class="db.DBCon" scope="page" />

<%
java.sql.ResultSet rs1 ;
mybeen.prepare("select count(*) from TT_PROVINCE_SCHEDULE");
rs1 = mybeen.Query();
while(rs1.next()) {
out.println (rs1.getInt(1)+"<br>");
}
rs1.close();

java.sql.ResultSet rs2 ;
mybeen.prepare("select operation_name from TB_OPERATION_DEFINE");
rs2 = mybeen.Query();
while(rs2.next()) {
%>
<option value="<%=rs2.getString("operation_name")%>"><%=rs2.getString("operation_name")%></option>

<%
}
rs2.close();
%>
<%mybeen.close();%>
...全文
70 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
24680 2001-08-08
把db下的BEAN贴出来啊。
回复
backlove 2001-08-05
??
回复
robinhoodx 2001-08-05
each result set need a seperate Statement at a time!!!i.e. one rs per statement!otherwise, each result set will close previous result set.
for detail theory, see JDK doc, Statement() item.

I am new to jsp and met this problem yesterday too.
In your dbbean, set a array for statement like

Statement stmt[]=new Statement[50];
then for each int j, increment j++ every time when do a query.

ok! to be clear, give u my source code. u can reuse it. only problem is that
it cannot completely disconnect with database. If anyone find solution, tell me please.


package application;
import java.util.*;
import java.sql.*;

public class StmtBean{

String sDBDriver = "org.gjt.mm.mysql.Driver";
String sConnStr = "jdbc:mysql://localhost/questionnaire?user=&password=";
Connection conn = null;
// Statement stmt[]={null,null,null,null,null,null,null,null};
Statement stmt[]=new Statement[50];
ResultSet rs = null;
int j=0;
int count=0;
//private boolean isDouble=false;

public void connect(){
try {
Class.forName(sDBDriver).newInstance();
conn = DriverManager.getConnection(sConnStr);
checkForWarning (conn.getWarnings());

System.out.println("connect to database :OK!");
}catch (SQLException ex) {
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null) {
System.out.println("SQLState:" +ex.getSQLState());
System.out.println(" Message :" +ex.getMessage());
System.out.println(" Vendor :" +ex.getErrorCode());
ex = ex.getNextException ();
}
}catch (java.lang.Exception ex) {
ex.printStackTrace ();
}
}


public int execUpdate(String sql) {
try {
j++;
stmt[j] = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
count=stmt[j].executeUpdate(sql);

if(count==0) {
System.out.println("update error!");
}
// stmt.close();
// rs.close();
}catch (SQLException ex) {
System.out.println("Update error");
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null) {
System.out.println("SQLState:" +ex.getSQLState());
System.out.println(" Message :" +ex.getMessage());
System.out.println(" Vendor :" +ex.getErrorCode());
ex = ex.getNextException ();
}
}catch (java.lang.Exception ex) {
ex.printStackTrace ();

}
return count;
}
public ResultSet execQuery(String sql) {
try {
j++;
stmt[j] = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs=stmt[j].executeQuery(sql);

}catch (SQLException ex) {
System.out.println ("\n*** SQLException caught2 ***\n");
while (ex != null) {
System.out.println("SQLState:" +ex.getSQLState());
System.out.println(" Message :" +ex.getMessage());
System.out.println(" Vendor :" +ex.getErrorCode());
ex = ex.getNextException ();
}
}catch (java.lang.Exception ex) {
ex.printStackTrace ();
}
return rs;
}

private boolean checkForWarning (SQLWarning warn) throws SQLException
{
// System.out.println("DBAccess checkForWarning");
boolean rc = false;

if (warn != null) {
System.out.println ("\n *** Warning ***\n");
rc = true;
while (warn != null) {
System.out.println("SQLState:" +warn.getSQLState());
System.out.println("Message :" +warn.getMessage());
System.out.println("Vendor :" +warn.getErrorCode());
warn = warn.getNextWarning ();
}
}
return rc;
}
public void disconnect() {
try {
System.out.println("DBmysql disconnect");
rs.close();
// for(int i=0;i<3;i++)
// rs[i].close();
for(;j>0;j--)
{ System.out.println("DBmysql disconnect");
stmt[j].close();
System.out.println("ok");}
conn.close();
}catch (SQLException ex) {
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null) {
System.out.println ("SQLState: " +ex.getSQLState());
System.out.println ("Message: " +ex.getMessage());
System.out.println ("Vendor: " +ex.getErrorCode());
ex = ex.getNextException ();
System.out.println ("");
}
}catch (java.lang.Exception ex) { ex.printStackTrace (); }
}


public StmtBean() {
System.out.println("create StatementBean ok");
connect();

}


}




回复
kookoo 2001-08-01
……
回复
kookoo 2001-07-31
你是不是在mybeen里面把数据库连接close了……
回复
lyradancing 2001-07-31
你的mybeen呢?
看一下
回复
ExitWindows 2001-07-31
up
回复
ExitWindows 2001-07-31
up
回复
ExitWindows 2001-07-31
up
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2001-07-31 10:34
社区公告
暂无公告