packagesql;importjava.sql.*;publicclassDBConnection{privatefinalStringDriverName="com.mysql.jdbc.Driver";privatefinalStringURL="jdbc:mysql://localhost:3306/csms";privatef...
package sql;
import java.sql.*;
public class DBConnection {
private final String DriverName="com.mysql.jdbc.Driver";
private final String URL="jdbc:mysql://localhost:3306/csms";
private final String name="root";
private final String pwd="111111";
public Connection conn;
public Connection getConn() throws ClassNotFoundException, SQLException{
Class.forName(DriverName);
conn=DriverManager.getConnection(URL, name, pwd);
return conn;
}
public void close() throws SQLException{
conn.close();
}
static public void main(String []args) throws SQLException, ClassNotFoundException{
StringBuffer strSQL=new StringBuffer();
strSQL.append("select * from Users ");
strSQL.append("where Login_ID='2' and passwd='222'");
DBConnection db=new DBConnection();
Connection conn=db.getConn();
PreparedStatement st=conn.prepareStatement(strSQL.toString());
ResultSet rs=st.executeQuery();
System.out.println(rs.getString("Name"));
}
}
异常
Exception in thread "main" java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5650)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5610)
at sql.DBConnection.main(DBConnection.java:28)
求救啊,想死了 不知道为什么报错 rs 不为空
找了好久了
。
展开
本文档解析了一个Java程序在尝试从MySQL数据库获取数据时遇到的异常,重点在于解决`SQLException: Before start of result set`的问题,展示了如何检查并修复ResultSet未正确初始化的问题。
3048

被折叠的 条评论
为什么被折叠?



