“获得数据库连接”操作,将在以后的增删改查所有功能中都存在,可以封装工具类JDBCUtils。
提供获取连接对象的方法,从而达到代码的重复利用。
未加配置文件
package demo;
import java.sql.*;
public class JDBCUtils {
static String url = “jdbc:mysql://localhost:3306/day04”;
static String user = “root”;
static String password = “root”;
//静态代码块,随类加载
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接对象
public static Connection conn() throws SQLException {
Connection connection = DriverManager.getConnection(url,user,password);
return connection;
}
//给与外部调用
public static void Close(Connection connection, Statement statement, ResultSet resultSet){
connectionClose(connection);
statementClose(statement);
resultClose(resultSet);
}
//私有方法让关闭流
private static void statementClose(Statement statement){
if (statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void connectionClose(Connection connection){
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void resultClose(ResultSet resultSet){
if (resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
加配置文件
package mysql;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtil {
static String driverClass = null;
static String url = null;
static String name = null;
static String password = null;
static {
try {
Properties properties = new Properties();
//导入输入流
InputStream is = new FileInputStream("G:\\javaEE\\develop_mysql\\mysqlCode\\src\\jdbc.properties");
// InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//加载配置文件
properties.load(is);
//获取配置文件数据
driverClass = properties.getProperty("driverClass");
// System.out.println(driverClass);
url = properties.getProperty("url");
name= properties.getProperty("name");
password= properties.getProperty("password");
Class.forName(driverClass);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() throws SQLException {
//注册驱动
Connection conn = DriverManager.getConnection(url, name, password);
/* try {
conn = null;
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
System.out.println(name);
连接(协议+访问数据库 用户名 密码)*/
//和数据库打交道,必须用此
/* } catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}*/
return conn;
}
/*
* 释放资源
* */
public static void release(Connection conn, Statement st, ResultSet rs){
closeSt(st);
closeConn(conn);
closeRs(rs);
}
private static void closeRs(ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void closeSt(Statement st){
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void closeConn(Connection conn){
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}