JDBC实战教程-优化JDBC配置及代码
在前面的代码中我们把jdbc配置信息(用户名、密码、驱动信息及连接数据库服务的URL)都在方法中声明一个变量进行赋值如下:
public static void main(String[] args){
String url = "jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false";
String user = "root";
String password = "root";
Connection con = DriverManager.getConnection(url, user, password);
String sql = "select * from company where name like ?";
//创建Statement对象
PreparedStatement st = con.prepareStatement(sql);
//执行SQL语句并处理结果集
ResultSet rs = st.executeQuery();
}
其实我们完全可以将数据库的连接配置信息写在一个properties文件中,然后提供一个方法引用properties文件,具体做法如下:
1、在src目录下创建一个名叫jdbc.properties文件,在jdbc.properties文件编写连接数据库需要使用到的数据库驱动,连接URL地址,用户名,密码,如下:
jdbc.driver = com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false
user=root
password=root
2、在java文件中引用jdbc.properties文件,如下:
//获取数据库连接配置信息
public static String getProperties(String key){
Properties prop = new Properties();
try {
prop.load(new FileInputStream("jdbc.properties"));
return prop.getProperty(key);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
3、加载驱动这步操作我们可以提取出来放到一个静态代码块内,这样就可以让类加载的时候就可以加载驱动,加载驱动driver我们就可以使用getProperties()方法,如下:
//加载数据库驱动
static {
try {
Class.forName(getProperties("jdbc.driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
4、获取Connection连接操作也可以提取出一个方法,如下:
//获取Connection连接
public static Connection getConnection(){
String url = getProperties("url");
String user = getProperties("user");
String password = getProperties("password");
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
5、创建Statement对象,执行SQL语句并返回结果集封装如下:
//创建Statement对象并执行SQL语句
public static ResultSet doStatement(String sql){
Connection con = getConnection();
try {
Statement stmt = con.createStatement();
return stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//使用PreparedStatement预处理,来操作数据库
public static ResultSet doPreparedStatement(String sql){
Connection con = getConnection();
try {
PreparedStatement stmt = con.prepareStatement(sql);
return stmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
6、关闭连接操作如下:
//关闭连接操作
public static void close(Connection con, Statement st, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
源码如下:
package com.zhq.jdbc;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil {
public static void main(String[] args) {
String sql = "select * from user";
doStatement(sql);
}
static {
try {
Class.forName(getProperties("jdbc.driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
String url = getProperties("url");
String user = getProperties("user");
String password = getProperties("password");
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static String getProperties(String key) {
Properties prop = new Properties();
try {
prop.load(new FileInputStream("jdbc.properties"));
return prop.getProperty(key);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static ResultSet doStatement(String sql) {
Connection con = getConnection();
try {
Statement stmt = con.createStatement();
return stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static ResultSet doPreparedStatement(String sql) {
Connection con = getConnection();
try {
PreparedStatement stmt = con.prepareStatement(sql);
return stmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(Connection con, Statement st, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}