简体   繁体   中英

call method for mysql connection from main in java

im learning java, i added the mysql connector with netbeans and im trying to do a sample app to query a database, however i dont know how to call the method that makes the connection from the main method.

here is my code:

MySqlJdbcTest.java:

public class MySqlJdbcTest {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
    //          new com.mysql.jdbc.Driver();
            Class.forName("com.mysql.jdbc.Driver").newInstance();
                        //conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=");
            String connectionUrl = "jdbc:mysql://localhost:3306/test";
            String connectionUser = "root";
            String connectionPassword = "";
            conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM java");
            while (rs.next()) {
                String id = rs.getString("id");
                String firstName = rs.getString("name");
                String lastName = rs.getString("number");
                System.out.println("ID: " + id + ", First Name: " + firstName
                        + ", Last Name: " + lastName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }
}

and here is the test.java file:

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        MySqlJdbcTest prueba = new MySqlJdbcTest();
        prueba();
    }
}

You have 2 main(String[]) methods. Java requires that you have one of these as the entry point to your application.

If we assume that Test.main() is the entry point for your application, you can call the other as

public static void main(String[] args) {
    MySqlJdbcTest.main(args);
}

which is equivalent to calling a static method.

However, to make things more clear, I suggest you rename the MySqlJdbcTest.main() method to something else and possibly remove the static modifier and the String[] parameter. It would end up as something like

public class MySqlJdbcTest {
    public void executeSql() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
    //          new com.mysql.jdbc.Driver();
            Class.forName("com.mysql.jdbc.Driver").newInstance();
                        //conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=");
            String connectionUrl = "jdbc:mysql://localhost:3306/test";
            String connectionUser = "root";
            String connectionPassword = "";
            conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM java");
            while (rs.next()) {
                String id = rs.getString("id");
                String firstName = rs.getString("name");
                String lastName = rs.getString("number");
                System.out.println("ID: " + id + ", First Name: " + firstName
                        + ", Last Name: " + lastName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }
}

Because the method is now an instance method, you need an instance of the class to run it on

public class Test {
    public static void main(String[] args) {
        MySqlJdbcTest prueba = new MySqlJdbcTest();
        prueba.executeSql();
    }
}

Consider reading through the official Java tutorials, here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM