工具类JDBCUtils包括以下方法
项目结构如下
代码如下
package com.lyl.utils;
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;
// 可以将CRUD操作设计为静态方法
// 也可以写成DAO类,让实体类继承CRUD的方法
public class JDBCUtils {
/**
* 获取数据库的一个连接
* 方法中使用JDBCUtils.class.getClassLoader().getResourceAsStream(path),该path是指类路径classPath,
* 即在普通项目中指"src/",在web项目中指"/WEB-INF/classes/"
*
* @param propertiesPath 返回配置文件的路径propertiesPath,该文件存放连接数据库需要的user、password、url、driverClass等相关参数
* @return 到数据库的连接
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConnection(String propertiesPath) throws Exception{
InputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResourceAsStream(propertiesPath);
Properties properties = new Properties();
properties.load(resourceAsStream);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driverClass = properties.getProperty("driverClass");
Class.forName(driverClass);
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
}
/**
* 对于增删改操作,关闭到数据库的connection和statement
* @param conn 到数据库的连接 conn
* @param ps sql对应的statement ps
*/
public static void closeResource(Connection conn, Statement ps) {
try {
// 避免空指针(对象没有创建的时候就关闭)
if (conn != null)
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
// 避免空指针(对象没有创建的时候就关闭)
if (ps != null)
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/**
* 对于查找操作,关闭到数据库的connection、statement和resultSet
* @param conn 到数据库的连接 conn
* @param ps sql对应的statement ps
* @param rs 查询出的结果集 rs
*/
public static void closeResource(Connection conn, Statement ps, ResultSet rs) {
try {
// 避免空指针(对象没有创建的时候就关闭)
if (conn != null)
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
// 避免空指针(对象没有创建的时候就关闭)
if (ps != null)
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
// 避免空指针(对象没有创建的时候就关闭)
if (rs != null) {
rs.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/**
* 输入带占位符的sql和填补占位符的参数,执行增删改操作
* @param sql 带占位符的sql
* @param args 填补占位符的参数 args
* @return 成功操作的行数,出错则返回-1
* @throws SQLException
* @throws IOException
* @throws ClassNotFoundException
*/
public static int update(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
int ans = -1;
try {
connection = JDBCUtils.getConnection("login.properties");
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
ans = preparedStatement.executeUpdate();
return ans;
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.closeResource(connection, preparedStatement);
}
return -1;
}
/**
* 输入实体类的Class对象,带占位符的sql,填补占位符的参数,执行查询操作,返回实体类的List集合
* @param tClass 实体类的Class对象 tClass
* @param sql 带占位符的sql
* @param args 填补占位符的参数 args
* @param <T> 指实体类的类型 <T>
* @return 执行查询操作,返回实体类的List集合,出错则返回null
*/
public static <T> List<T> queryToEntityClass(Class<T> tClass, String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<T> ansList = null;
try {
connection = JDBCUtils.getConnection("login.properties");
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof Integer) {
preparedStatement.setInt(i + 1, (Integer) args[i]);
} else if (args[i] instanceof String) {
preparedStatement.setString(i + 1, (String) args[i]);
} else {
preparedStatement.setObject(i + 1, args[i]);
}
}
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
ansList = new ArrayList<>();
while (resultSet.next()) {
// 创建一个实体类对象
T entityClass = tClass.newInstance();
for (int i = 0; i < columnCount; i++) {
// 从ResultSet中获取属性值
Object fieldValue = resultSet.getObject(i + 1);
// 通过元数据MetaData获得属性名,通过反射获得实体类的属性
String columnName = metaData.getColumnName(i + 1);
Field field = entityClass.getClass().getDeclaredField(columnName);
field.setAccessible(true);
field.set(entityClass, fieldValue);
}
ansList.add(entityClass);
}
return ansList;
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(connection, preparedStatement, resultSet);
}
return null;
}
}
标签:JDBC,args,param,connection,preparedStatement,sql,工具,null From: https://www.cnblogs.com/lylhome/p/17581968.html