在使用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的更多相关文章

  1. java.sql.SQLException: Before start of result set解决方法

    java.sql.SQLException: Before start of result set解决方法 今天做东西的时候发现这个错误,查了查,特地记下来,以后开始积累了 哈哈 解决发法是: 使用r ...

  2. 【java.sql.SQLException: Before start of result set】

    将ResultSet转换为 Map<String,String>时抛出了一个这样的异常:java.sql.SQLException: Before start of result set ...

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

    异常:java.sql.SQLException: Before start of result set 解决方法:使用rs.getString();前一定要加上rs.next(); sm = con ...

  4. 解决 java.sql.SQLException: Before start of result set

    java中使用如下代码做数据库连接,用以查询数据 *******************我是分割线************************************* try { Class.f ...

  5. 解决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. ...

  6. Error getting nested result map values for 'company'. Cause: java.sql.SQLException: Invalid value for getInt() - 'NFHK188'

    我今天遇到一个我不解的问题,是mybatis多对一关系查询出问题了,但是我自己还是解决了,在网上也查过那个错误,可是找不到我想要的.不知道你们遇到过没有,我接下来分享给大家.希望我这个第一篇博客能帮助 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 基于LAMP平台的网站架构(或Web系统架构)

    1.网站架构的前提(或者说需求) 我们公司是一电子商务的网站,因为线下家具建材项目的推广需求,从而有了我们公司的这个线上网站,在这里我贴一张公司的网站架构图. 总体来说网站规模不是太大,注册人数在15 ...

  2. Deep Learning Overview

    [Ref: http://en.wikipedia.org/wiki/Deep_learning] Definition: a branch of machine learning based on ...

  3. ORACLE RAC NTP 时间服务器配置

    Linux 时间同步配置 . 一. 使用ntpdate 命令 1.1 服务器可链接外网时 # crontab -e 加入一行: */1 * * * * ntpdate 210.72.145.44 21 ...

  4. Add controls dynamically in flowlayoutpanel

    For a FlowLayoutPanel, you don't need to specify a location since the controls are arranged for you. ...

  5. apache 实用配置

    1.反向代理 反向代理是指想访问目标机器,但无法直接访问,此时,可以通过与目标机器相同网络段的机器做桥接,通过访问桥接机器,访问目标机器,称为反向代理. vi httpd.conf 将代理配置开放: ...

  6. 四元数(Quaternion)详细讲解以及在图形图像编程中的使用

    关于四元数介绍可以直接看wiki,写的很详细了. 四元数的基本运算:http://www.linuxgraphics.cn/opengl/opengl_quaternion.html,代码有些问题. ...

  7. [CODEVS1048]石子归并

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子 ...

  8. 【原】Comparator和Comparable的联系与区别

    1.知识点了解 Comparator和Comparable都是用用来实现集合中元素的比较.排序的,所以,经常在集合外定义Comparator接口的方法和集合内实现Comparable接口的方法中实现排 ...

  9. 计蒜客 取数游戏(dp)

    有如下一个双人游戏:N个正整数的序列放在一个游戏平台上,两人轮流从序列的两端取数,每次有数字被一个玩家取走后,这个数字被从序列中去掉并累加到取走该数的玩家的得分中,当数取尽时,游戏结束.以最终得分多者 ...

  10. SignalR Supported Platforms -摘自网络

    SignalR is supported under a variety of server and client configurations. In addition, each transpor ...