0

I am new to Java and NetBeans, and I am attempting to create a form that

  1. connects to a database using JDBC connection
  2. reads information from seven columns and displays them on a jTable component already on the form

I already have this working. I am now trying to optimize my code and use a better architecture to separate the database connection and the user interface (UI forms) code so that I can have a separate class to perform the connection and then simply call the method from this class in the code behind the button. The problem with using NetBeans and forms is that I don't know where to instantiate this class and such. Below is a cope of the class that I have created to perform the JDBC connection

public class ConnectionManager {
private static String url = "jdbc:mysql://localhost:3306/prototypeeop";
private static String driverName = "com.mysql.jdbc.Driver";
private static String username = "root";
private static String password = "triala";
private static Connection con;
private static String url;

public static Connection getConnection() {
    try {
        Class.forName(driverName);
        try {
            con = DriverManager.getConnection(url, username, password);
        } catch (SQLException ex) {
            // log an exception. fro example:
            System.out.println("Failed to create the database connection.");
        }
    } catch (ClassNotFoundException ex) {
        // log an exception. for example:
        System.out.println("Driver not found.");
    }
    return con;
}

}

This is already a .java file. I have a JForm, and I need to call this method behind the button. Here is how I do it in the form currently without using a connection class:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
    model.setRowCount(0);
    String sql="Select * from eopdata";
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/prototypeeop","root","jakamuga");
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery(sql);
        while(rs.next())
        {
            String Year=rs.getString("Year");
            String Month=rs.getString("Month");
            String Day=rs.getString("Day");
            String MJD=rs.getString("MJD");
            Double xarcsec=rs.getDouble("xarcsec");
            Double yarcsec=rs.getDouble("yarcsec");
            Double UT1UTCsec=rs.getDouble("UT1UTCsec");
            model.addRow(new Object[] { Year, Month, Day, MJD,xarcsec,yarcsec,UT1UTCsec});
        }
    }
    catch(Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }

How can I use the class instead of hard coding in the connection? I have already created the class but where do I instantiate it. Do I do it in the main of the form or do I do it in the actionevent code with the following code?

private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;

con = ConnectionManager.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
4

1 回答 1

3

To literally answer your question: your getConnection method is a public static method so you can call this from anywhere. Just call ConnectionManager.getConnection() where-ever you need that connection.

Some other remarks about your code:

  • You shouldn't query a database in the actionPerformed method. This method is called on the EDT, and doing a database query and looping over the results is a long-running task. Doing this task on the EDT will block your UI. Consult the Concurrency in Swing tutorial for more info about Swing and threading
  • Consider caching the Connection object
  • Do not forget to close your resources. If I remember correctly, a ResultSet must be closed afterwards. Do this in a finally block
于 2012-06-08T15:57:31.980 回答