【发布时间】:2016-08-13 00:00:58
【问题描述】:
我在让我的程序从数据库中读取信息时遇到问题。我希望它基本上打印出一个包含提取信息的表格(我遵循的教程正是这样做的)。不幸的是,在翻阅我的代码和教程之后,我发现没有任何区别。最终我希望能够从数据库中提取信息并允许用户编辑这些信息。
这是我的代码:
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 将在结果开始后发生设置而不是之前。
【问题讨论】:
标签: java mysql database jdbc resultset