本文是小编为大家收集整理的关于Java数据库最小化连接创建的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我是 Java 的初学者,并且是我的一项任务.我必须做数据库操作.我有这个用于将数据插入数据库的 Java 代码,它工作正常.
public class JavaDBWork { public static void main(String[] args) { try { // create a mysql database connection String myDriver = "org.gjt.mm.mysql.Driver"; String myUrl = "jdbc:mysql://localhost/test"; Class.forName(myDriver); Connection conn = DriverManager.getConnection(myUrl, "root", ""); // create a sql date object so we can use it in our INSERT statement Calendar calendar = Calendar.getInstance(); java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime()); // the mysql insert statement String query = " insert into users (first_name, last_name, date_created, is_admin, num_points)" + " values (?, ?, ?, ?, ?)"; //Other Code }
我的问题是每次代码运行时,我的代码都必须创建数据库连接和准备好的语句对象,据我所知,这是一项昂贵的操作.
有什么方法可以让我保持与数据库的单一连接并使用它来执行我的数据库工作?
推荐答案
你可以像这样使用设计模式Singleton Connection:
1- 创建一个类 SingletonConnection.java 看起来像这样:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.EmptyStackException; public class SingletonConnection { private static Connection connection = null ; static { try{ Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection ("jdbc:mysql://localhost:3306/gestion_comptes","root",""); }catch(Exception ex) { } } public static Connection getConnection() throws Exception { return connection; } }
在你的其他班级中这样称呼它:
public class DaoImpl{ public Connection connection = SingletonConnection.getConnection(); public DaoImpl() throws Exception { if(connection==null) throw new Exception("impossible de se connecter à la base de données"); } }
问题描述
I am a very beginner to Java and for one of my assignments. I have to do database operations. I have this Java code for inserting data to a database and it works fine.
public class JavaDBWork { public static void main(String[] args) { try { // create a mysql database connection String myDriver = "org.gjt.mm.mysql.Driver"; String myUrl = "jdbc:mysql://localhost/test"; Class.forName(myDriver); Connection conn = DriverManager.getConnection(myUrl, "root", ""); // create a sql date object so we can use it in our INSERT statement Calendar calendar = Calendar.getInstance(); java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime()); // the mysql insert statement String query = " insert into users (first_name, last_name, date_created, is_admin, num_points)" + " values (?, ?, ?, ?, ?)"; //Other Code }
My question is every time the code runs, my code has to create a database connection and prepared statement objects and as I understand it is a costly operation.
Is there any way that I can keep a single connection to database and use that to perform my database work?
推荐答案
You can use the design pattern Singleton Connection like this :
1- create a class SingletonConnection.java look like that :
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.EmptyStackException; public class SingletonConnection { private static Connection connection = null ; static { try{ Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection ("jdbc:mysql://localhost:3306/gestion_comptes","root",""); }catch(Exception ex) { } } public static Connection getConnection() throws Exception { return connection; } }
And in your other class call it like that :
public class DaoImpl{ public Connection connection = SingletonConnection.getConnection(); public DaoImpl() throws Exception { if(connection==null) throw new Exception("impossible de se connecter à la base de données"); } }