package ThirdPassage;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import been.Customers;
public class Finally_Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException, SQLException, NoSuchFieldException, SecurityException {
// String sql=" insert into customers (name,email,birth) values(?,?,?)";
// Finally_Test.zsg(sql, "taobao","taobaoQQ.com",new Date(4651626461614L));
String sql="select id,name,email,birth from customers where id<?";
List<Customers> list = Finally_Test.Query_Finally(Customers.class, sql, 45);
list.forEach(System.out::println);
}
//通用的查询操作
public static <T> List<T> Query_Finally(Class<T> clazz,String sql,Object...args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException{
//获取配置信息
InputStream is = Finally_Test.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("user");
String password = prop.getProperty("password");
String url = prop.getProperty("url");
String driver1 = prop.getProperty("driver");
//获取dirver运行类的实例
Class clazz1 = Class.forName(driver1);
Driver driver= (Driver)clazz1.newInstance();
//注册驱动
Connection conn = DriverManager.getConnection(url, user, password);
//预编译
PreparedStatement ps = conn.prepareStatement(sql);
//填充占位符
for(int i=0;i<args.length;i++) {
ps.setObject(i+1, args[i]);
}
//执行sql语句,并获取结果集
ResultSet rs = ps.executeQuery();
//获取结果集的元数据
ResultSetMetaData metaData = rs.getMetaData();
//获取列数
int columnCount = metaData.getColumnCount();
//声明一个列表,存储对象
ArrayList<T> arrayList = new ArrayList<T>();
while(rs.next()) {
T t=clazz.newInstance();
for(int i=0;i<columnCount;i++) {
//获取每一列的列值
Object value = rs.getObject(i+1);
//获取每一列的列名
String columnName = metaData.getColumnName(i+1);
//通过反射获取属性
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(t, value);
}
arrayList.add(t);
}
conn.close();
rs.close();
ps.close();
return arrayList;
}
//数据库增删改通用的操作
public static void zsg(String sql,Object...args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
//读取配置信息
InputStream is = Finally_Test.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties prop = new Properties();
prop.load(is);
String user = prop.getProperty("user");
String password = prop.getProperty("password");
String url = prop.getProperty("url");
String driver1 = prop.getProperty("driver");
//获取driver运行类实例
Class clazz = Class.forName(driver1);
Driver driver= (Driver)clazz.newInstance();
//注册驱动
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
//预编译
PreparedStatement ps = conn.prepareStatement(sql);
//填充占位符
for(int i=0;i<args.length;i++) {
ps.setObject(i+1, args[i]);
}
//执行sql语句
ps.execute();
//关闭资源
ps.close();
conn.close();
}
}
多表通用的增删改查语句
最新推荐文章于 2022-09-13 08:46:26 发布