前几天闲着改了改原先的数据库查询工具,勉强基本够用,虽然还没有加入事务。
感觉实现的有些问题,并发考虑的方式错了,先做提醒,整理好知识点再考虑修改
感觉只对修改进行了加锁,查询不用进行加锁操作,这样应该比较合理,有问题欢迎提出来。
/**
* @Author: YangZaining
* @Date: Created in 23:20 2018/6/15
*/
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.*;
public class JdbcBase {
private static Map<Class<?>, Class<?>> keyd;//装箱
private static Map<Class<?>, Class<?>> keyu;//拆箱
static {
keyd = new HashMap<>();
keyd.put(Integer.class, int.class);
keyd.put(Boolean.class, boolean.class);
keyd.put(Character.class, char.class);
keyd.put(Long.class, long.class);
keyd.put(Short.class, short.class);
keyd.put(Byte.class, byte.class);
keyd.put(Double.class, double.class);
keyd.put(Float.class, float.class);
keyd.put(int.class, int.class);
keyd.put(boolean.class, boolean.class);
keyd.put(char.class, char.class);
keyd.put(long.class, long.class);
keyd.put(short.class, short.class);
keyd.put(byte.class, byte.class);
keyd.put(double.class, double.class);
keyd.put(float.class, float.class);
keyd.put(String.class, String.class);
keyu = new HashMap<>();
keyu.put(int.class, Integer.class);
keyu.put(boolean.class, Boolean.class);
keyu.put(char.class, Character.class);
keyu.put(long.class, Long.class);
keyu.put(short.class, Short.class);
keyu.put(byte.class, Byte.class);
keyu.put(double.class, Double.class);
keyu.put(float.class, Float.class);
keyu.put(Integer.class, Integer.class);
keyu.put(Boolean.class, Boolean.class);
keyu.put(Character.class, Character.class);
keyu.put(Long.class, Long.class);
keyu.put(Short.class, Short.class);
keyu.put(Byte.class, Byte.class);
keyu.put(Double.class, Double.class);
keyu.put(Float.class, Float.class);
keyu.put(String.class, String.class);
}//基本类型初始话
private JdbcBase() {
}
static class Pair<T1, T2> {
T1 t_1;
T2 t_2;
public Pair(T1 t_1, T2 t_2) {
this.t_1 = t_1;
this.t_2 = t_2;
}
public T1 getT_1() {
return t_1;
}
public void setT_1(T1 t_1) {
this.t_1 = t_1;
}
public T2 getT_2() {
return t_2;
}
public void setT_2(T2 t_2) {
this.t_2 = t_2;
}
@Override
public String toString() {
return "Pair{" +
"t_1=" + t_1 +
", t_2=" + t_2 +
'}';
}
}
private static void setObject(PreparedStatement st, Object... obj) {
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
try {
st.setObject(i + 1, obj[i]);
} catch (SQLException e) {
System.out.println("set object failed");
throw new RuntimeException(e);
}
}
}
}
public static synchronized int Update(String sql, Object... obj) {
int result = -1;
Connection con = ConnectionPool.getConnection();
PreparedStatement st = null;
try {
st = con.prepareStatement(sql);
setObject(st, obj);
result = st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionPool.free(null, st, con);
}
return result;
}//更新
public static <T> List<T> dateQuery(Class<T> clazz, String sql, Object... obj) {
ResultSet rs = null;
Connection con = null;
PreparedStatement st = null;
List<T> ls = null;
try {
con = ConnectionPool.getConnection();
ls = new ArrayList<>();
st = con.prepareStatement(sql);
setObject(st, obj);
rs = st.executeQuery();
ResultSetMetaData rm = rs.getMetaData();
List<Pair<Method, String>> ms = getmethods(rm, clazz);
while (rs.next()) {
if (ms != null) {
T o = clazz.newInstance();
setObjects(ms, o, rs);
ls.add(o);
} else {
T o = (T) upvalue(clazz)
.getConstructor(String.class)
.newInstance(rs.getString(rm.getColumnLabel(1)));
ls.add(o);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionPool.free(rs, st, con);
}
return ls;
}//查找多个
public static <T> T dateQueryOne(Class<T> clazz, String sql, Object... obj) {
ResultSet rs = null;
Connection con = null;
PreparedStatement st = null;
T o = null;
try {
con = ConnectionPool.getConnection();
st = con.prepareStatement(sql);
setObject(st, obj);
rs = st.executeQuery();
ResultSetMetaData rm = rs.getMetaData();
List<Pair<Method, String>> ms = getmethods(rm, clazz);
if (rs.next()) {
if (ms != null) {
o = clazz.newInstance();
setObjects(ms, o, rs);
} else {
o = (T) upvalue(clazz)
.getConstructor(String.class)
.newInstance(rs.getString(rm.getColumnLabel(1)));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionPool.free(rs, st, con);
}
return o;
}//查找一个
private static <T> void setObjects(List<Pair<Method, String>> ms, T o, ResultSet rs) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
for (Pair<Method, String> k : ms) {
k.t_1.setAccessible(true);
Class<?> t = downvalue(k.t_1.getParameterTypes()[0]);
String name = classname(t);
Method mm = ResultSet.class.getDeclaredMethod(name, String.class);//getInt getDouble getString
mm.setAccessible(true);
k.t_1.invoke(o,mm.invoke(rs, k.t_2));
}
}
private static List<Pair<Method, String>> getmethods(ResultSetMetaData rm, Class<?> clazz) throws SQLException {
Map<String, String> k1 = new HashMap<>();
List<Pair<Method, String>> k2 = new ArrayList<>();
int col = rm.getColumnCount();
for (int i = 1; i <= col; i++) {//change name -> setName
String name = rm.getColumnLabel(i);
String namenew = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
k1.put(namenew, name);
}
Method[] methods = clazz.getDeclaredMethods();
for (Method m : methods) {//chose all exist set Methods
String s = k1.get(m.getName());
if (s != null) {
k2.add(new Pair<Method, String>(m, s));
}
}
return k2.size() != 0 ? k2 : null;
}
private static Class<?> downvalue(Class<?> t) {
if (keyd.get(t) == null) {
throw new ArithmeticException();
}
return keyd.get(t);
}
private static Class<?> upvalue(Class<?> t) {
if (keyu.get(t) == null) {
throw new ArithmeticException();
}
return keyu.get(t);
}
private static String classname(Class<?> clazz) {
if (String.class.isAssignableFrom(clazz)) {
return "getString";
} else {
return "get" + clazz.getName().substring(0, 1).toUpperCase() + clazz.getName().substring(1);
}
}
}