0
我遇到了讓我的程序從數據庫中讀取信息的問題。我希望它基本上打印出提取信息的表格(我遵循的教程就是這樣做的)。不幸的是,在翻閱我的代碼和教程後,我發現沒有什麼區別。最終我希望能夠從數據庫中獲取信息,並允許用戶編輯這些信息。java.sql.SQLException:結果集開始之前/之後
這裏是我的代碼:
import java.sql.*;
public class Main {
//Driver and connection URL.
private String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static String connectionString = "jdbc:mysql://localhost/employees";
//DB Credentials.
private static String password = "password";
private static String username = "root";
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//Register JDBC Driver.
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionString, username, password);
System.out.println("Connected to database.");
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
resultSet = statement.executeQuery("SELECT * from employees");
//process query results.
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.println("Emloyees table of Employees DB");
for (int i=1; i<=numberOfColumns; i++){
System.out.printf("%-10s\t", resultSet.getObject(i)); //Return the value of a given object as a java object.
}
System.out.println();
//Initial cursor is before first row.
while(resultSet.next()){ //move cursor forward one row.
for (int i=1; i<=numberOfColumns; i++){
System.out.printf("%-10s\t", metaData.getColumnName(i));
}
}
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try{
connection.close();
statement.close();
resultSet.close();
}
catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Great! Everything works!");
}
}
這裏是我的什麼,當我運行的程序發生了:
Fri Aug 12 19:49:55 EDT 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Connected to database.
Emloyees table of Employees DB
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790)
at com.mysql.jdbc.UpdatableResultSet.checkRowPos(UpdatableResultSet.java:214)
at com.mysql.jdbc.ResultSetImpl.getObject(ResultSetImpl.java:4432)
at Main.main(Main.java:38)
Great! Everything works!
值得注意的是:它是連接到數據庫,如果我換我的for循環使用我的while循環,它會多次打印每列的名稱,java.sql.SQLException將在結果集的開始之後而不是之前發生。