日期:2014-05-19  浏览次数:21287 次

java.sql.SQLException: No value specified for parameter 1
代码如下 public boolean login(UserInfo user)throws Exception{
//用户登录
boolean flag=false;
int userNo=Integer.parseInt(user.getUserNo());
String password=user.getPassword();
//测试是否从现在的user对象中取到了值
System.out.println(userNo+"我是USER对象的userNo");
System.out.println(password+"我是USER对象的password");
DatabaseConnection dbc=new DatabaseConnection();//取得数据库连接和关闭对象;
try{
String sql="SELECT userNo FROM userInfo where userNo=? and password=?";
this.conn=dbc.getConnection();
this.pst=conn.prepareStatement(sql);
//this.pst.setInt(1, userNo);//给sql中参数赋值
this.pst.setLong(1, userNo);
this.pst.setString(2, password);
ResultSet rs=pst.executeQuery();
if(rs.next()){//传入用户账号和对应的密码都存在时为真
flag=true;
System.out.println(rs.getString(userNo)+"我是从数据库里取出来的");
System.out.print(flag+"测试flag");//测试flag,看是否进入了if里面,true则进入了
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(this.pst!=null){
this.pst.close();
}
this.conn.close();
}
return flag;
}

报的错误信息如:
at java.lang.Thread.run(Unknown Source)
123我是从页面取出来的userNO
123我是USER对象的userNo
123我是USER对象的password
java.sql.SQLException: No value specified for parameter 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2214)

从上面可以看出userNO和password都是取得了值 的,可是为什么会设置不了SQL语句里面的参数呢?

------解决方案--------------------
探讨

引用:

是否表名与关键字之间添加空格了?


难道不用吗?不会吧,这个我一直都是这样用的呀…………