首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

找不到statement流

2011-12-25 
找不到statement源?我遇到这样的一个问题,我在一个用户输入表单,输入数据,然后调用一个类的addUser方法将

找不到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.err.println( "Error: "+e.getMessage());
}finally
{
sm.close();
con.close();
}
return   rs;
}

连接类:

public   class   DbConn   {
private   static   String   dbDriver= "com.mysql.jdbc.Driver ";


private   static   String   dbConnUrl= "jdbc:mysql://localhost:3309/news?user=root&password=jimluck ";
private   static   Connection   conn   =   null;
        private   Statement   stmt=null;
        ResultSet   rs=null;
        private   static   int   sign=1;
       
        public   DbConn()
        {
          }
       
        public   static   Connection   getConn()   throws   SQLException  
        {
        if   (conn   !=   null   &&   !conn.isClosed())//   不为空且没关闭
return   conn;
       
        try{
              Class.forName(dbDriver);
              conn=   DriverManager.getConnection(dbConnUrl);
              }
              catch(java.lang.ClassNotFoundException   e){
              System.err.println( "Class   Error: "+e.getMessage());
              }
        return   conn;
        }
        ...
}

[解决办法]
//********定义方法:获取所有用户信息*******
public ResultSet getAllUser() throws SQLException
{
Connection con=null;
PreparedStatement sm=null;
ResultSet rs=null;
try{
//获取连接
con=DbConn.getConn();
String sql= "select * from Admin ";

sm=con.PreparedStatement (sql);
sm.executeQuery();
}catch(SQLException e){
System.err.println( "Error: "+e.getMessage());
}finally
{
sm.close();
con.close();
}
return rs;
}
最好把你的程序中的statement都统一为PreparedStatement
[解决办法]
public ResultSet getAllUser() throws SQLException
{
Connection con=null;
Statement sm=null;
ResultSet rs=null;
try{
//获取连接
con=DbConn.getConn();
String sql= "select * from Admin ";

sm=con.createStatement();
rs=sm.executeQuery(sql); //少了对rs的赋值
}catch(SQLException e){
System.err.println( "Error: "+e.getMessage());
}finally
{
sm.close();
con.close();
}
return rs;
}

热点排行