下面是该工具类
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BaseDao {
public static int upDate(String sql, List<Object> params){
int row = 0;
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
if(params.size() > 0){
for(int i = 0; i < params.size(); i++){
preparedStatement.setObject(i + 1, params.get(i));
}
}
row = preparedStatement.executeUpdate();
} catch (SQLException throwable) {
throwable.printStackTrace();
} finally {
DBUtil.close(null,preparedStatement,connection);
}
return row;
}
public static Object findSingleValue(String sql,List<Object> params){
Object value = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
try{
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
if(params.size() > 0){
for(int i = 0; i < params.size(); i++){
preparedStatement.setObject(i + 1, params.get(i));
}
}
rs = preparedStatement.executeQuery();
if(rs.next()){
value = rs.getObject(1);
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.close(rs,preparedStatement,connection);
}
return value;
}
public static List queryList(String sql,List<Object> params,Class c){
List list = new ArrayList();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
try{
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
if(params.size() > 0){
for(int i = 0; i < params.size(); i++){
preparedStatement.setObject(i + 1, params.get(i));
}
}
rs = preparedStatement.executeQuery();
Field[] fields = c.getDeclaredFields();
int length = fields.length;
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
Constructor constructor = c.getConstructor();
while(rs.next()){
Object o = constructor.newInstance();
for(int i = 0;i < length; i++){
for(int j = 0;j < columnCount; j++){
String columnName = metaData.getColumnName(j+1);
if(columnName.equals(fields[i].getName())){
fields[i].setAccessible(true);
fields[i].set(o,rs.getObject(columnName));
}
}
}
list.add(o);
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.close(rs,preparedStatement,connection);
}
return list;
}
public static Object querySingle(String sql,List<Object> params,Class c){
Object bean = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
try{
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
if(params.size() > 0){
for(int i = 0; i < params.size(); i++){
preparedStatement.setObject(i + 1, params.get(i));
}
}
rs = preparedStatement.executeQuery();
Field[] fields = c.getDeclaredFields();
int length = fields.length;
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
Constructor constructor = c.getConstructor();
while(rs.next()){
bean = constructor.newInstance();
for(int i = 0;i < length; i++){
for(int j = 0;j < columnCount; j++){
String columnName = metaData.getColumnName(j+1);
if(columnName.equals(fields[i].getName())){
fields[i].setAccessible(true);
fields[i].set(bean,rs.getObject(columnName));
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.close(rs,preparedStatement,connection);
}
return bean;
}
}
下面是上面的工具类中用到的关闭资源,获取数据库链接的操作
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBUtil {
private static Properties properties = new Properties();
static {
try {
InputStream in = DBUtil.class.getResourceAsStream("/db.properties");
properties.load(in);
Class.forName(properties.getProperty("JDBCNAME"));
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection connection = null;
try {
String URL = properties.getProperty("URL");
String USER = properties.getProperty("USER");
String PASSWORD = properties.getProperty("PASSWORD");
connection = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException throwable) {
throwable.printStackTrace();
}
return connection;
}
public static void close(ResultSet rs, PreparedStatement ps, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException throwable) {
throwable.printStackTrace();
}
}
}