日期:2014-05-16  浏览次数:20875 次

java数据库访问通用类;

package org.studentmanager.commons;
/**
?* java数据库访问通用类;
?* @author clplain;
?*
?*/
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.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sun.org.apache.commons.beanutils.ConvertUtils;

public class DBHelp {
?protected Connection conn = null;
?protected PreparedStatement ps = null;
?protected ResultSet rs = null;
?
?/**
? * 连接数据库方法
? * @return
? */
?
?public Connection getConn(){
??try {
???Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载驱动
???//获得连接
???conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=student","sa","sa");
??} catch (ClassNotFoundException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (SQLException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??return conn;
?}
?/**
? * 释放资源
? */
?public void closeAll(){
??try {
???if(rs != null){
????rs.close();
???}
???if(ps != null){
????ps.close();
???}
???if(conn != null){
????conn.close();
???}
??} catch (SQLException e) {
???e.printStackTrace();
??}
?}
?/**
? * 增、删、改方法
? * 参数1: sql语句
? * 参数2:Object数组,表示sql语句中问号占位符的值
? * 返回值:更新成功还是失败
? */
?public int executeUpdate(String sql,Object[] params){
??int result = 0;
??try {
???ps = this.getConn().prepareStatement(sql);//获得预处理对象
???//判断SQL语句中是否有问号占位符
???if(params != null && params.length != 0){
????//如果存在问号占位符,在给问号占位符设置值
????for(int i=0;i<params.length;i++){
?????ps.setObject(i+1, params[i]);
?????
????}
???}
???//执行增、删、改SQL语句
???result = ps.executeUpdate();
??} catch (SQLException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}finally{
???this.closeAll();//释放资源
??}
??return result;
?}
?
?/**
? * 使用反射技术实现数据库查询的通用方法
? * 参数1: sql语句
? * 参数2:Object数组,表示sql语句中问号占位符的值
? * 参数3: 将要封装的对象的类的反射
? * 返回:List 表示存放查询结果对象的集合
? */
?public List executeQuery(String sql,Object[] params,Class cla){
??List list = new ArrayList();
??try {
???ps = this.getConn().prepareStatement(sql);
???if(params != null && params.length != 0){
????for(int i=0;i<params.length;i++){
?????ps.setObject(i+1, params[i]);
????}
???}
???rs = ps.executeQuery();//执行SQL语句,并返回结果集
???/**
??? * 1.从结果集中获得数据
??? * 2.将数据封装到(未知)对象中、
??? * 3.将对象封装到List集合中
??? */
???//利用结果集对象获得ResultSetMetadata对象,ResultSetMetadata是结果集的源数据,在ResultSetMetadata中存放着查询结果的结果集的表结构
???ResultSetMetaData rsmt = rs.getMetaData();
???while(rs.next()){
????Object obj = cla.newInstance();
????//获得查询结果的列数
????int columnCount = rsmt.getColumnCount();
????//根据列数确定循环次数
????for(int i=0;i<columnCount;i++){
?????//获得属性名
?????String fieldName = rsmt.getColumnName(i+1);
?????//获得属性
?????Field field = cla.getDeclaredField(fieldName);
?????//拼接方法名
?????String methodName = "set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
//?????获得方法
?????Method method = cla.getDeclaredMethod(methodName, field.getType());
?????method.invoke(obj,rs.getObject(i+1));//调用对象的set方法赋值?????
????}
????list.add(obj);
???}
???
??} catch (Exception e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??return list;
?}
?
?
/*
public ResultSet executeQuery(String sql) throws SQLException{
??ps = this.getConn().prepareStatement(sql);
??rs = ps.executeQuery();
??return rs;
?}
?*/
}