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;
/**
* Description TODO
* Author wanghan
* Date 2020/12/21 16:32
**/
public class DBUtil {
public static String driver;
public static String username;
public static String password;
public static String url;
static {
//准备键值对
Properties properties = new Properties();
//获取文件
InputStream is = DBUtil.class.getResourceAsStream("/jdbc.properties");
//加载流文件进入propertis
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//获取key value
driver = properties.getProperty("jdbc.driver");
url = properties.getProperty("jdbc.url");
username = properties.getProperty("jdbc.username");
password = properties.getProperty("jdbc.password");
//加载驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* @ Description //获取Connection
* @ Author wanghan
* @ Date 17:04 2020/12/21
*/
public static Connection getConnection(){
Connection conn = null;
//创建数据链接
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs, Statement st, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static int excuteUpdate(String sql, Object... params) {
Connection conn = null;
PreparedStatement pstmt = null;
int n = 0;
try {
//获取连接
conn = DBUtil.getConnection();
//获取执行者
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
n = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据源
DBUtil.closeAll(null,pstmt,conn);
}
//返回数据
return n;
}
public static <T>List<T> query(Class<T> clazz,String sql,Object...args) {
ArrayList<T> list = new ArrayList<>();
//连接
Connection conn = null;
//准备preparedstatement
PreparedStatement pstm = null;
//返回集
ResultSet rs = null;
try {
conn = getConnection();
pstm = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pstm.setObject(i+1,args[i]);
}
rs = pstm.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columncount = rsmd.getColumnCount();
//处理结果
while (rs.next()) {
T t = clazz.newInstance();
for (int i = 0; i < columncount; i++) {
//获取列名,即对象属性
String columnlabel = rsmd.getColumnLabel(i + 1);
//根据列名获取反射对象的属性
Field field = clazz.getDeclaredField(columnlabel);
field.setAccessible(true);//属性可见
//本质是设置true时候,反射对象调用时候禁止Java语言访问检查(public和private)
//赋值
field.set(t,rs.getObject(i+1));
}
list.add(t);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} finally {
closeAll(rs,pstm,conn);
}
return list;
}
public static void main(String[] args) {
Connection conn = getConnection();
System.out.println(conn);
}
}