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("%-10st", 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("%-10st", 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将发生在结果集开始之后,而不是之前。

你的代码没有意义。除非next()返回true,否则不能调用ResultSet.getObject(),并且不需要在循环中调用ResultSet.next()来打印列名。它们不会每行改变。

还假定您希望在数据之前打印列名。

打印列名的循环应该在打印getObject()值的循环所在的位置,反之亦然。

相关内容

  • 没有找到相关文章

最新更新