简体   繁体   English

java中main的mysql连接的调用方法

[英]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. 我正在学习Java,我用netbeans添加了mysql连接器,我试图做一个示例应用来查询数据库,但是我不知道如何从主方法中调用建立连接的方法。

here is my code: 这是我的代码:

MySqlJdbcTest.java: 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: 这是test.java文件:

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. 您有2个main(String[])方法。 Java requires that you have one of these as the entry point to your application. Java要求您将其中之一作为应用程序的入口点。

If we assume that Test.main() is the entry point for your application, you can call the other as 如果我们假设Test.main()是应用程序的入口点,则可以将另一个调用为

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

which is equivalent to calling a static method. 这等效于调用static方法。

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. 但是,为了使情况更清楚,建议您将MySqlJdbcTest.main()方法重命名为其他名称,并可能删除static修饰符和String[]参数。 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. 考虑在此处阅读官方Java教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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