首页 技术 正文
技术 2022年11月8日
0 收藏 867 点赞 1,412 浏览 4979 个字

实现增删该查的jdbc封装

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;public class DbUtil {
private static String user = null;
private static String password = null;
private static String driver = null;
private static String url = null;
private static Connection conn = null; private static Properties p =null; //单利模式 --懒汉式(双重锁定)保证线程的安全性
public static DbUtil db = null;
private DbUtil(){ }
public static DbUtil getInstance(){
if(db == null){
synchronized(DbUtil.class){
if(db == null){
db = new DbUtil();
}
}
}
return db;
}
//读取配置文件且加载数据库驱动
static{
//实例化一个properties对象用来解析我们的配置文件
p = new Properties();
//通过类加载器来读取我们的配置文件,以字节流的形式读取
InputStream in = DbUtil.class.getClassLoader().getResourceAsStream("/config.properties");
try {
//将配置文件自如到Propreties对象,来进行解析
p.load(in);
//读取配置文件
driver = p.getProperty("driver");
url = p.getProperty("url");
user = p.getProperty("user");
password = p.getProperty("password");
//加载驱动
Class.forName(driver);
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} }
//建立数据库的连接
public Connection getConn(){
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//查询返回List容器
public List<Map<String,Object>> query(String sql,Object...params){
PreparedStatement pst = null;
ResultSet rs = null;
try {
//获得连接
conn = getConn();
//获得preparedSttement对象进行预编译(?占位符)
pst = conn.prepareStatement(sql);
int paramsIndex = 1;
for(Object p : params){
pst.setObject(paramsIndex++, p);
}
//执行sql语句获得结果集的对象
rs = pst.executeQuery();
//获得结果集中列的信息
ResultSetMetaData rst = rs.getMetaData();
//获得结果集的列的数量
int column = rst.getColumnCount();
//创建List容器
List<Map<String,Object>> rstList = new ArrayList<Map<String,Object>>();
//处理结果
while(rs.next()){
//创建Map容器存取每一列对应的值
Map<String,Object> m = new HashMap<String,Object>();
for(int i=1;i<=column;i++){
m.put(rst.getColumnName(i), rs.getObject(i));
}
//将Map容器放入List容器中
rstList.add(m);
}
return rstList;
} catch (SQLException e) {
e.printStackTrace();
return null;
}finally{
//关闭资源
close(rs, pst, conn);
}
} public List<Map<String,Object>> query(String sql,List<Object> params){
PreparedStatement pst = null;
ResultSet rs = null;
try {
//获得连接
conn = getConn();
//获得preparedSttement对象进行预编译(?占位符)
pst = conn.prepareStatement(sql);
int paramsIndex = 1;
for(Object p : params){
pst.setObject(paramsIndex++, p);
}
//执行sql语句获得结果集的对象
rs = pst.executeQuery();
//获得结果集中列的信息
ResultSetMetaData rst = rs.getMetaData();
//获得结果集的列的数量
int column = rst.getColumnCount();
//创建List容器
List<Map<String,Object>> rstList = new ArrayList<Map<String,Object>>();
//处理结果
while(rs.next()){
//创建Map容器存取每一列对应的值
Map<String,Object> m = new HashMap<String,Object>();
for(int i=1;i<=column;i++){
m.put(rst.getColumnName(i), rs.getObject(i));
}
//将Map容器放入List容器中
rstList.add(m);
}
return rstList;
} catch (SQLException e) {
e.printStackTrace();
return null;
}finally{
//关闭资源
close(rs, pst, conn);
}
} //分页查询总共有多少条记录totleSize
public long queryLong(String sql,Object...params){
PreparedStatement pst = null;
ResultSet rs = null;
try {
//获得连接
conn = getConn();
//获得preparedSttement对象进行预编译(?占位符)
pst = conn.prepareStatement(sql);
int paramsIndex = 1;
for(Object p : params){
pst.setObject(paramsIndex++, p);
}
//执行sql语句获得结果集的对象
rs = pst.executeQuery();
while(rs.next()){
return Long.valueOf(rs.getLong(1));
}
return 0;
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
//插入
public boolean insert(String sql,Object...params){
PreparedStatement pst = null;
ResultSet rs = null;
try {
//获得连接
conn = getConn();
//获得PrepareStatement对象进行预编译
pst = conn.prepareStatement(sql);
//处理将数据插入占位符
int paramsIndex = 1;
for(Object p : params){
pst.setObject(paramsIndex++, p);
}
//执行sql语句
pst.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally{
//关闭资源
close(null, pst, conn);
}
} //修改
public boolean update(String sql,Object...params){
PreparedStatement pst = null;
ResultSet rs = null;
try {
//获得连接
conn = getConn();
//获得PrepareStatement对象进行预编译
pst = conn.prepareStatement(sql);
//处理将数据插入占位符
int paramsIndex = 1;
for(Object p : params){
pst.setObject(paramsIndex++, p);
}
//执行sql语句
pst.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally{
//关闭资源
close(null, pst, conn);
}
} //删除
public boolean delete(String sql,Object...params){
PreparedStatement pst = null;
ResultSet rs = null;
try {
//获得连接
conn = getConn();
//获得PrepareStatement对象进行预编译
pst = conn.prepareStatement(sql);
//处理将数据插入占位符
int paramsIndex = 1;
for(Object p : params){
pst.setObject(paramsIndex++, p);
}
//执行sql语句
pst.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally{
//关闭资源
close(null, pst, conn);
}
}
//关闭资源
public static void close(ResultSet rs,PreparedStatement pst,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
pst = null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}

配置文件config.properties:

user=root
password=root
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/cp_data
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,951
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,477
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,290
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,107
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,739
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,773