package com.teach.commons;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Field;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import com.teach.annotations.Name;
import com.teach.annotations.Id;
/**
* jdbc工具类
*
* @author J.L.Zhou
*
*/
public class DBUtil {
/**
* 构造方法私有化,无例模式
*/
private DBUtil() {
}
private static String url;
private static String user;
private static String pwd;
static {
try {
Properties config = new Properties();
config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
url = config.getProperty("jdbc.url");
user = config.getProperty("jdbc.username");
pwd = config.getProperty("jdbc.password");
String className = config.getProperty("jdbc.driverClassName");
Class.forName(className);
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
/**
*
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, pwd);
}
/**
* 关闭jdbc对象
*
* @param conn
* @param stmt
*/
public static void close(Connection conn, Statement stmt) {
try {
stmt.close();
} catch (Exception ex) {
}
try {
conn.close();
} catch (Exception ex) {
}
}
/**
* 关闭jdbc对象
*
* @param conn
* @param stmt
* @param rs
*/
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
rs.close();
} catch (Exception ex) {
}
close(conn, stmt);
}
/**
* 执行sql语句
*
* @param sql
* @param args
* @throws SQLException
*/
public static void execute(String sql, Object... args) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
setParams(pstmt, args);
pstmt.executeUpdate();
} finally {
close(conn, pstmt);
}
}
/**
* 获取一个对象
*
* @param sql
* @param cls
* @param args
* @return
* @throws SQLException
*/
public static <T> T get(String sql, Class<T> cls, Object... args) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
setParams(pstmt, args);
rs = pstmt.executeQuery();
if (rs.next()) {
T t = load(rs, cls);
if (rs.next()) {
throw new SQLException("发现的记录>1");
} else {
return t;
}
} else {
throw new SQLException("没有发现一条记录");
}
} finally {
close(conn, pstmt, rs);
}
}
public static Map<String,Object> getMap(String sql, Object... args) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
setParams(pstmt, args);
rs = pstmt.executeQuery();
if (rs.next()) {
Map<String,Object> t = load(rs);
if (rs.next()) {
throw new SQLException("发现的记录>1");
} else {
return t;
}
} else {
throw new SQLException("没有发现一条记录");
}
} finally {
close(conn, pstmt, rs);
}
}
/**
* 查询数据库获取结果集的第一行第一列的数据
*
* @param sql
* @param args
* @return
* @throws SQLException
*/
public static String getString(String sql, Object... args) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
setParams(pstmt, args);
rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getString(1);
} else {
throw new SQLException("没有发现一条记录");
}
} finally {
close(conn, pstmt, rs);
}
}
/**
* 查询数据库获取结果集的第一行第一列的数据
*
* @param sql
* @param args
* @return
* @throws SQLException
*/
public static long getLong(String sql, Object... args) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
setParams(pstmt, args);
rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getLong(1);
} else {
throw new SQLException("没有发现一条记录");
}
} finally {
close(conn, pstmt, rs);
}
}
/**
* 获取对象列表
*
* @param sql
* @param cls
* @param args
* @return
* @throws SQLException
*/
public static <T> List<T> list(String sql, Class<T> cls, Object... args) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
setParams(pstmt, args);
rs = pstmt.executeQuery();
List<T> list = new ArrayList<T>();
while (rs.next()) {
list.add(load(rs, cls));
}
return list;
} finally {
close(conn, pstmt, rs);
}
}
public static List<Map<String,Object>> listMap(String sql, Object... args) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
setParams(pstmt, args);
rs = pstmt.executeQuery();
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
while (rs.next()) {
list.add(load(rs));
}
return list;
} finally {
close(conn, pstmt, rs);
}
}
/**
* 设置预编译的语句的所有参数
*
* @param pstmt
* @param args
* @throws SQLException
*/
public static void setParams(PreparedStatement pstmt, Object... args) throws SQLException {
for (int i = 0; i < args.length; i++) {
setParams(pstmt, i + 1, args[i]);
}
}
/**
* 设置预编译的语句的一个参数
*
* @param pstmt
* @param index
* @param value
* @throws SQLException
*/
public static void setParams(PreparedStatement pstmt, int index, Object value) throws SQLException {
if (value == null) {
pstmt.setObject(index, null);
} else if (value instanceof Date) {
Date d = (Date) value;
pstmt.setTimestamp(index, new java.sql.Timestamp(d.getTime()));
} else {
pstmt.setObject(index, value);
}
}
@SuppressWarnings("unchecked")
public static <T> T load(ResultSet rs, Class<T> cls) throws SQLException {
Object obj = null;
try {
obj = cls.newInstance();
} catch (Exception e) {
throw new SQLException("调用无参构造方法实例化错误:" + cls.getName(), e);
}
for (PropertyDescriptor prop : getPropertyDescriptors(cls)) {
if (prop.getName().equals("class")) {
continue;
}
String columnName = getColumnName(cls, prop);
try {
Object value = null;
try {
value = rs.getObject(columnName);
} catch (Exception ex) {
// 警告,字段不存在
}
if (value != null) {
if (prop.getPropertyType() == Integer.class) {
value = rs.getInt(columnName);
} else if (prop.getPropertyType() == Character.class) {
value = rs.getString(columnName).charAt(0);
} else if (prop.getPropertyType() == Date.class) {
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Java Web实现登陆注册页面前端与后台数据连接(代码+sql文件) (112个子文件)
DBUtil.class 16KB
CnToSpell.class 14KB
FileUtil.class 6KB
ZipUtil.class 6KB
UserInfo.class 5KB
RoleInfoDAO.class 4KB
BaseDAO.class 3KB
Converter.class 3KB
SnowflakeId.class 2KB
RequestUtil.class 2KB
UserInfoServiceTest.class 2KB
DBUtil.class 2KB
UserInfoServiceImpl.class 2KB
LoginServlet.class 2KB
LoginUserVO.class 2KB
MD5.class 2KB
OrderItemMessage.class 2KB
UserListServlet.class 2KB
NewsClass.class 2KB
RoleInfoDAOTest.class 1KB
NewsClassDAOTest.class 1KB
RoleInfo.class 1KB
OrderMessage.class 1KB
ConvertDate.class 1KB
UserInfoDAOTest.class 935B
DAOSupport.class 800B
Name.class 565B
Id.class 471B
ConvertChar.class 463B
UserMessageService.class 413B
UserInfoService.class 399B
OrderItemMessageDAO.class 398B
OrderMessageDAO.class 386B
NewsClassDAO.class 370B
UserInfoDAO.class 366B
FileUtil$LineReadListener.class 331B
.classpath 892B
org.eclipse.wst.common.component 549B
org.eclipse.wst.jsdt.ui.superType.container 49B
style.css 5KB
table.css 3KB
table.css 3KB
RegisteredOfFront.html 4KB
LoginOfFront.html 3KB
UserListOfFront.html 2KB
index.html 2KB
UserListOfFront.html 2KB
SysPromptOfFront.html 1KB
index.html 123B
mysql-connector-java-5.1.6-bin.jar 687KB
DBUtil.java 16KB
CnToSpell.java 13KB
ZipUtil.java 10KB
FileUtil.java 5KB
UserInfo.java 4KB
SnowflakeId.java 4KB
RoleInfoDAO.java 2KB
Converter.java 2KB
BaseDAO.java 2KB
UserInfoServiceTest.java 2KB
OrderItemMessage.java 2KB
DBUtil.java 2KB
LoginServlet.java 1KB
DAOSupport.java 1KB
MD5.java 1KB
RequestUtil.java 1KB
UserInfoServiceImpl.java 1KB
OrderMessage.java 1KB
UserListServlet.java 1KB
LoginUserVO.java 1KB
NewsClass.java 1KB
RoleInfo.java 814B
RoleInfoDAOTest.java 756B
UserMessageService.java 734B
UserInfoService.java 715B
NewsClassDAOTest.java 666B
Name.java 647B
Id.java 468B
UserInfoDAOTest.java 406B
OrderItemMessageDAO.java 151B
OrderMessageDAO.java 147B
UserInfoDAO.java 129B
NewsClassDAO.java 125B
jquery.min.js 95KB
jquery-1.9.1.min.js 90KB
jquery-1.9.1.min.js 90KB
back-endPage.js 16KB
back-endPage.js 16KB
front-endPage.js 16KB
front-endPage.js 16KB
shujv2.js 12KB
shujv2.js 12KB
.jsdtscope 567B
login.jsp 3KB
userlist.jsp 2KB
userlist.jsp 2KB
loginErro.jsp 1KB
MANIFEST.MF 39B
org.eclipse.wst.jsdt.ui.superType.name 6B
backgroud.png 447KB
共 112 条
- 1
- 2
资源评论
伤感666
- 粉丝: 86
- 资源: 108
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功