如何使用Singleton Design Pattern java连接到SQL数据库?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Singleton Design Pattern java连接到SQL数据库?相关的知识,希望对你有一定的参考价值。
此外,谁可以显示与本机代码的区别?例如,使用Java连接到数据库并使用Singleton Design模式执行此操作。使用它有什么好处?
答案
您的连接实例应该被保留,您只能通过getConnection()方法检索您的连接(Singleton模式的getInstance)
这是一个示例代码;
A - Sample Database Utility Based On Singleton Design Pattern of Gang Of Four
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private final String HOST;
private final String PORT;
private final String DB_NAME;
private final String USER;
private final String PASSWORD;
private final String TARGET_TABLE;
private final String COLUMN_NAME;
private final String CONNECTION_STRING;
private final String SELECT_ALL_SQL;
private final String SELECT_COLUMN_SQL;
private final String INSERT_SQL;
private Connection conn;
public DBUtil() {
// Database Parameters
HOST = "your_host_here";
PORT = "your_port_number_here";
DB_NAME = "your_database_name";
USER = "your_databse_user_here";
PASSWORD = "your_database_password_here";
// Database Tables
TARGET_TABLE = "your_table_name";
COLUMN_NAME = "your_tables_column_name";
CONNECTION_STRING = String.format("jdbc:mysql://%s:%s/%s", HOST, PORT, DB_NAME);
// Query Strings
SELECT_ALL_SQL = String.format("SELECT * FROM %s.%s WHERE id=?", DB_NAME, TARGET_TABLE);
SELECT_COLUMN_SQL = String.format("SELECT %s FROM %s.%s WHERE id=?", COLUMN_NAME, DB_NAME, TARGET_TABLE);
INSERT_SQL = String.format("INSERT INTO %s.%s (id, name) values (?, ?)", DB_NAME, TARGET_TABLE);
printConfiguration();
conn = null;
}
private void printConfiguration() {
System.out.println("******************************************************************************");
System.out.println("DB_NAME : " + DB_NAME);
System.out.println("USERNAME : " + USER);
System.out.println("PASSWORD : " + PASSWORD);
System.out.println("CAMT TABLE : " + TARGET_TABLE);
System.out.println("******************************************************************************");
}
private void getConnection() {
if(conn != null) {
System.out.println("Connection already established with db");
return;
}
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(CONNECTION_STRING, USER, PASSWORD);
} catch (ClassNotFoundException e) {
System.out.println("Connection FAILED");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeConnection() {
if(conn == null) {
return;
}
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
public int insert(int id, String column) {
int rowNum;
getConnection();
PreparedStatement preparedStatement = null;
try {
preparedStatement = conn.prepareStatement(INSERT_SQL);
System.out.println("SQL: " + INSERT_SQL);
preparedStatement.setInt(1, id);
preparedStatement.setString(2, column);
rowNum = preparedStatement.executeUpdate();
System.out.println(rowNum + " rows are inserted");
return rowNum;
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public Model getModel(int id) {
getConnection();
PreparedStatement preparedStatement = null;
Model model = new Model();
try {
preparedStatement = conn.prepareStatement(SELECT_ALL_SQL);
System.out.println("SQL: " + SELECT_ALL_SQL);
preparedStatement.setInt(1, id);
ResultSet rs = preparedStatement.executeQuery();
if(rs.next()) {
model.setId(rs.getInt("id"));
model.setName(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return model;
}
public String getName(int id) {
getConnection();
PreparedStatement preparedStatement = null;
String name = null;
try {
preparedStatement = conn.prepareStatement(SELECT_COLUMN_SQL);
System.out.println("SQL: " + SELECT_ALL_SQL);
preparedStatement.setInt(1, id);
ResultSet rs = preparedStatement.executeQuery();
if(rs.next()) {
name = rs.getString("name");
}
} catch (SQLException e) {
e.printStackTrace();
}
return name;
}
}
B - Model Used in Singleton Database Utility Demo
public class Model {
private int id;
private String name;
public Model() {}
public Model(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
以上是关于如何使用Singleton Design Pattern java连接到SQL数据库?的主要内容,如果未能解决你的问题,请参考以下文章
[Design Pattern] Singleton Pattern 简单案例
从壹开始 [ Design Pattern ] 之二 ║ 单例模式 与 Singleton