0

我正在学习java,我用netbeans添加了mysql连接器,我试图做一个示例应用程序来查询数据库,但是我不知道如何调用从主方法建立连接的方法。

这是我的代码:

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(); }
        }
    }
}

这是 test.java 文件:

public class Test {

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

1 回答 1

1

你有2main(String[])种方法。Java 要求您将其中之一作为应用程序的入口点。

如果我们假设这Test.main()是您的应用程序的入口点,您可以将另一个称为

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

这相当于调用一个static方法。

但是,为了让事情更清楚,我建议您将MySqlJdbcTest.main()方法重命名为其他名称,并可能删除static修饰符和String[]参数。它最终会像

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(); }
        }
    }
}

因为该方法现在是一个实例方法,所以您需要一个类的实例来运行它

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

考虑在此处阅读官方 Java 教程。

于 2013-09-09T00:00:02.847 回答