gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:36:20 26 4
gpt4 key购买 nike

我正在学习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();
}
}

最佳答案

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

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

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

这相当于调用静态方法。

但是,为了让事情更清楚,我建议您将 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();
}
}

考虑通读 official Java tutorials, here.

关于java - java中从main调用mysql连接的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18689769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com