制作简单的JdbcEasyUseUtils
package jdbc;
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;
public class JdbcEasyUseUtils {
public static void update(String sql, Object... args){
Connection connection = null;
PreparedStatement ps = null;
try {
connection = JdbcEasyUseUtils.getConnection();
ps = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1, args[i]);
}
ps.execute();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JdbcEasyUseUtils.close(connection, ps);
}
}
public static <T> List<T> queryMore(Class<T> clazz, String sql, Object... args) {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = JdbcEasyUseUtils.getConnection();
ps = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList<T> list = new ArrayList<>();
while (rs.next()) {
T t = clazz.newInstance();
for (int i = 0; i < columnCount; i++) {
Object value = rs.getObject(i + 1);
String columnName = metaData.getColumnLabel(i + 1);
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, value);
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcEasyUseUtils.close(connection, ps, rs);
}
return null;
}
public static <T> T queryOne(Class<T> clazz, String sql, Object... args) {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = JdbcEasyUseUtils.getConnection();
ps = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
if (rs.next()) {
T t = clazz.newInstance();
for (int i = 0; i < columnCount; i++) {
Object value = rs.getObject(i + 1);
String columnName = metaData.getColumnLabel(i + 1);
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, value);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcEasyUseUtils.close(connection, ps, rs);
}
return null;
}
public static Connection getConnection() {
Connection connection = null;
try {
InputStream resource = JdbcPreparedStatementTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(resource);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driverClassName = properties.getProperty("driverClassName");
Class.forName(driverClassName);
connection = DriverManager.getConnection(url, username, password);
} catch (IOException | ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection, Statement statement) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}