日期:2014-05-16 浏览次数:20612 次
package com.drug.db;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
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.Iterator;
/**
* 自己写的一个数据库工具类,希望可以封装所有的数据库操作
* 直接对封装好的数据库对象javaBean进行操作
*
* 数据库中表中的字段名必须首字母大写
* 封装好的数据对象类javaBean中的变量必须和数据库表中的字段相对应,(首字母不用大写)
*
* 这样以后修改数据库,只需要对应的修改一下数据对象类javaBean就行了,呵呵
*
* 本人java还在自学中,望大家多多指正
*/
public class DBUtil {
/**
* 方 法 名: getTableRowCount
* 功能描述: 获取表的行数
* 输入参数: tableName:表名
* 返 回 值: int
* 编 码 人: zmj
* 编码时间: 2010-2-8 上午11:10:26
*/
public int getTableRowCount(String tableName) {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
int rowCount = -1;
try {
String sql = "select * from " + tableName;
con = getCon();
pst = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = pst.executeQuery();
rs.last();
rowCount = rs.getRow();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
close(con, pst, rs);
}
return rowCount;
}
/**
* 方 法 名:hasElem
* 功能描述:判断表中是否含有该元素
* 输入参数: tableName:表名,keyName:查询的键值,key用来查询的键
* 返 回 值: boolean
* 编 码 人: zmj
* 编码时间: 2010-2-20 上午10:26:33
*/
public boolean hasElem(String tableName, String keyName, Object key) {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
String sql = "select * from " + tableName + " where "
+ firstUpper(keyName) + "=?";
try {
con = getCon();
pst = con.prepareStatement(sql);
setObject(pst, 1, key);
rs = pst.executeQuery();
if (rs.next()) {
return true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(con, pst, rs);
}
return false;
}
/**
* 方 法 名:getTableElem
* 功能描述: 获取数据库中一个表中的全部数据
* 输入参数:tableName:表名,beanName:用来封闭的javaBean
* 返 回 值: ArrayList<Object>
* 编 码 人: zmj
* 编码时间: 2010-2-20 上午08:16:52
*/
public ArrayList<Object> getTableElem(String tableName, String beanName) {
return getElemList(tableName, beanName, 1, -1);
}
/***
* 方 法 名:getTable
* 功能描述:获取数据库中一个表中的数据
* 输入参数:tableName:表名,beanName:用来封装的javaBean,offset:偏移量,count:取出的记录条数,为-1时表示全部
* 返 回值:ArrayList<Object>
* 编 码 人: zmj
* 编码时间: 2010-2-8
* **/
public ArrayList<Object> getElemList(String tableName, String beanName,
int offset, int count) {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
ArrayList<Object> rs_list = new ArrayList<Object>();
try {
String sql = "select * from " + tableName;
con = getCon();
pst = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = pst.executeQuery();
String[] paramName = tableColName(rs);
Class<?> beanClass = Class.forName(beanName);
if (offset > 1)
rs.absolute(offset - 1);
else if (offset < 0 && offset != -1)
return rs_list;
while (rs.next() && (count == -1 || count-- > 0)) {