【发布时间】:2012-05-16 13:54:13
【问题描述】:
以下 sn-p 从数据库中获取照片的名称:
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/photog");
Connection connection = ds.getConnection();
String sqlQuery = "select nameofthephoto from photocaptions where useremail='" + email + "'";
PreparedStatement statement = connection.prepareStatement(sqlQuery);
set = statement.executeQuery();
if(set == null) {
System.out.println("set is null !");
} else if(set != null) {
System.out.println("set is not null !");
System.out.println(set.next());
System.out.println(set.getString("nameofthephoto")); // last statement
}
else-if 块执行
代码最后2条语句的结果是:
false
最后一条语句抛出异常
java.sql.SQLException: 当前光标位置的操作无效。
表中只有一个“数据行”与查询匹配,while.next() 显示的错误结果也可以看到该“数据行”。那为什么上面sn-p中的最后一条语句(stacktrace这么说)会抛出异常呢?_
【问题讨论】:
-
不设置任何参数时 pstmt 有什么用
-
@skaffman 你为什么删除你的答案?