`
michaels888
  • 浏览: 6574 次
社区版块
存档分类
最新评论

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

    博客分类:
  • JDBC
阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics