java.sql.SQLException: Before start of result set
在使用JDBC查询数据库报了这么一个错误
CREATE TABLE `d_user` (
`id` int(10) NOT NULL,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
insert into d_user values(1,'sean');
public class Test {
public static void main(String[] args){
Connection conn = null;
Statement stat = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb";
String user = "root";
String pwd = "196428"; conn = DriverManager.getConnection(url, user, pwd);
stat = conn.createStatement();
String sql = "select name from d_user where id = 1";
ResultSet rs = stat.executeQuery(sql);
// if(rs.next()){
String name = rs.getString(1);
System.out.println(name);
// }
}catch(Exception e){
e.printStackTrace();
}finally{
if(null != conn){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(null != stat){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
运行结果为:
java.sql.SQLException: Before start of result set
具体的报错信息和使用的数据库驱动有关系,当我把数据库驱动更换为mysql-connector-java-5.1.6-bin.jar后(原先使用的驱动为mysql-connector-java-5.1.10.jar),报错为:
java.sql.SQLException
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
去掉测试代码中的注释部分后运行正常:
sean
在对结果集ResultSet进行操作之前,一定要先用ResultSet.next()将指针移动至结果集的第一行
看看API对next()方法的描述:
......
将光标从当前位置向前移一行。ResultSet 光标最初位于第一行之前;第一次调用 next 方法使第一行成为当前行;第二次调用使第二行成为当前行,依此类推。
当调用 next 方法返回 false 时,光标位于最后一行的后面。
......
JDBC写起来让人眼花
java.sql.SQLException: Before start of result set的更多相关文章
- java.sql.SQLException: Before start of result set解决方法
java.sql.SQLException: Before start of result set解决方法 今天做东西的时候发现这个错误,查了查,特地记下来,以后开始积累了 哈哈 解决发法是: 使用r ...
- 【java.sql.SQLException: Before start of result set】
将ResultSet转换为 Map<String,String>时抛出了一个这样的异常:java.sql.SQLException: Before start of result set ...
- java中mysql查询报错java.sql.SQLException: Before start of result set
异常:java.sql.SQLException: Before start of result set 解决方法:使用rs.getString();前一定要加上rs.next(); sm = con ...
- 解决 java.sql.SQLException: Before start of result set
java中使用如下代码做数据库连接,用以查询数据 *******************我是分割线************************************* try { Class.f ...
- 解决java.sql.SQLException: ORA-01789: query block has incorrect number of result columns
java.sql.SQLException: ORA-01789: query block has incorrect number of result columns at oracle.jdbc. ...
- Error getting nested result map values for 'company'. Cause: java.sql.SQLException: Invalid value for getInt() - 'NFHK188'
我今天遇到一个我不解的问题,是mybatis多对一关系查询出问题了,但是我自己还是解决了,在网上也查过那个错误,可是找不到我想要的.不知道你们遇到过没有,我接下来分享给大家.希望我这个第一篇博客能帮助 ...
- org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup' from result set. Cause: java.sql.SQLException: Error
异常展示: org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'alarmGroup ...
- java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@27ce24aa is still active. No statements may be issued when any streaming result sets are open and in use on a given connection
在Sqoop往mysql导出数据的时候报了这个错误,一开始还以为是jar包没有打进去或者打错位置了,未解便上网查询. Error reading from database: java.sql.SQL ...
- Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: java.sql.SQLException: 不支持的特性
mybatis插入数据时报错: Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or ...
随机推荐
- 解决因block的损坏而导致hdfs启动后进入安全模式
问题描述: 以单机伪分布式方式安装了hadoop2.7.1,并在该机器上安装了hive1.2.1.首先执行 sbin/start-dfs.sh 启动hdfs服务,然后执行hive,这时hive脚本启动 ...
- 轻松搭建自己的Linux发行版本
许多人想要搭建自己的Linux发行版本,可能是觉得有趣,也可能是为了学习更多的Linux知识,或者因为他们有很正式的问题要解决.但是秘密是:自己搭建完美的发行版本不是很困难的一件事.事实上,我们收集了 ...
- cannot find the word template:WordToRqm.dot的解决方法
powerdesigner安装后: C:"Program Files"Sybase"PowerDesigner 12"Add-ins"Microsof ...
- ASP.NET MVC 開發心得分享 (21):Routing 觀念與技巧
ASP.NET MVC 預設在 Global.asax 所定義的 RegisterRoutes 方法中可以輕易的定義你希望擁有的網址格式,嚴格上來講這並非 ASP.NET MVC 的專利,而是從 AS ...
- How to change the property of a control from a flowlayoutpanel?
Well, the easiest way would be to retain an explicit reference to the buttons you're adding. Otherwi ...
- MailSystem.NET Gmail IMAP讀取信件
程式的主流程為: 開啟SSL連線,逐一讀取收信匣中的信件,將信件內文HTML及附檔逐一存檔後,再將信件移至垃圾桶. 程式碼如下,補充說明我寫在註解裡,請參考: static void Main(str ...
- python导入模块时的路径疑惑
有一个事儿,以前没注意,今天发现了,记录一下. 假设一个python文件a.py中,有一段代码,是打印当前路径的.当单独执行a.py文件的时候,打印的是a.py的位置. 但是当a.py文件被其他pyt ...
- leetcode 逆转字符串 当年的第一题,今天再写一遍,物是人非
public class Solution { public String reverseWords(String s) { if(s==null||s.length()==0) return &qu ...
- HDU4607 - Park Visit(树的直径)
题目大意 给定一颗树,要求走过其中连续的k个点,使得步数最少 题解 每条边要么经过两次,要么一次,因为我们的目标就是使得走一次的边尽量的多,这样就转换成求树的直径了,求树的直径我用的是两次dfs,先随 ...
- httpclient在抓取网页时出现速度慢的情况分析
问题: 最近在使用httpclient3.0 来做项目,在一台机器部署系统后,发现此机器比其它机器在抓取页面的速度上慢了大概4s,左右. 项目是部署在局域内网,所以代码中都是直接写的IP地址 .在使用 ...