package com.whiteblack.ebookbusniss.dao.common;
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.HashMap;
import java.util.List;
import java.util.Map;
public class BaseDao {
private static String driverClass = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/tb_user";
private static String user = "root";
private static String password = "123456";
public int update(String sql, Object ... params) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
if(params != null){
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
int count = pstmt.executeUpdate();
return count;
} catch (Exception e) {
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, pstmt, null);
}
}
public int insert(String sql, Object ... params) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
if(params != null){
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
int count = pstmt.executeUpdate();
if(count > 0){
String idSql = "SELECT LAST_INSERT_ID()";
ResultSet rs = conn.createStatement().executeQuery(idSql);
if(rs.next()){
return rs.getInt(1);
}
}
} catch (Exception e) {
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, pstmt, null);
}
return 0;
}
@SuppressWarnings("unchecked")
public <T> T queryForObject(Class<T> clazz, String sql, Object ... params) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
if(params != null){
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
rs = pstmt.executeQuery();
if(rs.next()){
Object value = rs.getObject(1);
return (T)value;
}
} catch (Exception e) {
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, pstmt, rs);
}
return null;
}
public int queryForInt(String sql, Object ... params) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
if(params != null){
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
rs = pstmt.executeQuery();
if(rs.next()){
return rs.getInt(1);
}
} catch (Exception e) {
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, pstmt, rs);
}
return -1;
}
public <T> T queryForBean(Class<? extends ParseResultSet<T>> clazz, String sql, Object ... params) throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
if(params != null){
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
rs = pstmt.executeQuery();
if(rs.next()){
ParseResultSet<T> bean = clazz.newInstance();
return bean.readResultSetValues(rs);
}
} catch (Exception e) {
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, pstmt, rs);
}
return null;
}
@SuppressWarnings("unchecked")
public <T> List<T> query(String sql, Object ... params) throws SQLException{
List<T> list = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
if(params != null){
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
rs = pstmt.executeQuery();
list = new ArrayList<T>();
while(rs.next()){
Object value = rs.getObject(1);
list.add((T)value);
}
} catch (Exception e) {
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, pstmt, rs);
}
return list;
}
public <T> List<T> query(Class<? extends ParseResultSet> clazz, String sql, Object ... params) throws SQLException{
List<T> list = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
if(params != null){
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
rs = pstmt.executeQuery();
list = new ArrayList<T>();
while(rs.next()){
ParseResultSet<T> bean = clazz.newInstance();
list.add(bean.readResultSetValues(rs));
}
} catch (Exception e) {
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, pstmt, rs);
}
return list;
}
public List<Map<String, Object>> queryForList(String sql, Object ... params) throws SQLException{
List<Map<String, Object>> list = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
if(params != null){
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
rs = pstmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
list = new ArrayList<Map<String, Object>>();
while(rs.next()){
Map<String, Object> rowMap = new HashMap<String, Object>();
for (int i = 1; i <= colCount; i++) {
String name = rsmd.getCatalogName(i).toLowerCase();
Object value = rs.getObject(i);
if(value != null) rowMap.put(name, value);
}
list.add(rowMap);
}
} catch (Exception e) {
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, pstmt, rs);
}
return list;
}
public int[] executeBatch(String sql, Object[][] paramsAry) throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
if(paramsAry != null){
for (int i = 0; i < paramsAry.length; i++) {
Object[] params = paramsAry[i];
for (int j = 0; j < params.length; j++) {
pstmt.setObject(j + 1, params[j]);
}
pstmt.addBatch();
}
}
int[] counts = pstmt.executeBatch();
return counts;
} catch (Exception e) {
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, pstmt, null);
}
}
public int[] executeBatch(String[] sqlAry, Object[][] paramsAry) throws SQLException {
int[] counts = new int[sqlAry.length];
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(false);
for (int i = 0; i < sqlAry.length; i++) {
String sql = sqlAry[i];
pstmt = conn.prepareStatement(sql);
if(paramsAry != null){
Object[] params = paramsAry[i];
if(params != null){
for (int j = 0; j < params.length; j++) {
pstmt.setObject(j + 1, params[j]);
}
}
}
counts[i] = pstmt.executeUpdate();
pstmt.close();
}
conn.commit();
return counts;
} catch (Exception e) {
conn.rollback();
if(e instanceof SQLException){
throw (SQLException)e;
}else{
throw new SQLException(e);
}
}finally{
close(conn, null, null);
}
}
private void close(ResultSet rs, PreparedStatement pstmt, Connection conn) {
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// log ....
}
}
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
// log ....
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// log ....
}
}
}
private void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
BaseDao
最新推荐文章于 2018-09-17 12:30:24 发布