JDBC工具类
最近学了JDBC,使用的时候只要写好sql语句和要使用的数据就能返回结果 感觉非常好用
public class JDBCUtil {
static Properties properties = new Properties();
static {
InputStream inputStream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(inputStream);
Class.forName(properties.getProperty("driverClass"));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(properties.getProperty("url"),properties.getProperty("username"),properties.getProperty("password"));
}
public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn){
try {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
* @description:增删改
* @param sql
* @param params
* @return: int
* @author: dodo
* @time: 2022/9/8 0:04
*/
public static int executeUpdate(String sql,Object... params){
Connection conn = null;
PreparedStatement preparedStatement = null;
try {
conn = JDBCUtil.getConnection();
preparedStatement = conn.prepareStatement(sql);
if(params != null && params.length > 0){
for (int i = 0 ; i <params.length;i++) {
preparedStatement.setObject(i+1,params[i]);
}
}
return preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.close(null,preparedStatement,conn);
}
JDBCUtil.close(null,preparedStatement,conn);
return 0;
}
/*
* @description:多个结果
* @param rs
* @param clazz
* @return: java.util.List<T>
* @author: dodo
* @time: 2022/9/8 0:38
*/
public static <T> List<T> parseResultSet(ResultSet rs, Class<T> clazz) throws SQLException, IllegalAccessException, InstantiationException {
List<T> list = new ArrayList<>();
if(rs == null){
return null;
}
while(rs.next()){
//反射
T t = clazz.newInstance();
//获取属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
//约定: 属性名与列名一样: 使用别名
field.set(t,rs.getObject(field.getName()));
//找set方法赋值
}
list.add(t);
}
return list;
}
/*
* @description: 单个结果
* @param rs
* @param clazz
* @return: java.lang.Object
* @author: dodo
* @time: 2022/9/8 9:14
*/
public static <T> Object parseResultSetByOne(ResultSet rs, Class<T> clazz) throws SQLException, IllegalAccessException, InstantiationException {
if(rs == null){
return null;
}
T t = null;
while(rs.next()){
//反射
t = clazz.newInstance();
//获取属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
//约定: 属性名与列名一样: 使用别名
field.set(t,rs.getObject(field.getName()));
//找set方法赋值
}
}
JDBCUtil.close(rs,null,null);
return t;
}
/*
* @description: 查询
* @param sql
* @param params
* @return: java.sql.ResultSet
* @author: dodo
* @time: 2022/9/8 9:14
*/
public static ResultSet executeQuery(String sql,Object... params) {
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConnection();
preparedStatement = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
}
rs = preparedStatement.executeQuery();
return rs;
} catch (SQLException e) {
e.printStackTrace();
}finally {
// JDBCUtil.close(null,preparedStatement,conn);
}
return null;
}
}