连接数据库的小问题
Java code
import java.sql.*;
public class JDBC
{
public static void main (String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/Information";
Connection conn = DriverManager.getConnection(url,"root","123456");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from information where name='zhangsan'");
String str = rs.getString("name");//把这句注释就没错啦 应该这句是错的
System.out.println(str);
rs.close();
stat.close();
conn.close();
}
}
Exception in thread "main"
java.sql.SQLException: Before start of result set
at
com.mysql.jdbc.SQLError.create
SQLException(SQLError.java:910)
at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:720)
at com.mysql.jdbc.ResultSet.getStringInternal(ResultSet.java:5624)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:5544)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:5584)
at JDBC.main(JDBC.java:10)
我有个information表里面有3个字段 有name,tel,email
有zhangsan 000000 zhangsan@126.com
请高人指点一下
------解决方案--------------------rs.next();
String str = rs.getString("name");//把这句注释就没错啦 应该这句是错的
------解决方案--------------------楼主漏掉了一句,rs.next(),用来读取结果集的下一条数据,你都没有读,怎么取呢,提示已经给了:
Exception in thread "main" java.sql.SQLException: Before start of result set