日期:2014-05-18  浏览次数:20868 次

找不到statement源?
我遇到这样的一个问题,我在一个用户输入表单,输入数据,然后调用一个类的addUser方法将数据存进去(事先获取了一个conn()),接着我在后面又用一个getAllUser方法来获取所有用户信息,就出错了,我利用单步跟踪发现是statement问题,提示它的各种源都找不到,我试着改了很多地方都不行:

下面是程序:

<%@   page   contentType= "text/html;   charset=gb2312 "   language= "java "   import= "java.sql.* "%>
<%@   page   import= "cn.db.Users "%>
<jsp:useBean   id= "udb "   scope= "request "   class= "cn.operate.UserOperate "/>
<%
ResultSet   userRs=null;
Users   admin=new   Users();
String   name=request.getParameter( "username ");
String   pwd=request.getParameter( "userpwd ");
String   usertype=request.getParameter( "utype ");
String   email=request.getParameter( "useremail ");
String   qq=request.getParameter( "userqq ");
String   msn=request.getParameter( "usermsn ");
admin.setUserName(name);
admin.setUserPwd(pwd);
admin.setUserLevel(usertype);
admin.setUserEmail(email);
admin.setUserQQ(qq);
admin.setUserMsn(msn);
admin.setLastLoginIp( "192.168.0.1 ");
out.println( "用户名: "+name+ " <br> ");
out.println( "密     码: "+pwd+ " <br> ");
out.println( "用户类型: "+usertype+ " <br> ");
out.println( "邮件: "+email+ " <br> ");
out.println( "QQ: "+qq+ " <br> ");
out.println( "MSN: "+msn+ " <br> ");

if(udb.addUser(admin))
out.println( " <br> 恭喜您,记录插入成功! ");
%>
<br> <br>
<%
userRs=udb.getAllUser();
while(userRs.next())
{
out.println( "aaa ");
}
%>

在userRs=udb.getAllUser();这出错了.

//********定义方法:添加用户*******
public   boolean   addUser(Users   admin)   throws   SQLException
{
Connection   con=null;
PreparedStatement   sm=null;
try{
//获取连接
con=DbConn.getConn();
String   sql= "insert   into   Admin(UserName,UserPwd,UserLevel,UserEmail,UserQQ,UserMsn,LastLoginIp)   ";
sql=sql+ "values(?,?,?,?,?,?,?) ";
sm=con.prepareStatement(sql);
//sm.setInt(1,   admin.getId());
sm.setString(1,admin.getUserName());
sm.setString(2,admin.getUserPwd());
sm.setString(3,admin.getUserLevel());
sm.setString(4,admin.getUserEmail());
sm.setString(5,admin.getUserQQ());
sm.setString(6,admin.getUserMsn());
sm.setString(7,admin.getLastLoginIp());
sm.execute();
}catch(SQLException   e){
System.err.println( "Error: "+e.getMessage());
}finally{
sm.close();
con.close();       //关闭数据库连接
}
return   true;
}

//********定义方法:获取所有用户信息*******
public   ResultSet   getAllUser()   throws   SQLException
{
Connection   con=null;
Statement   sm=null;
ResultSet   rs=null;
try{
//获取连接
con=DbConn.getConn();
sm=con.createStatement();
String   sql= "select   *   from   Admin ";
sm.executeQuery(sql);
}catch(SQLException   e){
System.