Java:从数据库中检索并在标签中显示值

huangapple go评论71阅读模式
英文:

Java: Retrieve and display value from database in label

问题

我正在使用Java Swing构建Java桌面应用程序

这是选择服务方法

```java
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(String.valueOf(numberOfDays.select(1)));     
    // numberOfDays.select(1);
    System.out.println("Entered: " + jTextField1.getText());

}

jLabel1.setText(numberOfDays.select(1));这一行出现错误,提示“在此处不允许使用void类型”。

我做错了什么?感谢有关此问题的任何输入。谢谢。


<details>
<summary>英文:</summary>

I am building a Java desktop app using Java Swing.

This is the select service method

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(&quot;DB status 1: &quot;+ preparedStatement.toString());

    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}

I would like to display the result of this in a label
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(&quot;Entered: &quot; + jTextField1.getText());
   
} 
There is an error at 
```        jLabel1.setText(numberOfDays.select(1));     ```
Saying &#39;void type not allowed here&#39;.

What am I doing wrong? Appreciate any input regarding this matter. Thank you.



</details>


# 答案1
**得分**: 0

根据错误提示,'void类型在此不允许',因此您应该在您的“select”方法中添加返回值。

```java
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状态 1:" + preparedStatement.toString();

    } catch (Exception ex) {
       return ex.toString();
    }
}
英文:

As the error says, 'void type not allowed here' so you should add return in your "select" method.

public String select(int id){
    String sql = &quot;SELECT num_of_working_days FROM working_days_and_hours WHERE id = ?&quot;;
    
     try {
        connection = SQLite_Connection.connect();
        preparedStatement = connection.prepareStatement(sql);
        
        preparedStatement.setInt(1, id);
        
        preparedStatement.execute();
        preparedStatement.close();

        return &quot;DB status 1: &quot;+ preparedStatement.toString();

    } catch (Exception ex) {
       return ex.toString();
    }
}

答案2

得分: 0

你的选择方法定义如下:

public void select(int id)

这将返回 void

查看 Java 文档中的 JLabel: setText(String str) 方法,该方法需要一个字符串数据类型。

因此,请将你的方法 select 修改为返回一个字符串,即:

public String select(int id)

另外,在你的方法中,像下面这样存储预处理语句的结果:

ResultSet rs = preparedStatement.executeQuery();  // 从查询中获取结果表
while (rs.next()) {  // 将光标定位到结果上
  String result = rs.getInt(1);  // 如果你的列是整数类型
}
preparedStatement.close();
return result;
英文:

your select method definition

public void select(int id)

This returns void

Looking at the Java doc JLabel: setText(String str)
This method requires a string DataType

Hence change ur method select to return a String i.e.

public String select(int id)

also in ur method Store the result from prepared statement like below

    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;

答案3

得分: 0

这是我提出的解决方案:

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;
}
英文:

This was the solution I came up with:

public ResultSet retrieveData(){
        String sql = &quot;SELECT num_of_working_days FROM working_days_and_hours&quot;;
        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;
    }

huangapple
  • 本文由 发表于 2020年9月11日 14:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定