Java 17 Recipes


-6. Simplifying Connection Management


Download 3.2 Mb.
Pdf ko'rish
bet227/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   223   224   225   226   227   228   229   230   ...   245
Bog'liq
Java 17 Recipes

12-6. Simplifying Connection Management
 Problem
Your application requires the use of a database, and to work with the database, you need 
to open a connection for each interaction. Rather than code the logic to open a database 
connection every time you need to access the database, you want to use a single class to 
perform that task.
 Solution
Write a class to handle all the connection management within your application. Doing 
so allows you to call that class to obtain a connection, rather than setting up a new 
Connection object each time you need access to the database. Perform the following 
steps to set up a connection management environment for your JDBC application.
1. Create a class named CreateConnection.java that encapsulates 
your application’s connection logic.
2. Create a Properties file to store your connection information. 
Place the file somewhere on your classpath so that the 
CreateConnection class can load it.
3. Use the CreateConnection class to obtain your database 
connections.
The following code of the CreateConnection class can be used for centralized 
connection management.
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
Chapter 12 Working With Databases


445
public class CreateConnection {
static Properties props = new Properties();
String hostname = null;
String port = null;
String database = null;
String username = null;
String password = null;
public CreateConnection(){
try (InputStream in = Files.newInputStream(FileSystems 
.getDefault().
getPath(System.getProperty("user.dir") +
File.separator + "db_props 
.properties")); ) {
props.load(in);
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
loadProperties();
}
public final void loadProperties(){
this.hostname = props.getProperty("host_name");
port = props.getProperty("port_number");
database = props.getProperty("db_name");
username = props.getProperty("username");
password = props.getProperty("password");
}
public Connection getConnection() throws SQLException {
Connection conn = null;
String jdbcUrl;
jdbcUrl = "jdbc:mysql://" + this.hostname + ":" +
this.port + "/" + this.database;
Chapter 12 Working With Databases


446
System.out.println(jdbcUrl);
conn = DriverManager.getConnection(jdbcUrl, this.username, 
this.password);
System.out.println("Successfully connected");
return conn;
}
public static void main(String[] args) {
CreateConnection createConnection = new CreateConnection();
try {
Connection conn = createConnection.getConnection();
if (conn != null) {
System.out.println("Closing 
Connection...");
conn.close();
}
} catch (SQLException e){
e.printStackTrace();
}
}
}
The following is the output.
jdbc:mysql://localhost:3306/apressbooks
Successfully connected
Closing Connection...
Next, the following lines of text are an example of what should be contained in the 
properties file that is used for obtaining a connection to the database. For this example, 
the properties file is named db_props.properties.
host_name=your_db_server_name
db_name=your_db_name
username=db_username
password=db_username_password
port_number=db_port_number
Chapter 12 Working With Databases


447
Finally, use the CreateConnection class to obtain connections for your application. 
The following code demonstrates this concept.
CreateConnection createConn = new CreateConnection();
try(Connection conn = createConn.getConnection()) {
performDbTask();
} catch (java.sql.SQLException ex) {
ex.printStackTrace();
}
This code uses try-with-resources to automatically close the connection after 
performing the database task.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   223   224   225   226   227   228   229   230   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling