【问题标题】:Java: Implementing service methods in Java Swing [closed]Java:在 Java Swing 中实现服务方法 [关闭]
【发布时间】:2020-12-25 13:35:01
【问题描述】:

我正在使用 Java Swing 构建桌面应用程序,我已经连接了 SQLite DB 并实现了服务类,但我无法弄清楚如何调用这些方法并在应用程序中实现它们。

这是我创建的服务类之一,我已确认它们已成功连接到数据库

public class NumOfDaysService {
    
    private static Connection connection;
    private static PreparedStatement preparedStatement;
    private static Statement stmt;
    ResultSet resultSet;
    
    public void addNumberOfDays(WorkingDaysAndHoursModel work) {
        int numberOfDays = work.getNum_of_working_days();
           
        String sql = "INSERT INTO working_days_and_hours(num_of_working_days) VALUES('"+numberOfDays+"')";

      try {
            connection = SQLite_Connection.connect();
            preparedStatement = connection.prepareStatement(sql);
            boolean result = preparedStatement.execute();
            System.out.println("DB status: "+ result);
        } catch (Exception ex) {
            System.out.println(ex.toString());
            //Logger.getLogger(Services.class.getName()).log(Level.SEVERE, null, ex);
        }finally {      
                 // Services.colsedConnections();
        }  
    }
    
     public void select(){
        String sql = "SELECT num_of_working_days FROM working_days_and_hours";
        
         try {
            connection = SQLite_Connection.connect();
            stmt = connection.createStatement();
             resultSet = stmt.executeQuery(sql);
            System.out.println("DB status: "+ resultSet);
        } catch (Exception ex) {
            System.out.println(ex.toString());
            //Logger.getLogger(Services.class.getName()).log(Level.SEVERE, null, ex);
        }finally {      
                 // Services.colsedConnections();
        }  
    }
    
   
     
     public void update(int id, int num) {
        String sql = "UPDATE working_days_and_hours SET num_of_working_days = '"+num+"' WHERE id = '"+id+"'";

        try {
            connection = SQLite_Connection.connect();
            preparedStatement = connection.prepareStatement(sql);
            
            preparedStatement.setInt(1, id);
            preparedStatement.setInt(2, num);

            preparedStatement.executeUpdate();
            
            System.out.println("DB status: "+ preparedStatement);
            
        } catch (Exception ex) {
            System.out.println(ex.toString());
            //Logger.getLogger(Services.class.getName()).log(Level.SEVERE, null, ex);
        }finally {      
                 // Services.colsedConnections();
        }  
    }
        
     public void delete(int id) {
        String sql = "DELETE FROM working_days_and_hours WHERE id = ?";

        try {
            
            connection = SQLite_Connection.connect();
            preparedStatement = connection.prepareStatement(sql);

            // set the corresponding param
            preparedStatement.setInt(1, id);
            // execute the delete statement
            preparedStatement.executeUpdate();
            
        } catch (Exception ex) {
            System.out.println(ex.toString());
            //Logger.getLogger(Services.class.getName()).log(Level.SEVERE, null, ex);
        }finally {      
                 // Services.colsedConnections();
        }  
    }
     
//      public static void main(String[] args) {
//        NumOfDaysService lecturerService = new NumOfDaysService();
//        WorkingDaysAndHoursModel lecturer = new WorkingDaysAndHoursModel(3);
//        Lecturer lecturer2 = new Lecturer(0, "abcd", "123456", "eng", "OC", "Malabe", "new", "level", "rank");

//        lecturerService.addNumberOfDays(lecturer);
//        lecturerService.select();
//        lecturerService.delete(2);
//lecturerService.update(3, 2);

//    }

    
        
}

我的问题是我想使用这些方法,但我对如何使用知之甚少。

如何将insert方法调用为按钮方法,如何将select方法调用为文本字段?

【问题讨论】:

  • 所以,实例化类,调用方法。

标签: java sqlite swing service crud


【解决方案1】:

您通过创建类的实例来使用它,然后调用方法,例如:

NumOfDaysService service = new NumOfDaysService();
service.addNumberOfDays(work);

但是,该代码存在 很多 问题,例如:

  • 不要捕获异常并忽略它们。在 Swing 应用程序中,打印可能无处可去,因此在该代码中它们确实被忽略了。

  • 由于您在每个方法中都连接到数据库,因此不要将Connection 放在静态字段中。使用局部变量。

  • 不要将PreparedStatementStatementResultSet 放在字段中。使用局部变量。

  • 使用 try-with-resources 关闭所有 JDBC 资源。

  • 不要使用字符串连接在 SQL 语句中插入值。了解如何使用PreparedStatement

  • select()方法返回前需要使用ResultSet获取数据。

  • ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2012-04-07
    • 2012-06-27
    • 2010-10-09
    • 1970-01-01
    • 2010-11-21
    相关资源
    最近更新 更多