statement写通用的update方法、通用的查询方法(获取一条或多条)

public class TestJDBC {
 @Test
 public void test3(){
//  String sql = "select id, name, email, birth from customers where id <10";
//  List<Customer> list = getForList(sql, Customer.class);
//  for(Customer customer : list){
//   System.out.println(customer);
//  }
  String sql1 = "select order_id id, order_name name, order_date date from `order` where order_id <3";
  List<Order> list1 = getForList(sql1, Order.class);
  for(Order order : list1){
   System.out.println(order);
  }
 }
 
 
 // 通用的查询方法,返回多个对象的集合(version 1.0)
 public <T> List<T> getForList(String sql, Class<T> clazz){
  Connection connection = null;
  Statement s = null;
  ResultSet rs = null;
  try {
   connection = JDBCUtils.getConnection();
   s = connection.createStatement();
   rs = s.executeQuery(sql);
   ResultSetMetaData rsmd = rs.getMetaData();
   //获取列的个数
   int count = rsmd.getColumnCount();
   List<T> list = new ArrayList<T>();
   while(rs.next()){
    T t = clazz.newInstance();
    for(int i=0;i<count; i++){
     Object columnValue = rs.getObject(i+1);// 获取列值
     // getColumnLabel():获取列的别名,此别名由java类的属性名决定
     String columnLable = rsmd.getColumnLabel(i+1);
     PropertyUtils.setProperty(t, columnLable, columnValue);    
    }
    list.add(t);
   }
   return list;
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   JDBCUtils.close(rs, s, connection);   
  }
  return null;
 }
 
 @Test
 public void test2(){
  String sql = "select id, name, email, birth from customers where id = 22;";
   Customer customer = getInstance(sql,Customer.class);
   System.out.println(customer);
 }
 // 通用的查询方法,返回一个对象(version 1.0)
 public <T>T getInstance(String sql, Class<T> clazz){
  Connection connection = null;
  Statement s = null;
  ResultSet rs = null;
  try {
   connection = JDBCUtils.getConnection();
   s = connection.createStatement();
   rs = s.executeQuery(sql);
   ResultSetMetaData rsmd = rs.getMetaData();
   // getColumnCount():获取结果集的列数
   int count = rsmd.getColumnCount();
   while(rs.next()){
    T t = clazz.newInstance();
    for(int i=0; i<count; i++){
     // 获取列值
     Object columnValue = rs.getObject(i + 1);
     // getColumnLabel():获取列的别名,此别名由java类的属性名决定
     String columnLable = rsmd.getColumnLabel(i + 1);
     PropertyUtils.setProperty(t, columnLable, columnValue);
    }
    return t;
   }
  }  catch (Exception e) {
   e.printStackTrace();
  }finally{
   JDBCUtils.close(rs, s, connection);   
  }
  return null;
 }
 
 @Test
 public void test(){
  String sql = "insert into customers(name, email, birth) values('shawn', 'shawn@163.com', '1998-09-09')";
  update(sql);
 }
 // 通用的增、删、改的方法(version 1.0)
 public void update(String sql){
  Connection connection = null;
  Statement s = null;
  try {
   connection = JDBCUtils.getConnection();
   s = connection.createStatement();
   s.execute(sql);
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   JDBCUtils.close(null, s, connection);
  }
 }
 @Test
 public void test1() throws Exception{
  Connection connection = JDBCUtils.getConnection();
  System.out.println(connection);
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值