BASEJDBC
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class BaseJDBC {
private final String DRIVER = "com.mysql.jdbc.Driver";
private final String URL = "jdbc:mysql://localhost:3306/zzy";
private final String USERNMAE = "root";
private final String PASSWORD = "root";
/**
* 注册驱动 获取连接
*
* @return
*/
public Connection getConnection() {
try {
Class.forName(DRIVER);
return DriverManager.getConnection(URL, USERNMAE, PASSWORD);
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 关闭连接释放资源
* @param con
* @param st
* @param rt
*/
public void closeAll(Connection con, Statement st, ResultSet rt) {
try {
if (rt != null) {
rt.close();
rt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (st != null) {
st.close();
st = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
CRUDDAO
package com.dao;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.*;
import java.util.Map.Entry;
/**
*
* @author zzy
*
* 2016年12月1日下午1:49:49
*/
public class CRUDDAO<T> extends BaseJDBC {
private Connection con = null;
private PreparedStatement pt = null;
private Statement st = null;
private ResultSet rt = null;
private Class<T> c;
public CRUDDAO() {
}
public CRUDDAO(Class<T> c) {
this.c = c;
}
/**
* 查询操作要改造的地方 第一:参数必须抽象 第二:返回类型必须抽象
*
* @param <T>
* @param <T>
*
* @return Map<Integer, List<T>>
*/
public Map<Integer, List<T>> selectAll(Map<String, Object[]> m) {
int index = 0;
Map<Integer, List<T>> map = new LinkedHashMap<Integer, List<T>>();
List<T> list = null;
try {
con = super.getConnection();
if (con != null) {
Set<Entry<String, Object[]>> set = m.entrySet();
for (Entry<String, Object[]> entry : set) {
list = new ArrayList<T>();
pt = con.prepareStatement(entry.getKey());
this.bind(entry.getValue());
rt = pt.executeQuery();
while (rt.next()) {
list.add(this.toBean2());
}
map.put(++index, list);
}
} else {
System.out.println("数据库连接失败");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
super.closeAll(con, pt, rt);
}
return map;
}
/**
* 将数据库查询到的数据进行封装 封装成实体类之后再返回给调用者
*
* @return
*/
private T toBean() {
T t = null;
try {
t = c.newInstance();
Method[] m = c.getMethods();
ResultSetMetaData rmt = rt.getMetaData();
for (int i = 1, count = rmt.getColumnCount(); i <= count; i++) {
String columName = rmt.getColumnName(i);
columName = "set" + columName.substring(0, 1).toUpperCase()
+ columName.substring(1);
for (int j = 0; j < m.length; j++) {
if (columName.equals(m[j].getName())) {
m[j].invoke(t, rt.getObject(i));
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
private T toBean2() {
T t = null;
try {
t = c.newInstance();
Field[] field = c.getDeclaredFields();
for (Field f : field) {
Object value = rt.getObject(f.getName());
f.setAccessible(true);
f.set(t, value);
}
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
/**
* 绑定参数
*
* @param obj
*/
private void bind(Object[] obj) {
try {
if (obj != null) {
for (int i = 0, k = obj.length; i < k; i++) {
pt.setObject(i + 1, obj[i]);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 修改操作 进行的事务的控制 所有命令要么同时提交成功 要么同时回滚
*
* @param name
* @param id
* @return
*/
public int[] updateAll(Map<String, Object[]> map) {
int[] row = new int[map.size()];
int index = 0;
int error = 0;
try {
con = super.getConnection();
if (con != null) {
Set<Entry<String, Object[]>> set = map.entrySet();
con.setAutoCommit(false);
for (Entry<String, Object[]> entry : set) {
pt = con.prepareStatement(entry.getKey());
this.bind(entry.getValue());
row[index] = pt.executeUpdate();
if (row[index] == 0) {
throw new Exception("修改失败,数据回滚!");
}
index++;
}
} else {
System.out.println("数据库连接失败");
}
} catch (Exception e) {
error++;
e.printStackTrace();
} finally {
if (error > 0) {
try {
con.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
try {
con.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
super.closeAll(con, st, null);
}
return row;
}
}