Java: 从数据库中检索并显示标签中的值
Java: Retrieve and display value from database in label
我正在使用 Java Swing 构建一个 Java 桌面应用程序。
这是select服务方式
public void select(int id){
String sql = "SELECT num_of_working_days FROM working_days_and_hours WHERE id = ?";
try {
connection = SQLite_Connection.connect();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.execute();
preparedStatement.close();
System.out.println("DB status 1: "+ preparedStatement.toString());
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
我想在标签中显示这个结果
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jLabel1.setText(numberOfDays.select(1));
// numberOfDays.select(1);
System.out.println("Entered: " + jTextField1.getText());
}
出现错误
jLabel1.setText(numberOfDays.select(1));
说 'void type not allowed here'.
我做错了什么?感谢有关此事的任何意见。谢谢。
如错误所述,'void type not allowed here' 因此您应该在“select”方法中添加 return。
public String select(int id){
String sql = "SELECT num_of_working_days FROM working_days_and_hours WHERE id = ?";
try {
connection = SQLite_Connection.connect();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.execute();
preparedStatement.close();
return "DB status 1: "+ preparedStatement.toString();
} catch (Exception ex) {
return ex.toString();
}
}
你的select方法定义
public void select(int id)
这个return秒void
查看 Java 文档 JLabel: setText(String str)
此方法需要一个字符串 DataType
因此将您的方法 select 更改为 return 字符串,即
public String select(int id)
也在你的方法中存储准备好的语句的结果,如下所示
ResultSet rs = preparedStatement.executeQuery(); // Get the result table from the q
while (rs.next()) { // Position the cursor to the result
String result = rs.getInt(1); //If ur Column is of int type
}
preparedStatement.close();
return result;
这是我想出的解决方案:
public ResultSet retrieveData(){
String sql = "SELECT num_of_working_days FROM working_days_and_hours";
try {
connection = SQLite_Connection.connect();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
} catch (Exception ex) {
System.out.println(ex.toString());
}finally {
//Services.colsedConnections();
}
return resultSet;
}
我正在使用 Java Swing 构建一个 Java 桌面应用程序。
这是select服务方式
public void select(int id){
String sql = "SELECT num_of_working_days FROM working_days_and_hours WHERE id = ?";
try {
connection = SQLite_Connection.connect();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.execute();
preparedStatement.close();
System.out.println("DB status 1: "+ preparedStatement.toString());
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
我想在标签中显示这个结果
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jLabel1.setText(numberOfDays.select(1));
// numberOfDays.select(1);
System.out.println("Entered: " + jTextField1.getText());
}
出现错误
jLabel1.setText(numberOfDays.select(1));
说 'void type not allowed here'.
我做错了什么?感谢有关此事的任何意见。谢谢。
如错误所述,'void type not allowed here' 因此您应该在“select”方法中添加 return。
public String select(int id){
String sql = "SELECT num_of_working_days FROM working_days_and_hours WHERE id = ?";
try {
connection = SQLite_Connection.connect();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.execute();
preparedStatement.close();
return "DB status 1: "+ preparedStatement.toString();
} catch (Exception ex) {
return ex.toString();
}
}
你的select方法定义
public void select(int id)
这个return秒void
查看 Java 文档 JLabel: setText(String str)
此方法需要一个字符串 DataType
因此将您的方法 select 更改为 return 字符串,即
public String select(int id)
也在你的方法中存储准备好的语句的结果,如下所示
ResultSet rs = preparedStatement.executeQuery(); // Get the result table from the q
while (rs.next()) { // Position the cursor to the result
String result = rs.getInt(1); //If ur Column is of int type
}
preparedStatement.close();
return result;
这是我想出的解决方案:
public ResultSet retrieveData(){
String sql = "SELECT num_of_working_days FROM working_days_and_hours";
try {
connection = SQLite_Connection.connect();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
} catch (Exception ex) {
System.out.println(ex.toString());
}finally {
//Services.colsedConnections();
}
return resultSet;
}