标签:pre 获取 red ++ sql date jdb 结束 statement
private static PreparedStatement pst;
private static ResultSet rst;
public static <T> int insert(Connection conn, String sql, Class<T> clz, T vo) throws Exception {
pst = conn.prepareCall(sql);
// 为sql设置占位符内容
String[] split = sql.split("\\(")[1].split("\\)")[0].split(",");
for (int i = 0; i < split.length; i++) {
// 通过反射获取字段
Field f = vo.getClass().getDeclaredField(split[i]);
// 取消私有封装
f.setAccessible(true);
//取得字段对应的属性的值
Object fvalue = f.get(vo);
pst.setObject(i + 1, fvalue);
}
return pst.executeUpdate();
}
public <T> int update(Connection conn,String sql,T vo) throws Exception {
pst=conn.prepareStatement(sql);
String[] split = sql.split("SET")[1].split("WHERE")[0].split(",");
int i=0;
for ( i = 0; i < split.length; i++) {
String column = split[i].split("=")[0];
Field f = vo.getClass().getDeclaredField(column.trim());
f.setAccessible(true);
Object fvalue = f.get(vo);
pst.setObject(i + 1, fvalue);
}
[1]表示开始 [0]表示结束//代表中间的内容
String st = sql.split("WHERE")[1].split("=")[0];
//st.trim 表示去掉多余空格
Field declared = vo.getClass().getDeclaredField(st.trim());
declared.setAccessible(true);
Object fvalue = declared.get(vo);
pst.setObject(i + 1, fvalue);
return pst.executeUpdate();
}
标签:pre 获取 red ++ sql date jdb 结束 statement
原文地址:https://www.cnblogs.com/yhgn/p/10719675.html