[英]sql query execute with PreparedStatement
I have the follow code in a servlet - 我在servlet中有以下代码 -
String loginID = request.getParameter("loginId").toString();
String loginPassword = request.getParameter("loginPassword").toString();
String strSQLcount = "SELECT COUNT(*) as 'Number Of Match' "
+ "FROM persons " + "WHERE (password =? AND id =?);";
PreparedStatement prepareSQL = connection
.prepareStatement(strSQLcount);
prepareSQL.setString(1, loginPassword);
prepareSQL.setString(2, loginID);
ResultSet numOfMatchResult = prepareSQL.executeQuery();
// now we know number of matchs ...
int numOfMatch = numOfMatchResult.getInt(1);
When on running and reach to the line int numOfMatch = numOfMatchResult.getInt(1);
当运行并到达行时int numOfMatch = numOfMatchResult.getInt(1);
it throws the exception - java.sql.SQLException
. 它抛出异常 - java.sql.SQLException
。 I checked it and seen that it because the executeQuery()
retrieved no one . 我检查了它并看到它,因为executeQuery()
没有检索到它。 it occur although I have in persons
table ,created with MySQL, the 2 fields - id (text)
with value "300" and password (text)
with value "500" . 它会发生,虽然我在persons
表,使用MySQL,2场创建- id (text)
与值“300”和password (text)
与值“500”。 and of course I check it when loginID
and loginPassword
with same 2 values . 当然,当loginID
和loginPassword
具有相同的2值时,我会检查它。 I checked all the other things about the connection to the DB and it was OK .. so I think the problem is in the SQL syntax in strSQLcount
. 我检查了与DB的连接的所有其他事情,它没关系..所以我认为问题出在strSQLcount
的SQL语法中。
You forgot to call next()
on the result set: 你忘了在结果集上调用next()
:
ResultSet numOfMatchResult = prepareSQL.executeQuery();
int numOfMatch = 0;
if (rs.next() {
numOfMatch = numOfMatchResult.getInt(1);
}
If that is not sufficient to solve the problem, paste the whole stack trace of the exception: it contains meaningful information. 如果这不足以解决问题,请粘贴异常的整个堆栈跟踪:它包含有意义的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.