今天我们来写一个JDBC工具类,方便我们以后项目中使用!
1、使用配置文件方、DBCP连接池方式连接数据库
1、导入DBCP和Mysql的有关jar包
- commons-dbcp2-2.5.0.jar
- commons-logging-1.2.jar
- commons-pool2-2.4.2.jar
- mysql-connector-java-5.1.7-bin(1).jar
- mysql.dbcp.properties
########DBCP配置文件##########
#非自动提交
defaultAutoCommit=false
#驱动名
driverClassName=com.mysql.jdbc.Driver
#url
url=jdbc:mysql://127.0.0.1:3306/store
#用户名
username=root
#密码
password=6831245
#初始连接数
initialSize=1
#最大活跃数
maxTotal=30
#最大空闲数
maxIdle=10
#最小空闲数
minIdle=1
#最长等待时间(毫秒)
maxWaitMillis=5000
#程序中的连接不使用后是否被连接池回收(该版本要使用removeAbandonedOnMaintenance和removeAbandonedOnBorrow)
#removeAbandoned=true
removeAbandonedOnMaintenance=true
removeAbandonedOnBorrow=true
#连接在所指定的秒数内未使用才会被删除(秒)
removeAbandonedTimeout=5
#连接代码
#Properties ps = new Properties();
#使用类加载器在项目的src目录下加载指定文件名的文件,来封装输入流
#InputStream fis =ConnUtil.class.getClassLoader().getResourceAsStream("dbcp.properties");
#也可以使用文件输入流加载项目路径下的文件,封装为文件输入流对象键盘输入对象
#FileInputStream fis = new FileInputStream("项目下文件路径/mysql.dbcp.properties");
#ps.load(fis);
#DataSource ds = BasicDataSourceFactory.createDataSource(ps);
#conn = ds.getConnection();
方式一:项目新建dbProperties包,引入配置文件,在打jar包时配置文件单独存在,易于修改。使用文件输入流加载配置文件
工具类源码如下:
package com.yueqian.mgremp.util;
/**
* 配置文件方式连接池工具类
* @author LinChi
*
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
public class DBCPProUtil {
private static BasicDataSource bs;
/**
* 配置文件方式连接数据库
* @return
*/
public static Connection getConnection() {
//创建类加载器方式文件输入流对象
FileInputStream fis = null;
if(bs == null) {
//创建属性对象 从属性文件创建属性信息
Properties ps = new Properties();
try {
//将属性文件封装成文件输入流
//可以使用文件输入流加载项目路径下的文件,封装为文件输入流对象键盘输入对象
//让属性文件通过属性对象读取文件输入流中的属性信息
ps.load(fis);
//使用DBCP工厂类创建核心类BasicDataSource,将属性文件对象出入
//自动设置属性信息配置数据源 (工厂模式)
//注意:属性文件中的key大小写是固定的,利用反射自动获取key值
bs = BasicDataSourceFactory.createDataSource(ps);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
return bs.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 关闭连接池对象
*/
public static void closeDbcp() {
if(bs != null) {
try {
bs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 关闭数据库
* @param conn 连接
* @param st 处理sql对象
* @param rs 查询结果集
*/
public static void closeAll(Connection conn,Statement st,ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st != null) {
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
方式二:在src下存放properties配置文件,打jar包可以同配置文件一起打入项目中,不易修改。使用类加载器加载配置文件
代码如下:
package com.yueqian.mgremp.util;
/**
* 配置文件方式连接池工具类
* @author LinChi
*
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
public class DBCPProUtil {
private static BasicDataSource bs;
/**
* 配置文件方式连接数据库
* @return
*/
public static Connection getConnection() {
//创建类加载器方式文件输入流对象
InputStream is = null;
if(bs == null) {
//创建属性对象 从属性文件创建属性信息
Properties ps = new Properties();
try {
//也可以使用类加载器在项目的src目录下加载指定文件名的文件,来封装输入流
//注意:使用类加载机制记载配置文件,配置文件必须为src目录下
is = DBCPProUtil.class.getClassLoader().getResourceAsStream("mysql.dbcp.properties");
//让属性文件通过属性对象读取文件输入流中的属性信息
ps.load(is);
//使用DBCP工厂类创建核心类BasicDataSource,将属性文件对象出入
//自动设置属性信息配置数据源 (工厂模式)
//注意:属性文件中的key大小写是固定的,利用反射自动获取key值
bs = BasicDataSourceFactory.createDataSource(ps);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
return bs.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 关闭连接池对象
*/
public static void closeDbcp() {
if(bs != null) {
try {
bs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 关闭数据库
* @param conn 连接
* @param st 处理sql对象
* @param rs 查询结果集
*/
public static void closeAll(Connection conn,Statement st,ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st != null) {
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2、使用DBCP连接数据库(不使用配置文件方式)
代码如下:
package com.yueqian.dbcp2;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp2.BasicDataSource;
/**
* DBCP连接池工具类
* @author LinChi
*
*/
public class DBCPUtil2 {
private static BasicDataSource bs;
/**
* 建立连接
* @return
*/
public static Connection getConnection(){
if (bs == null) {
// 创建dbcp核心类
bs = new BasicDataSource();
// 设置基础连接属性
// 设置连接驱动类
bs.setDriverClassName("com.mysql.jdbc.Driver");
// 设置连接url
bs.setUrl("jdbc:mysql://localhost:3306/scott");
// 设置用户名
bs.setUsername("root");
// 设置密码
bs.setPassword("6831245");
// 设定连接除信息
// 设置数据库最大连接数
bs.setMaxTotal(20);
// 设置最大空闲连接数
bs.setMaxIdle(1);
// 设定超时连接:5秒 如果设定为-1则不超时
bs.setMaxWaitMillis(5000);
}
try {
//返回连接对象
return bs.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 关闭连接池
*/
public static void closeBS() {
if(bs != null) {
try {
bs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3、传统方式JDBC工具类(不适用配置文件方式)
代码如下:
package com.yueqian.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
/**
* JDBC连接
* 1- 加载数据库驱动入口类
* 2- 建立连接对象
* 3- 建立一个可执行sql的 Statement对象
* 4- 执行sql返回一个结果集合封装到 ResultSet对象
* 5- 解析set中的数据
* 6- 关闭通道
* @author LinChi
*
*/
public class TestJdbc {
//2- 创建连接对象
private static Connection conn = null;
//3-建立一个可执行sql的statement对象
private static Statement st = null;
//4-执行sql,返回一个结果集对象Resultset
private static ResultSet set = null;
public static void main(String[] args) {
try {
//1- 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/scott";
String username = "root";
String password = "6831245";
//获得连接
conn = DriverManager.getConnection(url, username, password);
//测试连接
// System.out.println(conn);
st = conn.createStatement();
set = st.executeQuery("select empno,ename,sal,hiredate from emp");
//5- 解析sql
//set.next() 使调用行指针到具体的行数据,如果到了行末尾,则返回felse
while(set.next()) {
//获取数据表中不同的列值
//可以根据列名获取列数据,也可以根据行号获取列数据(行号从1开始)
int empno = set.getInt("empno");
String empName = set.getString("ename");
//根据行号获取列数据
double sal = set.getDouble(3);
Date date = set.getDate(4);
System.out.println(empno+"\t"+empName+"\t"+sal+"\t"+date.toLocaleString());
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
//6- 关闭通道
if(set != null) {
set.close();
}
if(st != null) {
st.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4、常用DML操作实例
代码如下:
package com.yueqian.mgremp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.yueqian.mgremp.entity.DeptEntity;
import com.yueqian.mgremp.entity.EmpEntity;
import com.yueqian.mgremp.util.DBCPProUtil;
public class EmpDao {
/**
* 查询单个员工信息
*/
public EmpEntity getEmpByEmpNo(int empNo){
String sql = "SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,"
+ "SAL,COMM,E.DEPTNO,DNAME,Loc\r\n" +
"FROM\r\n" +
" emp e,dept d\r\n" +
"WHERE e.DEPTNO = d.DEPTNO AND EMPNO = ?";
//连接数据库
Connection conn = DBCPProUtil.getConnection();
if(conn == null) {
System.out.println("ERROR: create connection failed!");
//返回空集合
return null;
}
EmpEntity emp = null;
//创建Preparement对象
PreparedStatement ps = null;
//创建查询结果集对象
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, empNo);
rs = ps.executeQuery();
if(rs.next()) {
DeptEntity dept = new DeptEntity();
dept.setDeptno(rs.getInt(8));
dept.setDname(rs.getString(9));
dept.setLoc(rs.getString(10));
emp = new EmpEntity();
emp.setEmpno(rs.getInt(1));
emp.setEname(rs.getString(2));
emp.setJob(rs.getString(3));
emp.setMgr(rs.getInt(4));
emp.setHiredate(rs.getDate(5));
emp.setSal(rs.getInt(6));
emp.setComm(rs.getInt(7));
emp.setDept(dept);
}
//手动提交事务
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBCPProUtil.closeAll(conn, ps, rs);
}
return emp;
}
/**
* 查询条件查询员工信息
*/
public List<EmpEntity> getEmps(Map<Integer,String> connMap){
String sql = "SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,"
+ "SAL,COMM,E.DEPTNO,DNAME,Loc\r\n" +
"FROM\r\n" +
" emp e,dept d\r\n" +
"WHERE e.DEPTNO = d.DEPTNO";
String con = "";
if(connMap != null && connMap.size()>0) {
//按照员工编号查
if(connMap.get(0) != null) {
con = " and EMPNO = "+connMap.get(0);
}
//按照员工姓名查
if(connMap.get(1) != null) {
con = " and ENAME like'%"+connMap.get(1).trim()+"%'";
}
//按照员工工作查
if(connMap.get(2) != null) {
con = " and JOB like'%"+connMap.get(2).trim()+"%'";
}
//按照员工部门查
if(connMap.get(3) != null) {
con = " and DNAME like'%"+connMap.get(3).trim()+"%'";
}
sql+=con;
}
//连接数据库
Connection conn = DBCPProUtil.getConnection();
if(conn == null) {
System.out.println("ERROR: create connection failed!");
//返回空集合
return Collections.EMPTY_LIST;
}
//创建List集合
ArrayList<EmpEntity> list = new ArrayList<EmpEntity>();
//创建Preparement对象
PreparedStatement ps = null;
//创建查询结果集对象
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
DeptEntity dept = new DeptEntity();
dept.setDeptno(rs.getInt(8));
dept.setDname(rs.getString(9));
dept.setLoc(rs.getString(10));
EmpEntity emp = new EmpEntity();
emp.setEmpno(rs.getInt(1));
emp.setEname(rs.getString(2));
emp.setJob(rs.getString(3));
emp.setMgr(rs.getInt(4));
emp.setHiredate(rs.getDate(5));
emp.setSal(rs.getInt(6));
emp.setComm(rs.getInt(7));
emp.setDept(dept);
list.add(emp);
}
//手动提交事务
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBCPProUtil.closeAll(conn, ps, rs);
}
return list;
}
/**
* 添加员工
*/
public int insertEmp(EmpEntity emp) {
if(emp == null) {
System.out.println("ERROR: get newEmpNo failed!");
// 返回0
return 0;
}
//获取员工新账号
int newEmpNo = getNewEmpNo();
if(newEmpNo == -1) {
System.out.println("ERROR: get newEmpNo failed!");
// 返回0
return 0;
}
String sql = "INSERT INTO emp(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)"
+ " VALUES(?,?,?,?,?,?,?,?)";
//连接数据库
Connection conn = DBCPProUtil.getConnection();
if (conn == null) {
System.out.println("ERROR: create connection failed!");
// 返回0
return 0;
}
//定义影响的行数
int count = 0;
// 创建Preparement对象
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1,newEmpNo);
ps.setString(2,emp.getEname());
ps.setString(3,emp.getJob());
ps.setInt(4,emp.getMgr());
//使用 java.util.Date. getTime() 将时间转换为毫秒后,再使用 java.sql.Date构造器构造;
ps.setDate(5,new java.sql.Date(emp.getHiredate().getTime()));
ps.setDouble(6, emp.getSal());
ps.setDouble(7, emp.getComm());
ps.setInt(8,emp.getDept().getDeptno());
count = ps.executeUpdate();
// 手动提交事务
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBCPProUtil.closeAll(conn, ps, null);
}
return count;
}
/**
* 删除员工
*/
public int deleteEmpByEmpNo(int empNo) {
if (empNo == -1) {
System.out.println("ERROR: get newEmpNo failed!");
// 返回0
return 0;
}
String sql = "delete from emp where empno = ?";
// 连接数据库
Connection conn = DBCPProUtil.getConnection();
if (conn == null) {
System.out.println("ERROR: create connection failed!");
// 返回0
return 0;
}
// 定义影响的行数
int count = 0;
// 创建Preparement对象
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, empNo);
count = ps.executeUpdate();
// 手动提交事务
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBCPProUtil.closeAll(conn, ps, null);
}
return count;
}
/**
* 修改员工
*/
public int updateEmp(EmpEntity emp) {
if(emp == null) {
System.out.println("ERROR: get newEmpNo failed!");
// 返回0
return 0;
}
//获取员工新账号
int newEmpNo = getNewEmpNo();
if (newEmpNo == -1) {
System.out.println("ERROR: get newEmpNo failed!");
// 返回0
return 0;
}
String sql = "UPDATE EMP SET ENAME = ?,JOB = ?,MGR = ?,HIREDATE = ?,SAL = ?,COMM = ?,DEPTNO = ? WHERE EMPNO = ?";
// 连接数据库
Connection conn = DBCPProUtil.getConnection();
if (conn == null) {
System.out.println("ERROR: create connection failed!");
// 返回0
return 0;
}
// 定义影响的行数
int count = 0;
// 创建Preparement对象
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(8, emp.getEmpno());
ps.setString(1, emp.getEname());
ps.setString(2, emp.getJob());
ps.setInt(3, emp.getMgr());
// 使用 java.util.Date. getTime() 将时间转换为毫秒后,再使用 java.sql.Date构造器构造;
ps.setDate(4, new java.sql.Date(emp.getHiredate().getTime()));
ps.setDouble(5, emp.getSal());
ps.setDouble(6, emp.getComm());
ps.setInt(7, emp.getDept().getDeptno());
count = ps.executeUpdate();
// 手动提交事务
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBCPProUtil.closeAll(conn, ps, null);
}
return count;
}
/**
* 查询所有工作
*/
public List<String> getJobs(){
String sql = "SELECT DISTINCT job FROM emp";
//连接数据库
Connection conn = DBCPProUtil.getConnection();
if(conn == null) {
System.out.println("ERROR: create connection failed!");
//返回空集合
return Collections.EMPTY_LIST;
}
//创建List集合
List<String> jobList = new ArrayList<String>();
//创建Preparement对象
PreparedStatement ps = null;
//创建查询结果集对象
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
jobList.add(rs.getString(1));
}
//手动提交事务
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBCPProUtil.closeAll(conn, ps, rs);
}
return jobList;
}
/**
* 获取员工新账号
*/
public int getNewEmpNo() {
String sql = "SELECT MAX(EMPNO)+1 FROM emp";
//连接数据库
Connection conn = DBCPProUtil.getConnection();
if (conn == null) {
System.out.println("ERROR: create connection failed!");
// 返回空集合
return -1;
}
// 创建Preparement对象
PreparedStatement ps = null;
// 创建查询结果集对象
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
// 手动提交事务
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBCPProUtil.closeAll(conn, ps, rs);
}
return -1;
}
}
今天就整理了JDBC常用的工具类,希望以后可以拿来直接使用。