0

Novice here. How do I organize a JDBC program? I'm making a SQL GUI so my co-workers can have a safer and easier way to run queries and make small changes. I've had success with making it but I think it can be improved.

I've been using the following from https://www.tutorialspoint.com/jdbc/jdbc-select-records.htm as boilerplate. Currently each SQL query is its own method that connects, tries, catches, closes, etc. In an effort to not duplicate code can I have a method that connects/opens and disconnects/closes resources? I suspect not because of the tries and catches.

I can't just substitute queries because the result set needs to be extracted by the column name, and some options are statements that update rows.

The program also writes the data to a jTable and saves as an Excel using the Apache POI if anyone has any off the cuff design tips or best practices on that as well.

Any other overall suggestions would be appreciated. Thanks.

//STEP 1. Import required packages
import java.sql.*;

public class JDBCExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";

   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("Connected database successfully...");

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();

      String sql = "SELECT id, first, last, age FROM Registration";
      ResultSet rs = stmt.executeQuery(sql);
      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         int id  = rs.getInt("id");
         int age = rs.getInt("age");
         String first = rs.getString("first");
         String last = rs.getString("last");

         //Display values
         System.out.print("ID: " + id);
         System.out.print(", Age: " + age);
         System.out.print(", First: " + first);
         System.out.println(", Last: " + last);
      }
      rs.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            conn.close();
      }catch(SQLException se){
      }// do nothing
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");
}//end main
}//end JDBCExample
  • Way are you trying to implement an SQL "GUI"? There is plenty of open source options.... – AR1 Feb 17 '17 at 16:20
  • I was trying to make a program to do common tasks for my coworkers who don't know SQL. What are the open source options? – NullPointerProliferator Feb 17 '17 at 16:27
  • Many off the tools [cited](http://stackoverflow.com/questions/2760887/frontend-tool-to-manage-h2-database) work well with MySQL, too. – trashgod Feb 17 '17 at 17:05
  • The SQuirreL program looks like it has as many bells and whistles as SQL Server Management Studio. The gui that I'm building has a drop down for queries and an execute button, that's it. – NullPointerProliferator Feb 17 '17 at 19:06

0 Answers0