我正在尝试用 Java 打印数据库中的表值。我已成功创建连接并且也能够创建表。但我在打印表值时遇到问题。
尝试
{
Statement stmt = conn.createStatement();
ResultSet rs;
String getValues =
"SELECT * " +
"FROM EMPLOYEE" /*+ this.tableName*/ + " ; ";
//this.executeUpdate(conn, getValues);
rs = stmt.executeQuery(getValues);
String printValues = rs.getString(???Want to print all the table values);
System.out.println(printValues);
System.out.println("Values Retrived");
}
catch (SQLException e)
{
System.out.println("ERROR: Could not get values from table");
e.printStackTrace();
return;
}
}
最佳答案
您将需要使用 while 循环来循环结果集。下面的例子:
public static ObservableList<Customer> search_ForDropDown(String searchQuery){
MysqlDataSource dataSource = CurrentServer.getDataSource();
ObservableList<Customer> data = FXCollections.observableArrayList();
data.clear();
try {
String query = "SELECT * FROM CUSTOMER LIMIT 5";
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Customer customer = new Customer();
customer.setID(rs.getInt("id"));
customer.setPhoneNumber(rs.getString("phoneNumber"));
customer.setEmailAddress(rs.getString("emailAddress"));
data.add(customer);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return data;
}
关于java - 使用 Java 打印 MySQL 表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30062879/