package com.suangti3.util;
import com.suangti3.bean.Order;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* @author 兰晓东
* @create 2022-03-10 21:31
*/
public class JDBCUtils {
/**
* @return
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException
* @Description 获取数据库的连接
* @author lxd
* @date 2022年3月10日下午12:07:34
*/
public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
// 1.读取配置文件中的4个基本信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String dirverClass = pros.getProperty("driverClass");
// 加载驱动
Class.forName(dirverClass);
// 获取连接
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
/**
* @param conn
* @param ps
* @Description 关闭连接和Statement的操作
* @author lxd
* @date 2022年3月10日下午12:11:07
*/
public static void closeResource(Connection conn, Statement ps) {
try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* @param conn
* @param ps
* @param rs
* @Description 关闭资源的操作
* @author lxd
* @date 2022年3月10日下午3:52:14
*/
public static void closeResource(Connection conn, Statement ps, ResultSet rs) {
try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 通用的增删改操作:
public static int update(String sql, Object... args) { // sql中占位符个数与可变性惨的长度相同!
// 获取连接
Connection conn = null;
// 预编译sql语句
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
// 填充占位符
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
// 执行
/*方式一: ps.execute();
* return: 如果执行的是查询操作,有返回结果,则此方法返回true; 如果执行的是增删改操作,没有返回结果,则此方法返回false;
*/
//方式二:
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
JDBCUtils.closeResource(conn, ps);
}
return 0;
}
/**
* 针对于不同的表的通用的查询操作,返回表中的一条记录
*/
public <T> T getInstance(Class<T> clazz, String sql, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = rs.getMetaData();
// 通过ResultSetMetaData获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 给cust对象指定的某个属性,赋值为value
if (rs.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列.
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = rs.getObject(i + 1);
// 获取每个列的列名
// 获取列的列名:getColumnName -不推荐使用
// String columnName = rsmd.getColumnName(i+1);
// 获取列的别名:getColumnbLabel
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给cust对象指定的columnName的属性,赋值为columvalue,通过反射
Field filed = clazz.getDeclaredField(columnLabel);
filed.setAccessible(true);
filed.set(t, columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, rs);
}
return null;
}
/*
针对于不同的表的通用的查询操作,返回表中的多条记录
*/
public <T> List<T> getForList(Class<T> clazz, String sql, Object... args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
// 获取结果集的元数据:ResultSetMetaData
ResultSetMetaData rsmd = rs.getMetaData();
// 通过ResultSetMetaData获取结果集中的列数
int columnCount = rsmd.getColumnCount();
// 创建集合对象
ArrayList<T> list = new ArrayList<>();
// 给cust对象指定的某个属性,赋值为value
while (rs.next()) {
T t = clazz.newInstance();
// 处理结果集一行数据中的每一个列:给t对象指定的属性赋值
for (int i = 0; i < columnCount; i++) {
// 获取列值
Object columnValue = rs.getObject(i + 1);
// 获取每个列的列名
// 获取列的列名:getColumnName -不推荐使用
// String columnName = rsmd.getColumnName(i+1);
// 获取列的别名:getColumnbLabel
String columnLabel = rsmd.getColumnLabel(i + 1);
// 给cust对象指定的columnName的属性,赋值为columvalue,通过反射
Field filed = clazz.getDeclaredField(columnLabel);
filed.setAccessible(true);
filed.set(t, columnValue);
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps, rs);
}
return null;
}
}