gpt4 book ai didi

java - 如何让SQL连接语句通用

转载 作者:可可西里 更新时间:2023-11-01 07:18:37 26 4
gpt4 key购买 nike

我必须在同一类的不同方法中执行多个 SQL 查询。有什么方法可以使这些语句通用,我可以在所有方法中使用相同的 con,statement 变量来执行查询。

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/kamal","root","root");
Statement statement=con.createStatement();

最佳答案

在你的类中使用这个方法并一次又一次地调用它

public Connection getMyConnection() throws ClassNotFoundException, SQLException
{
String connectionURL = "jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(connectionURL, "root", "root");
return con;
}

关于java - 如何让SQL连接语句通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14914494/

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