我遇到了让程序从数据库中读取信息的问题。我希望它基本上打印出一个包含所提取信息的表格(我所遵循的教程就是这样)。不幸的是,在倾注我的代码和教程后,我发现没有区别。最终,我希望能够从数据库中提取信息,并允许用户编辑此信息。
这是我的代码:
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!
值得注意的是:它正在连接到数据库,如果我用while循环交换for循环,它会多次打印每列的名称,java.sql.SQLException将在结果开始后发生而不是之前设定。
答案 0 :(得分:2)
你的代码毫无意义。除非ResultSet.getObject()
返回true,否则无法调用next()
,并且您不需要在循环中调用ResultSet.next()
来打印列名称。它们不会每行更改。
还可以假设您要在数据之前打印列名称。
打印列名称的循环应该是打印getObject()
值的循环的位置,而反之亦然。