Search code examples
javasqliteswingservicecrud

Java: Implementing service methods in Java Swing


I'm building a desktop app using Java Swing, I've connected the SQLite DB and implemented the service classes already but I'm unable to figure out how to call these methods and implement them in the app.

This is one of the service classes I have created, I have confirmed that they successfully connect to the 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);

//    }

    
        
}

My question is I want to use these methods but I have little understanding of how to.

How do I call the insert method to a button method and how do I call the select method to a text field?


Solution

  • You use it by creating an instance of the class, then call the methods, e.g.:

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

    However, there are a lot of issues with that code, e.g.:

    • Don't catch exceptions and ignore them. In a Swing application, printing likely goes nowhere, so they are truly ignored in that code.

    • Since you connect to the database in each method, don't put Connection in a static field. Use a local variable.

    • Don't put PreparedStatement, Statement, and ResultSet in fields. Use local variables.

    • Close all the JDBC resources by using try-with-resources.

    • Don't use string concatenation to insert values in a SQL statement. Learn how to use PreparedStatement.

    • The select() method needs to use the ResultSet to get the data before returning.

    • ...