jdbc 工具包实现,另附赠mysql-connector-java-5.1.46jar包
package com.sd.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
*
* @author Administrator
* 专门用来减少重复代码的
*/
public class DBUtils {
private static Properties properties = null;
private static Connection connection = null;
/**
* 禁止实例化
*/
private DBUtils(){}
static {
try {
/**
* 在类被加载的时候,读取配置文件中的值
* 将配置文件中的驱动加载
*/
properties = FileUtil.getProperties();
Class.forName(properties.getProperty("driver"));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建数据库链接
* 懒加载
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
/**
* 没有的时候进行船舰
* 有的时候则进行直接使用
*/
if(connection == null || connection.isClosed()){
connection = DriverManager.getConnection(properties.getProperty("url"),properties);
}
return connection;
}
/**
* 用来关闭数据库信息的
* @param statement
* @param connection
* @param resultSet
* @throws SQLException
*/
public static void close(Statement statement,Connection connection,ResultSet resultSet) throws SQLException{
if(statement != null ){
statement.close();
}
if(connection != null ){
connection.close();
}
if(resultSet != null ){
resultSet.close();
}
}
/**
* 用来关闭数据库信息的
* @param statement
* @param connection
* @param resultSet
* @throws SQLException
*/
public static void close(Statement statement,Connection connection) throws SQLException{
close(statement,connection,null);
}
}
其他代码传送门:http://www.mababa.xin/2022/03/18/454.html

浙公网安备 33010602011771号