* 创建项
使用到的 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 用户对应密码》