数据库:JDBC:代码:创建JDBCUtil + properties 配置文件

* 创建项

使用到的 jar:mysql-connector-java-5.1.7-bin.jar。

创建:JDBCUtil + jdbc.properties

设计模式:connection加成员锁的饱汉式 + 门户模式

遇到的 Exception:ClassNotFoundException:创建 connection 时候应该把它移到 ResourceBundle 读取完配置文件之后,否则无法实例化。

1、JDBCUtil:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;



/**
 * 创建 JDBCUtil
 *
 */
public final class JDBCUtil 
{
	
	/*================= static 静态设置 =================*/
	
	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	
	// 获取 jdbc.properties 的文件信息
	static
	{
		ResourceBundle bundle = ResourceBundle.getBundle("JDBC/jdbc");
		
		// 设置 JDBC 基本参数
		driver   = bundle.getString("driver");
		url      = bundle.getString("url");
		user     = bundle.getString("user");
		password = bundle.getString("password");
	}
	
	// 创建单例连接
	private static Connection connection = getConnection();
	
	
	
	
	/*================= public 公共方法 =================*/
	
	
	/**
	 * 创建 statement
	 * @param connection
	 * @return
	 */
	public static Statement getStatement(Connection connection)
	{
		// 判断是否为空,否则使用本类的连接
		try 
		{
			if(connection == null)
			{
				connection = getConnection();
			}
			return connection.createStatement();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 返回 resultSet 的 sql 查询 方法
	 */
	public static ResultSet getResultSet(Statement statement,String sql)
	{
		try
		{
			if(statement == null)
			{
				statement = getStatement(getConnection());
			}
			return statement.executeQuery(sql);
		}
		catch(SQLException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	
	/**
	 * 关闭
	 * @param args
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static void releaseAll(Connection conn,Statement statement,ResultSet resultSet)
	{
		try
		{
			// 首先关闭 ResultSet
			if(resultSet != null)
			{
				resultSet.close();
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			// 然后关闭 statement
			try
			{
				if(statement != null)
				{
					statement.close();
				}
			}
			catch(SQLException e)
			{
				e.printStackTrace();
			}
			finally
			{
				// 最后关闭 connection
				try
				{
					if(conn != null)
					{
						conn.close();
					}
					else if(connection != null)
					{
						connection.close();
					}
				}
				catch(SQLException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
	
	
	
	
	/*================= private 私有方法 =================*/
	
	
	/**
	 * 创建JDBC连接
	 * @return
	 */
	private static Connection getConnection()
	{
		// 使用锁并返回单例
		try
		{	
			if(connection == null)
			{
				synchronized(JDBCUtil.class)
				{
					if(connection == null)
					{
						Class.forName(driver);
						connection = DriverManager.getConnection(url,user,password);
					}
				}
			}
			return connection;
		}
		catch(SQLException | ClassNotFoundException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	
	/**
	 * 不允许实例化
	 */
	private JDBCUtil(){}
	
	
	
	
	
	/*================= 测试方法  =================*/
	
	
	public static void main(String[] args) throws SQLException
	{

		ResultSet set = getResultSet(null, "select * from user");
		while(set.next())
		{
			System.out.println(set.getLong("user_id"));
		}
		releaseAll(null, null, set);
	}
	

}

2、jdbc.properties 文件,放置在 /JDBC/jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/《你的 mysql 数据库名》
user=《你的 mysql 用户名》
password=《你的 mysql 用户对应密码》

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值