ResultSet结果集不为空但rs.next()为false,求解。
附代码:HttpSessionsession=request.getSession(true);Stringt_id=(String)session.getAttribu...
附代码:
HttpSession session = request.getSession(true); String t_id = (String) session.getAttribute("t_id");
List<Question> list = new ArrayList<Question>();
String condition = request.getParameter("condition");
String querySql;
if ("".equals(condition)) {
querySql = "select * from question1 where t_id=" + t_id;
} else {
condition = condition.trim();
querySql = "select * from question1 where ( question like '%"+ condition + "%' and t_id =" + t_id+")";
}
System.out.println(querySql);
Connection conn = MyDB.getConnection();
Statement stmt = MyDB.getStatement(conn);
ResultSet rs = MyDB.getResultSet(stmt,querySql);
if("".equals(rs)){
System.out.println( "ResultSet结果集为空!");
}else{
try {
while (rs.next()) {
Question q=new Question(); q.setId(rs.getInt("q_id"));
q.setQuestion(rs.getString("question"));
q.setChoice1(rs.getString("choice1"));
q.setChoice2(rs.getString("choice2"));
q.setChoice3(rs.getString("choice3"));
q.setChoice4(rs.getString("choice4"));
System.out.println(q.getId() + "--" + q.getQuestion() + "--"+ q.getChoice1() + "--" + q.getChoice2() + "--"+ q.getChoice3() + "--" + q.getChoice4() + "--");
list.add(q);
} request.setAttribute("modifyQuestion", list);
request.getRequestDispatcher("/modify.jsp").forward(request,
response);
} catch (SQLException e) {
e.printStackTrace();
}
在MySQL中该SQL语句执行正常 展开
HttpSession session = request.getSession(true); String t_id = (String) session.getAttribute("t_id");
List<Question> list = new ArrayList<Question>();
String condition = request.getParameter("condition");
String querySql;
if ("".equals(condition)) {
querySql = "select * from question1 where t_id=" + t_id;
} else {
condition = condition.trim();
querySql = "select * from question1 where ( question like '%"+ condition + "%' and t_id =" + t_id+")";
}
System.out.println(querySql);
Connection conn = MyDB.getConnection();
Statement stmt = MyDB.getStatement(conn);
ResultSet rs = MyDB.getResultSet(stmt,querySql);
if("".equals(rs)){
System.out.println( "ResultSet结果集为空!");
}else{
try {
while (rs.next()) {
Question q=new Question(); q.setId(rs.getInt("q_id"));
q.setQuestion(rs.getString("question"));
q.setChoice1(rs.getString("choice1"));
q.setChoice2(rs.getString("choice2"));
q.setChoice3(rs.getString("choice3"));
q.setChoice4(rs.getString("choice4"));
System.out.println(q.getId() + "--" + q.getQuestion() + "--"+ q.getChoice1() + "--" + q.getChoice2() + "--"+ q.getChoice3() + "--" + q.getChoice4() + "--");
list.add(q);
} request.setAttribute("modifyQuestion", list);
request.getRequestDispatcher("/modify.jsp").forward(request,
response);
} catch (SQLException e) {
e.printStackTrace();
}
在MySQL中该SQL语句执行正常 展开
3个回答
展开全部
可能是你的这个
ResultSet rs = MyDB.getResultSet(stmt,querySql);
取得ResultSet 时候出了问题,把你的MyDB类 贴出来看看
还有就是这句 if("".equals(rs)){ rs不是字符串 不能这样判空,要这样写if(rs==null){
ResultSet rs = MyDB.getResultSet(stmt,querySql);
取得ResultSet 时候出了问题,把你的MyDB类 贴出来看看
还有就是这句 if("".equals(rs)){ rs不是字符串 不能这样判空,要这样写if(rs==null){
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if("".equals(rs)){
System.out.println( "ResultSet结果集为空!");
}else{
这句话不能判断resultset里面是否有元素!rs.next()为false就是真的没有数据,你再仔细检查下你的程序,是不是数据库地址不对...
System.out.println( "ResultSet结果集为空!");
}else{
这句话不能判断resultset里面是否有元素!rs.next()为false就是真的没有数据,你再仔细检查下你的程序,是不是数据库地址不对...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的判断不对,就算rs没有数据也不会等于“”
while(rs.next()){
.....
}
while(rs.next()){
.....
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询