package com.tetralogy.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.*; public class DbUtil {
private static DbUtil instance;
// 连接池数据源(即连接池)
DataSource dataSource; public static DbUtil getInstance() {
if (null == instance) {
instance = new DbUtil();
}
return instance;
}
private DbUtil() {
Properties prop = new Properties();
InputStream is = DbUtil.class.getResourceAsStream("/druid.properties");
try {
prop.load(is);
//返回的是DataSource
dataSource = DruidDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
e.printStackTrace();
}
} private Connection getConnection() throws Exception {
Connection conn = dataSource.getConnection();;
return conn;
} public static int Execute(String sql,Object[] object) throws Exception{
Connection con = null;
PreparedStatement pstmt = null;
int count = -1;
try {
DbUtil instance = DbUtil.getInstance();
con = instance.getConnection();
pstmt = con.prepareStatement(sql);
if(object==null ||object.equals("")){ //判断一下 object 是否为空 如果为空直接执行sql语句
count = pstmt.executeUpdate();
}else{
for(int i=0;i<object.length;i++){
pstmt.setObject(i+1,object[i]);
}
count = pstmt.executeUpdate(); //最后执行executeQuery()方法 执行sql语句
}
} catch (Exception e) {
throw e;
}finally {
DbUtil.close(con,pstmt,null);
}
return count;
} public static int executeByParams(String sql, Object...params) throws Exception{
List<Object> list = new ArrayList<>();
if(null!=params){
for(int i=0;i<params.length;i++){
list.add(params[i]);
}
}
return Execute(sql,list.toArray());
} public static List queryForList(String sql, Object[] object) throws Exception {
DbUtil instance = DbUtil.getInstance();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List list = null;
try {
con = instance.getConnection();
pstmt = con.prepareStatement(sql);
if(object==null){
rs = pstmt.executeQuery();
}else{
for(int i=0;i<object.length;i++){
pstmt.setObject(i+1,object[i]);
}
rs = pstmt.executeQuery();
}
//转化
list = DbUtil.rsToList(rs);
} catch (Exception e) {
throw e;
}finally {
DbUtil.close(con,pstmt,rs);
}
return list;
} //T:代表我返回的结果,他是集合里面使用的泛型的结果;他和我们传过来的字节码文件也是一个类型
// class<T> cls :我们实体类的字节码对象
public static <T> List<T> queryForList(Class<T> cls, String sql, Object[] object) throws Exception {
DbUtil instance = DbUtil.getInstance();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List list = null;
try {
con = instance.getConnection();
pstmt = con.prepareStatement(sql);
if(object==null){
rs = pstmt.executeQuery();
}else{
for(int i=0;i<object.length;i++){
pstmt.setObject(i+1,object[i]);
}
rs = pstmt.executeQuery();
}
//转化
list = DbUtil.rsToEntity(cls,rs);
} catch (Exception e) {
throw e;
}finally {
DbUtil.close(con,pstmt,rs);
}
return list;
} public static <T> T queryForEntity(Class<T> cls, String sql, Object[] param) throws Exception {
List<T> list = queryForList(cls,sql,param);
if(list!=null && list.size()>0){
return list.get(0);
}
return null;
} public static String queryForOne(String sql, Object[] object) throws Exception {
DbUtil instance = DbUtil.getInstance();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String result = "";
try {
con = instance.getConnection();
pstmt = con.prepareStatement(sql);
if(object==null){
rs = pstmt.executeQuery();
}else{
for(int i=0;i<object.length;i++){
pstmt.setObject(i+1,object[i]);
}
rs = pstmt.executeQuery();
}
while (rs.next()) {
result = rs.getString(1);
break;
} } catch (Exception e) {
throw e;
}finally {
DbUtil.close(con,pstmt,rs);
}
return result;
} //关闭流原则,先创建后关闭
private static void close(Connection con, PreparedStatement ps, ResultSet rs) throws Exception {
if (ps != null) {
ps.close();
ps = null;
}
if (rs != null) {
rs.close();
rs = null;
}
if (con != null) {
con.close();
con = null;
}
} private static List rsToList(ResultSet rs) throws Exception{
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();//获取键名
int columnCount = md.getColumnCount();//获取行的数量
while (rs.next()) {
Map rowData = new HashMap();//声明Map
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));//获取键名及值
}
list.add(rowData);
}
return list;
} private static <T> List<T> rsToEntity(Class<T> cls, ResultSet rs) throws Exception{
ResultSetMetaData md = rs.getMetaData();//获取键名
int columnCount = md.getColumnCount();//获取行的数量
List<T> list = new ArrayList<>();
while (rs.next()) {
T t = cls.newInstance();
for (int i = 1; i <= columnCount; i++) {
String colName = md.getColumnLabel(i);
Object valueObj = rs.getObject(colName);
Field field = cls.getDeclaredField(colName);
field.setAccessible(true);
field.set(t,valueObj);
}
list.add(t);
}
return list;
}
}

  

最新文章

  1. 阿里的weex框架到底是什么
  2. 兼容好的JS图片上传预览代码
  3. perl 遍历对象数组
  4. winform 获取当前程序运行根目录
  5. 微信的redirect_uri参数错误解决办法
  6. HTTP协议 keep-alive连接 与 BS(firefox-thttpd)实验
  7. Jupyter增加内核
  8. Java 多线程同步的五种方法
  9. wzplayer,tlplayer支持ActiveX
  10. jquery实现定时调度(倒计时)
  11. 设计模式——适配器模式(C++实现)
  12. 《C++ Primer》学习笔记:3.3.3其他vector操作
  13. 正则序RegExp
  14. LInux命令英文全称
  15. C语言的AT指令
  16. linux结束程序内存不会马上释放的解决方法
  17. BZOJ 1878 HH的项链 | 主席树
  18. MySQl中的\g和\G
  19. [移动端WEB] 移动端网站响应式布局之&quot;rem&quot;,CSS3 rem使用方式
  20. android textview空格占位符以及一些其他占位符汇总

热门文章

  1. try catch引发的性能优化深度思考
  2. 【机器学习与R语言】1-机器学习简介
  3. dlang ref的作用
  4. php header下载文件 无法查看原因
  5. Vector总结及部分底层源码分析
  6. hadoop运行jar包报错
  7. Swift-技巧(十一)重写运算符
  8. A Child&#39;s History of England.23
  9. Prompt branches and tab completion
  10. MySQL(4):卸载MySQL