/**
* 更新的公共方法
* @param sql 拼接的sql语句
* @param paramValue vo属性值的集合
* @throws SQLException
* @throws ClassNotFoundException
*/
public void updateVo(String sql,Object...paramValue) throws ClassNotFoundException, SQLException{
//获取连接
Connection con =DBUtil.getCon();
//预处理sql语句
PreparedStatement ps=con.prepareStatement(sql);
//填坑
if(paramValue!=null&¶mValue.length>0){
int count=0;
for(int i=0;i<paramValue.length;i++){
ps.setObject(++count, paramValue[i]);
}
}
//执行
ps.executeUpdate();
//关闭
DBUtil.close(con, ps, null);
}
/**
* 添加记录
* @param objVo
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws SQLException
* @throws ClassNotFoundException
*/
public void addVo(Object objVo,String preKeyName) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, SQLException{
//建立存放属性值的List
List fieldList = new ArrayList();
//获取表名
Class c= objVo.getClass();
String tableName=GetName.getTabOrColumnName(c.getSimpleName());
//动态拼接sql语句
StringBuffer sql=new StringBuffer("insert into "+tableName+" values(");
Field[] fArray=c.getDeclaredFields();
for(int i=0;i<fArray.length;i++){
if(fArray[i].getName().equals(preKeyName)){
sql.append("seq_"+tableName+".nextval,");
}else{
sql.append("?,");
//将vo的属性值添加到List里
String getter =GetName.getter(fArray[i]);
Method m = c.getDeclaredMethod(getter);
fieldList.add(m.invoke(objVo));
}
}
sql.deleteCharAt(sql.length()-1);
sql.append(")");
//执行公共方法
updateVo( sql.toString(), fieldList.toArray());
}
public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
详细解决方案
自各儿封装了一个BaseDao的插入的方法,但是添加日期类型老报错,求解!
热度:1451 发布时间:2016-04-11 00:11:17.0