JDBC学习笔记(DAO)二

DAO:Data Access Object

public class DAO{

  public void update(String sql,Object ... args){
   Connection connection=null;
   PreparedStatement preparedStatement=null;
   
   try{
   connection = JDBCTools.getConnection();
   preparedStatement =connection.preparedStatement(sql);
   
   for(int i=0;i<args.length;i++){
   preparedStatement.setObject(i+1,args[i]);
   }
   preparedStatement.executeUpdate();
    }catch(Exception e){
	e.printStackTrace();
	}finally{
	JDBCTools.releaseDB(null,preparedStatement,connection);
	}
  }
  
  //查询一条记录
  public <T> T get(Class<T> clazz,String sql,Object ... args){
 
   T entity=null; //泛型
   Connection connection=null;
   PreparedStatement preparedStatement=null;
   ResultSet resultSet =null;
   
   try{
   connection = JDBCTools.getConnection();
   preparedStatement =connection.preparedStatement(sql);
   
   for(int i=0;i<args.length;i++){
   preparedStatement.setObject(i+1,args[i]);
   }
   resultSet=preparedStatement.executeQuery();
   
   //Map用来存放记录
   if(resultSet.next()){
    Map<String,Object> values =new HashMap<String,Object>();
	resultSetMetadata rsmd =resultSet.getMetaData();
	
	//查询的记录有多少列
	int columnCount =rsmd.getColumnCount();
	
	for(int i=0;i<columnCount;i++){
	Srting columnLabel=rsmd.getColumnLabel(i+1);
	Object columnValue=resultSet.getObject(i+1);
	
	//把值添加到map对应的对象中
	values.put(columnLabel,columnValue);
	}
	
	//利用反射创建对象
	Object object=clazz.newInstance();
	//遍历Map集合,给对象的属性赋值
	for(Map.Entry<String,Object> entry: values.entrySet){
	 String propertyName = entry.getKey();
	 Object value = entry.getValue();
	 
	 ReflectionUtils.setFieldValue(object,propertyName,value);//此处建议使用beanutils
	}
   }
    }catch(Exception e){
	e.printStackTrace();
	}finally{
	JDBCTools.releaseDB(null,preparedStatement,connection);
	}
	return entity;
  }
  
  //查询多条记录,返回对应的对象的集合
  public <T> List<T> getForList(Class<T> clazz,String sql,Object ... args){
   
   List<T> list =new ArrayList<>();
   
   Connection connection=null;
   PreparedStatement preparedStatement=null;
   ResultSet resultSet =null;
   
   try{
   connection = JDBCTools.getConnection();
   preparedStatement =connection.preparedStatement(sql);
   
   for(int i=0;i<args.length;i++){
   preparedStatement.setObject(i+1,args[i]);
   }
   resultSet=preparedStatement.executeQuery();
   
    //多条记录用List来装,每条都是 Map类型.
	List<Map<String,Object>> values =new ArrayList<>();
	resultSetMetadata rsmd =resultSet.getMetaData();
	
	 Map<String,Object> map =null;
	
   //Map用来存放记录
   while(resultSet.next()){
    map =new HashMap<>();
	
	//查询的记录有多少列
	int columnCount =rsmd.getColumnCount();
	
	for(int i=0;i<columnCount;i++){
	Srting columnLabel=rsmd.getColumnLabel(i+1);
	Object columnValue=resultSet.getObject(i+1);
	
	//把值添加到map对应的对象中
	map.put(columnLabel,columnValue);
	}
	values.add(map)
	}
	 
	Object bean = null;
	if(values.size()>0){
	for(Map<String,Object> m: values){
	 for(Map.entry<String,Object> entry : m.entrySet){
	 String propertyName = entry.getKey();
	 Object value = entry.getValue();
	 
	  bean = clazz.newInstance;
	  Bean.setProperty(bean.propertyName,value);
	 }
	 list.add(bean);
	}
	}
    }catch(Exception e){
	e.printStackTrace();
	}finally{
	JDBCTools.releaseDB(null,preparedStatement,connection);
	}
	return list;
  }
  
  //返回某条记录的某一个字段的值或者一个统计的值
  
  

}

猜你喜欢

转载自blog.csdn.net/qq_31667705/article/details/81813025