码迷,mamicode.com
首页 > 数据库 > 详细

java中mysql查询报错java.sql.SQLException: Before start of result set

时间:2019-04-11 10:34:52      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:pre   调用   解决方法   数据   sql   cep   and   desc   .exe   

异常:java.sql.SQLException: Before start of result set

解决方法:使用rs.getString();前一定要加上rs.next();

 

                sm = conn.createStatement();
                ResultSet rs = null;
                String sql_max = "SELECT id FROM a WHERE name= ‘" + name
                        + "‘ ORDER BY online_time DESC LIMIT 1";
                rs = sm.executeQuery(sql_max);
                if (rs.next()) {
                    String id = rs.getString("id");
                    int id1 = Integer.parseInt(id);
                    String sql_update = "UPDATE  b SET offline_time = NOW()  WHERE  id=" + id1
                            + " and name= ‘" + name+ "‘";
                    sm.executeUpdate(sql_update);
                }

 

原因:ResultSet对象代表SQL语句执行的结果集,维护指向其当前数据行的光标。每调用一次next()方法,光标向下移动一行。最初它位于第一行之前,因此第一次调用next()应把光标置于第一行上,使它成为当前行。随着每次调用next()将导致光标向下移动一行。在ResultSe对象及其t父辈Statement对象关闭之前,光标一直保持有效。

参考:https://blog.csdn.net/killua_hzl/article/details/6073618 

java中mysql查询报错java.sql.SQLException: Before start of result set

标签:pre   调用   解决方法   数据   sql   cep   and   desc   .exe   

原文地址:https://www.cnblogs.com/webttt/p/10687997.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!