package com.alibaba.sql; import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.commons.beanutils.BeanUtils; public class DAO {
public void update(String sql, Object...args){
Connection connection = null;
PreparedStatement preparedStatement = null;
try{
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for(int i = 0; i < args.length; i++){
preparedStatement.setObject(i + 1, args[i]);
}
preparedStatement.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}
finally{
JDBCTools.release(null, preparedStatement, connection);
}
} public <T> T get(Class<T> clazz, String sql, Object...args){
List<T> list = getForList(clazz, sql, args);
if(list != null){
return list.get(0);
}
return null;
} public <T> List<T> getForList(Class<T> clazz, String sql, Object...args){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<T> list = null;
try{
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for(int i = 0; i < args.length; i++){
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
List<String> label = getLabel(resultSet);
List<Map<String, Object>> values = getValues(resultSet, label);
if(values.size() > 0){
list = valueTolist(clazz, values);
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCTools.release(resultSet, preparedStatement, connection);
}
return list;
} private <T> List<T> valueTolist(Class<T> clazz, List<Map<String, Object>> values) throws InstantiationException,
IllegalAccessException, InvocationTargetException {
List<T> list = new ArrayList<T>();
for(Map<String, Object> map:values){
T entity = clazz.newInstance();
for(Map.Entry<String, Object> entry:map.entrySet()){
String fieldName = entry.getKey();
Object fieldValue = entry.getValue();
BeanUtils.setProperty(entity, fieldName, fieldValue);
}
list.add(entity);
}
return list;
} private List<Map<String, Object>> getValues(ResultSet resultSet,
List<String> label) throws SQLException {
List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();
while(resultSet.next()){
Map<String, Object> map = new HashMap<String, Object>();
for(int i = 0; i < label.size(); i++){
String fieldName = label.get(i);
Object fieldValue = resultSet.getObject(fieldName);
map.put(fieldName, fieldValue);
}
values.add(map);
}
return values;
} private List<String> getLabel(ResultSet resultSet) throws SQLException {
ResultSetMetaData rsmd = resultSet.getMetaData();
List<String> label = new ArrayList<String>();
for(int i = 0; i< rsmd.getColumnCount(); i++){
label.add(rsmd.getColumnLabel(i+1));
}
return label;
} @SuppressWarnings("unchecked")
public <E> E getForValue(String sql, Object...args){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
E entity = null;
try{
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for(int i = 0; i < args.length; i++){
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
entity = (E)resultSet.getObject(1);
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCTools.release(resultSet, preparedStatement, connection);
}
return entity;
}
}
获取自动生成(auto-increment)主键
preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ResultSet rs = preparedStatement.getGeneratedKeys(); //仅返回一列, 列名GENERATED_KEYS, 值Id
if(rs.next){
Sysout(rs.getObject(1));
}