公共类java连接数据库_JDBC 建立连接公共操作类(静态方式与单例方式)

package com.ighost.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

/**

*

* 数据库操作公共类

*

* @author ghost

*

*/

public final class JDBCUtil {

private static String url = "jdbc:mysql://localhost:3306/jdbc";

private static String username = "root";

private static String password = "";

// 构造函数私有话 不允许构造

private JDBCUtil() {

}

// 注册驱动

static {

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

throw new ExceptionInInitializerError(e);

}

}

// 获取连接

public static Connection getConnection() throws SQLException {

return DriverManager.getConnection(url, username, password);

}

// 释放资源

public static void free(ResultSet rs, Statement stmt, Connection conn) {

try {

if (rs != null) {

rs.close();

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (stmt != null) {

stmt.close();

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (conn != null) {

conn.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

单例方式

package com.ighost.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

/**

*

* 数据库操作公共类 单例模式 虚拟机里只存在一个这样的实例 通过getInstance方式获取对象

*

* @author ghost

*

*/

public final class JDBCUtilSingle {

private String url = "jdbc:mysql://localhost:3306/jdbc";

private String username = "root";

private String password = "";

// 构造函数私有话 不允许构造

private JDBCUtilSingle() {

}

// 构造私有实例

// private static JDBCUtilSingle instance = new JDBCUtilSingle();

private static JDBCUtilSingle instance = null;

public static JDBCUtilSingle getInstance() {

//延迟加载

if (instance == null) {

//加锁 防止线程并发

synchronized (JDBCUtilSingle.class) {

//必须有的判断

if(instance == null){

instance = new JDBCUtilSingle();

}

}

}

return instance;

}

// 注册驱动

static {

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

throw new ExceptionInInitializerError(e);

}

}

// 获取连接

public Connection getConnection() throws SQLException {

return DriverManager.getConnection(instance.url, instance.username,

instance.password);

}

// 释放资源

public static void free(ResultSet rs, Statement stmt, Connection conn) {

try {

if (rs != null) {

rs.close();

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (stmt != null) {

stmt.close();

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

if (conn != null) {

conn.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值