package com.xing.mooc.util;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* 数据库操作辅助类
* @version 3.0
* @author yaohc
*/
public class DBUtil {
private static Logger logger = Logger.getLogger("DBUtil");
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
//private static Connection conn;
/**
* 该语句必须是 SQL INSERT、UPDATE 、DELETE 语句
*
* @param sql
* @return
* @throws Exception
*/
public int execute(String sql) throws Exception {
return execute(sql,new Object[]{});
}
/**
* insert语句使用,返回新增数据的主键。
* @param sql
* @return
*/
public Object execute(String sql,Object[] paramList,boolean falg) throws Exception{
Connection conn = null;
Object o = new Object();
try {
conn = getConnection();
o = this.execute(conn, sql, paramList, falg);
} catch (Exception e) {
logger.info(e.getMessage());
throw new Exception(e);
} finally {
closeConn(conn);
}
return o;
}
/**
* insert语句使用,返回新增数据的主键。
* @param sql
* @return
*/
public Object execute(Connection conn ,String sql,Object[] paramList,boolean falg) throws Exception{
if (sql == null || sql.trim().equals("")) {
logger.info("parameter is valid!");
}
PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
Object id = null;
try {
// 指定返回生成的主键
// 如果使用静态的SQL,则不需要动态插入参数
setPreparedStatementParam(pstmt, paramList);
if (pstmt == null) {
return -1;
}
pstmt.executeUpdate();
// 检索由于执行此 Statement 对象而创建的所有自动生成的键
ResultSet rs = pstmt.getGeneratedKeys();
if (rs.next()) {
id = rs.getObject(1);
System.out.println("数据主键地址:" + id);
}
} catch (Exception e) {
logger.info(e.getMessage());
throw new Exception(e);
} finally {
closeStatement(pstmt);
}
return id;
}
/**
* 该语句必须是 SQL INSERT、UPDATE 、DELETE 语句
* insert into table values(?,?,?,?)
* @param sql
* @param paramList:参数,与SQL语句中的占位符一
* @return
* @throws Exception
*/
public int execute(String sql,Object[] paramList) throws Exception {
if (sql == null || sql.trim().equals("")) {
logger.info("parameter is valid!");
}
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
try {
conn = getConnection();
pstmt = DBUtil.getPreparedStatement(conn, sql);
setPreparedStatementParam(pstmt, paramList);
if (pstmt == null) {
return -1;
}
result = pstmt.executeUpdate();
} catch (Exception e) {
logger.info(e.getMessage());
throw new Exception(e);
} finally {
closeStatement(pstmt);
closeConn(conn);
}
return result;
}
/**
* 事物处理类
* @param connection
* @param sql
* @param paramList:参数,与SQL语句中的占位符一
* @return
* @throws Exception
*/
public int execute(Connection conn,String sql,Object[] paramList) throws Exception {
if (sql == null || sql.trim().equals("")) {
logger.info("parameter is valid!");
}
PreparedStatement pstmt = null;
int result = 0;
try {
pstmt = DBUtil.getPreparedStatement(conn, sql);
setPreparedStatementParam(pstmt, paramList);
if (pstmt == null) {
return -1;
}
result = pstmt.executeUpdate();
} catch (Exception e) {
logger.info(e.getMessage());
throw new Exception(e);
} finally {
closeStatement(pstmt);
}
return result;
}
/**
* 获取实体类型的方法,type为实体类类型。
* @param type
* @param sql
* @param paramList
* @return
* @throws Exception
*/
public Object getObject(Class<?> type, String sql,Object[] paramList) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(type);
Object obj = type.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
Map map = getObject(sql, paramList);
if(map != null){
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map != null && map.containsKey(propertyName)) {
Object value = map.get(propertyName);
Object[] args = new Object[1];
args[0] = value;
if (null != value){
try{
descriptor.getWriteMethod().invoke(obj, args);
}catch(Exception e){
logger.info("检测一下Table列,和实体类属性:" + propertyName + ""
+ "是否一致,并且是否是" + value.getClass() + "类型");
throw new Exception("检测一下Table列,和实体类属性:" + propertyName + ""
+ "是否一致,并且是否是" + value.getClass() + "类型");
}
}
}
}
}else{
obj = null;
}
return obj;
}
public List<Class<?>> getQueryList(Class<?> type, String sql,Object[] paramList) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
List<Map<String,Object>> list = getQueryList(sql, paramList);
List beanList = new ArrayList();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Map<String, Object> map = (Map<String, Object>) iterator.next();
Object obj = type.newInstance();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map != null && map.containsKey(propertyName)) {
Object value = map.get(propertyName);
Object[] args = new Object[1];
args[0] = value;
if (null != value){
try{
descriptor.getWriteMethod().invoke(obj, args);
}catch(Exception e){
logger.info("检测一下Table列,和实体类属性:" + propertyName + ""
+ "是否一致,并且是否是" + value.getClass() + "类型");
throw new Exception("检测一下Table列,和实体类属性:" + propertyName + ""
+ "是否一致,并且是否是" + value.getClass() + "类型");
}
}
}
}
beanList.add(obj);
}
return beanList;
}
/**
* 将查询数据库获得的结果集转换为Map对象
*
* @param sql:查询
* @return
*/
public List<Map<String, Object>> getQueryList(String sql) throws Exception {
return getQueryList(sql,new Object[]{});
}
/**
* 将查询数据库获得的结果集转换为Map对象
*
* @param sql:查询
* @param paramList:参数
* @return
*/
public List<Map<String, Object>> getQueryList(String sql, Object[] paramList) throws Exception {
if (sql == null || sql.trim().equals("")) {
logger.info("parameter is valid!");
return null;
}
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Map<String, Object>> queryList = null;
try {
conn = getConnection();
pstmt = DBUtil.getPreparedStatement(conn, sql);
setPreparedStatementParam(pstmt, paramList);
if (pstmt == null) {
return null;
}
rs = getResultSet(pstmt);
queryList = getQueryList(rs);
} catch (RuntimeExcept
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
一个完成的java项目 (322个子文件)
DBUtil.class 15KB
MessageDaoImpl.class 11KB
AdminDaoImpl.class 6KB
AdminServlet.class 5KB
UserCenterServlet.class 5KB
MessageDealServlet.class 5KB
UserServlet.class 4KB
AdminMessageServlet.class 4KB
MessageServlet.class 4KB
MessageInfo.class 4KB
UserDaoImpl.class 4KB
User.class 4KB
CheckLoginFilter.class 3KB
UserMessageServlet.class 3KB
MessageServiceImpl.class 3KB
AdminThemeServlet.class 3KB
ReplyInfo.class 3KB
Message.class 2KB
LoadTestServlet.class 2KB
DBDataSource.class 2KB
CheckUserName.class 2KB
Reply.class 2KB
HTMLReplace.class 2KB
CharsetEncodingFilter.class 2KB
PageNew.class 2KB
UserServiceImpl.class 2KB
JsonTest.class 2KB
AdminServiceImpl.class 2KB
LogoutServlet.class 2KB
Test.class 2KB
ThemeDaoImpl.class 2KB
Admin.class 1KB
Page.class 1KB
ReplyDaoImpl.class 1KB
DateUtil.class 1KB
GetMessageText.class 1KB
Theme.class 1KB
IMessageService.class 1KB
IMessageDao.class 1KB
Stu.class 1KB
WebUtils.class 1KB
TestJiequ.class 1KB
IPUtil.class 1KB
LoadTest.class 988B
ThemeServiceImpl.class 923B
IAdminService.class 609B
IUserService.class 523B
TestDemo.class 517B
IAdminDao.class 507B
IUserDao.class 493B
StuText.class 402B
CountDaoImpl.class 307B
IReplyDao.class 241B
IThemeDao.class 238B
IThemeService.class 178B
ICountDao.class 117B
.classpath 931B
org.eclipse.wst.common.component 485B
org.eclipse.wst.jsdt.ui.superType.container 49B
bootstrap.css 143KB
bootstrap.min.css 118KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
bootstrap-datetimepicker.css 12KB
bootstrap-datetimepicker.min.css 11KB
site.css 2KB
bootstrapValidator.css 700B
bootstrapValidator.min.css 694B
glyphicons-halflings-regular.eot 20KB
favicon.ico 17KB
mysql-connector-java-5.1.35-bin.jar 946KB
spring-web-4.2.1.RELEASE.jar 751KB
c3p0-0.9.1.jar 594KB
log4j-1.2.17.jar 478KB
standard.jar 384KB
cglib-nodep-2.2.jar 315KB
commons-dbcp.jar 119KB
junit4.jar 105KB
commons-pool.jar 61KB
json-20170516.jar 56KB
jstl.jar 20KB
DBUtil.java 16KB
MessageDaoImpl.java 10KB
AdminServlet.java 6KB
AdminDaoImpl.java 5KB
MessageServlet.java 4KB
AdminMessageServlet.java 4KB
UserCenterServlet.java 4KB
MessageDealServlet.java 4KB
UserServlet.java 4KB
MessageInfo.java 3KB
User.java 3KB
UserDaoImpl.java 3KB
DBDataSource.java 2KB
UserMessageServlet.java 2KB
AdminThemeServlet.java 2KB
MessageServiceImpl.java 2KB
LoadTestServlet.java 2KB
CheckLoginFilter.java 2KB
ReplyInfo.java 2KB
共 322 条
- 1
- 2
- 3
- 4
资源评论
sdut菜鸟
- 粉丝: 215
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功