首页>代码>利用java线程实现数据上传demo>/ThreadTest/src/com/test/jdbc/BaseDao.java
package com.test.jdbc;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;



public class BaseDao {
	private   String driver="";
	private   String url = "";
	private   String user="";
	private   String pwd="";
	/*private static TestJdbc instance=null;
	private TestJdbc(){
		try {
			Class.forName(b.getDriver());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	

	public static TestJdbc getInstance(){
		if(null==instance){
			instance = new TestJdbc();
		}
		return instance;
	}
	*/
	public BaseDao(String driver, String url, String user, String pwd) {
		super();
		this.driver = driver;
		this.url = url;
		this.user = user;
		this.pwd = pwd;
		try {
			Class.forName(this.driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Connection getConnection(){
		Connection conn=null;
		try {
			conn = DriverManager.getConnection(url,user,pwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	

	public <T> List<T> queryAll(Class<T> clazz,String sql,Object[] param){
		List<T> lists = new ArrayList<T>();
		Connection conn = this.getConnection();
		//System.out.println("connection is success...");
		PreparedStatement pstmt =null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			if(null!=pstmt){
				for (int i = 0; i < param.length; i++) {
					pstmt.setObject(i+1, param[i]);
				}
			}
			rs = pstmt.executeQuery();
			while(rs.next()){
				T obj = clazz.newInstance();
				Field[] fields = clazz.getDeclaredFields();
				for (int i = 0; i < fields.length; i++) {
					Field field = fields[i];
					String fieldName = field.getName();
					String setMethodName = "set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
					Method setMethod = clazz.getDeclaredMethod(setMethodName, field.getType());
					String fieldTypeName = field.getType().getName();
					if("int".equals(fieldTypeName)){
						setMethod.invoke(obj, rs.getInt(fieldName));
					}else if("java.lang.Integer".equals(fieldTypeName)){
						setMethod.invoke(obj, rs.getInt(fieldName));
					}else if("java.lang.Double".equals(fieldTypeName)){
						setMethod.invoke(obj, rs.getDouble(fieldName));
					}else if("double".equals(fieldTypeName)){
						setMethod.invoke(obj, rs.getDouble(fieldName));
					}else if("java.lang.String".equals(fieldTypeName)){
						setMethod.invoke(obj, rs.getString(fieldName));
					}else{
						setMethod.invoke(obj, rs.getObject(fieldName));
					}
				}
				lists.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstmt, rs);
		}
		return lists;
	}
	public int insertSql(String sql,Object[] param) throws SQLException{
		int count = 0;
		Connection conn = this.getConnection();
		//System.out.println("insert connnection success....");
		PreparedStatement pstmt = null;
				pstmt = conn.prepareStatement(sql);
				if(null!=pstmt){
					for (int i = 0; i < param.length; i++) {
						pstmt.setObject(i+1, param[i]);
					}
				}
				count = pstmt.executeUpdate();
				this.closeAll(conn, pstmt, null);
			
		return count;
	}
	public boolean addSql(String sql,Object[] param){
		boolean flag = false;
		Connection conn = this.getConnection();
		//System.out.println("add connection success....");
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			if(null!=pstmt){
				for (int i = 0; i < param.length; i++) {
					if(param[i] instanceof Integer){
						pstmt.setInt(i+1, (Integer)param[i]);
					}else if(param[i] instanceof String){
						pstmt.setString(i+1, (String)param[i]);
					}else if(param[i] instanceof Double){
						pstmt.setDouble(i+1, (Double)param[i]);
					}
				}
			}
			flag = pstmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstmt, null);
		}
		return flag;
	}
	public ResultSet getResult(String sql,Object[] param){
		Connection conn = this.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			if(null!=pstmt){
				for (int i = 0; i < param.length; i++) {
					pstmt.setObject(i+1, param[i]);
				}
			}
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(conn, pstmt, rs);
		}
		return rs;
	}
	public void closeAll(Connection conn,Statement st,ResultSet rs){
		if(null!=conn){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(null!=st){
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(null!=rs){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
最近下载更多
newbag  LV9 2021年8月9日
wsfdwr  LV10 2019年7月10日
liuyishi  LV6 2019年4月6日
aaayyy1437  LV2 2019年3月8日
加加活宝  LV2 2018年8月1日
橙cheng001  LV8 2018年6月29日
ldjsaflj  LV1 2018年6月20日
lipiao161  LV16 2018年3月29日
wgc_jy  LV21 2018年1月5日
fxhh5057  LV13 2017年5月12日
最近浏览更多
爱情戴罪的羔羊  LV7 4月2日
1529860026  LV24 2023年6月1日
heqian  LV16 2023年1月10日
微信网友_6040315240812544  LV8 2022年10月20日
mylzdy  LV12 2022年5月12日
3199625134  LV10 2022年4月20日
liju1014 2022年4月5日
暂无贡献等级
xiaoding1999  LV7 2022年4月1日
hai77dong  LV3 2021年11月8日
随便取个名字_哈哈  LV27 2021年11月7日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友