package lss.util;
/**
- @author 疾风无名
- Object Test
- 数据库连接
- 关闭数据对象
- 实现更新查询操作
*/
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0Util {
public static Connection conn=null;
public static DataSource dSource=null;
//获得数据源
static{
dSource=new ComboPooledDataSource("datas");
}
/**
*@return conn;
*/
//获得连接对象
public static Connection getConnection(){
try{
conn=dSource.getConnection();
}catch (Exception e) {
e.printStackTrace();
System.err.println("连接数据库失败");
}
return conn;
}
//关闭连接数据库
public static void closeDB(ResultSet rSet,PreparedStatement psmt,Connection conn){
try{//依次关闭rSet\pSmt\conn
if(rSet!=null){
rSet.close();
}
if(psmt!=null){
psmt.close();
}
if(conn!=null){
conn.close();
}
}catch (Exception e) {
e.printStackTrace();
System.err.println("关闭数据库失败");
}
}
//查询操作
public static ResultSet dbQuery(Connection conn ,String sql,Object ...obj){
ResultSet rs=null;
PreparedStatement pstmt=null;
try{
pstmt=conn.prepareStatement(sql);
for(int i=0;i<obj.length;i++){
pstmt.setObject(i+1, obj[i]);
}
rs=pstmt.executeQuery();
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
//更新操作
public static int dbUpdate(Connection conn,String sql,Object...obj){
PreparedStatement pstmt=null;
int k=0;
try{
//把SQL语句发送给数据库
pstmt=conn.prepareStatement(sql);
//给占位符设置值
for(int i=0;i<obj.length;i++){
pstmt.setObject(i+1, obj[i]);
}
k=pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
closeDB(null, pstmt, conn);
return k;
}
/**************增加数据集合通用方法**********************/
//转换大小写
public static String toUpperOne(String str){
str= str.substring(0,1).toUpperCase()+str.substring(1);
return str;
}
//实现通用底层查询注入java bean 方法
//查询简便方法
public static <T> T queryGS(Class<T> clazz,String sql,Object ...obj){
List<T> result = getForList(clazz, sql, obj);
if(result.size() > 0){
return result.get(0);
}
return null;
}
public static<T> List<T> getForList(Class<T> clazz,String sql,Object ...obj){
List<T> list=new ArrayList<>();
Connection conn=null;
PreparedStatement prstmt=null;
ResultSet rs=null;
try {
conn=getConnection();
prstmt=conn.prepareStatement(sql);
for(int i=0;i<obj.length;i++){
prstmt.setObject(i+1, obj[i]);
}
rs=prstmt.executeQuery();
List<Map<String,Object>> values=new ArrayList<>();
ResultSetMetaData rsmd=rs.getMetaData();
Map<String,Object> map=null;
while(rs.next()){
map=new HashMap<>();
for(int i=0;i<rsmd.getColumnCount();i++){
String columnLabel = rsmd.getColumnLabel(i+1);
Object value=rs.getObject(i+1);
map.put(columnLabel, value);
}
values.add(map);
}
T bean=null;
if(values.size()>0){
for(Map<String,Object> m:values){
bean=clazz.newInstance();
for(Map.Entry<String , Object> entry:m.entrySet()){
String propertyName=entry.getKey().toLowerCase();
if("r".equals(propertyName))continue;
Object value=entry.getValue();
Field field =clazz.getDeclaredField(propertyName);
field.setAccessible(true);
field.set(bean, value);
}
list.add(bean);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally{
closeDB(rs,prstmt , conn);
}
return list;
}
public static<E> E getForValue(String sql, Object... args) {
List<E> list= getValueList(sql,args);
if(list.size()>0)
return list.get(0);
return null;
}
@SuppressWarnings("unchecked")
public static<E> List<E> getValueList(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<E> list =new ArrayList<E>();
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
list.add((E)resultSet.getObject(1));
}
} catch(Exception ex){
ex.printStackTrace();
} finally{
closeDB(resultSet, preparedStatement, connection);
}
return list;
}
public static <T> int update(T bean){
@SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) bean.getClass();
int k=0;
StringBuffer sql=new StringBuffer();
sql.append("update "+clazz.getName()+" set ");
try{
Field[] fields = clazz.getDeclaredFields();
for(int i=0;i<fields.length;i++){
String name= fields[i].getName();
Object value=fields[i].get(name);
sql.append(name+"="+value+" ");
System.out.println(name+":"+value);
}
}catch(Exception e){
e.printStackTrace();
}finally {
closeDB(null, null, conn);
}
return k;
}
}
作者技术博客:
https://lsson.xyz/myblogs
如需联系欢迎加q群:
855473670
481557406