多表通用的增删改查语句

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();
		
	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值