java中的数据库连接(Database Connectivity in java)

编程入门 行业动态 更新时间:2024-10-26 18:22:49
java中的数据库连接(Database Connectivity in java)

首先,我向您展示代码,然后问几个问题。 我有这样的类数据库连接(请忽略语法错误,如果有的话)

class DatabaseConnection { private static Connection connection = null; private static String driverName=""; private static String userName=""; private static String passwrod=""; private static String url=""; private DatabaseConnection() { } public static void createConnection() { if ( connection == null ) { // read database credentials from xml file and set values of driverName, userName, passowrd and url //create connection with database and set store this connection in connection object created a class level. } } public static void closeConnection1() throws Exception{ if ( connection != null ) { connection.close(); connection == null } } public static void closeConnection2() throws Exception{ if ( connection != null ) { connection.close(); } } public void insertData(Object data) { // insetData in database } }

我想知道哪个紧密连接在数据库连接中更加优化。 让我们假设我有这样的测试类

class Test { public static void main(String args[]) { DatabaseConnection.createConnection(); DatabaseConnection.insertData(data); DatabaseConnection.closeConnection2(); // we call also called close connection method within the insertData method after inserting the data } }

创建数据库连接后,我在数据库中插入数据,然后使用closeConnection2方法关闭连接。 以这种方式连接已经关闭所以如果我想插入更多的方法然后我必须重新创建与数据库的连接但我不能这样做因为连接对象不是null并且createConnection没有执行if语句内的代码。 现在,如果我调用closeConnection1方法来关闭连接,那么在执行此操作时,我必须再次解析xml文件以获取不是优化解决方案的凭据。 你能告诉我哪种方法好,如果两者都更糟,请告诉我更有效的方法来创建和关闭数据库连接。

First i show you the code then asked few questions. i have a class database connectivity like this (please ignore syntax error if any)

class DatabaseConnection { private static Connection connection = null; private static String driverName=""; private static String userName=""; private static String passwrod=""; private static String url=""; private DatabaseConnection() { } public static void createConnection() { if ( connection == null ) { // read database credentials from xml file and set values of driverName, userName, passowrd and url //create connection with database and set store this connection in connection object created a class level. } } public static void closeConnection1() throws Exception{ if ( connection != null ) { connection.close(); connection == null } } public static void closeConnection2() throws Exception{ if ( connection != null ) { connection.close(); } } public void insertData(Object data) { // insetData in database } }

I want to know which close connection is more optimize in database connection. Lets suppose I have test class like this

class Test { public static void main(String args[]) { DatabaseConnection.createConnection(); DatabaseConnection.insertData(data); DatabaseConnection.closeConnection2(); // we call also called close connection method within the insertData method after inserting the data } }

After creating database connection i insert data in database and then close the connection using closeConnection2 method. in this way connection has been close so if i want to insert some more method then i have to recreate connection with the database but i can't do this because connection object is not null and createConnection didn't execute the code inside the if statement. Now if I called closeConnection1 method for closing connection then in doing this i have to parse xml file again for credential which is not a optimize solution. can you tell me which method is good and if both are worse then please tell me more efficient way for creating and closing database connection.

最满意答案

我看到两个主要问题:

事实(包括Connection对象)是静态的这一事实意味着您不能同时从多个线程使用此类。 解析配置数据和打开连接是一个单独的问题,不应该混合。 至少将它们移动到单独的方法中,配置甚至可能会进入另一个类。

仅第二件事就是避免多次解析连接信息。

更好的方法是使用DataSource而不是每次都打开连接。 然后使用一个实际上是连接池的DataSource !

I see two major problems with this:

The fact that everything (including the Connection object) is static means that you can't ever use this class from more than one thread at once. parsing the configuration data and opening the connection are separate concerns and should not be mixed. At least move them into separate methods, the configuration could probably even go in another class.

The second thing alone will avoid having to parse the connection information multiple times.

An even better approach would be to use a DataSource instead of opening the connections each time. And then use a DataSource that's actually a connection pool!

更多推荐

本文发布于:2023-07-24 00:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239316.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库连接   java   Connectivity   Database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!