___码字选手 2015-12-02 08:53 采纳率: 0%
浏览 1555
已结题

java 泛型接口add 拼写出errors 无法进行添加操作

正在学习泛型接口 在底层代码中
insert into web_student_school_name(errors,name)values(?,?)
[{}, 四川大学]
拼写出errors 无法进行数据库添加操作
java.sql.SQLException: Unknown column 'errors' in 'field list' Query: insert into web_student_school_name(errors,name)values(?,?) Parameters: [{}, 四川大学]
at org.apache.commons.dbutils.QueryRunner.rethrow(QueryRunner.java:542)
at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:599)
at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:655)
at cn.bangbangjiao.cd.dao.impl.BaseDAO.update(BaseDAO.java:19)
at cn.bangbangjiao.cd.dao.impl.GenericDAOImpl.add(GenericDAOImpl.java:73)
at bangbangjiao.TestDAOImpl.testschool(TestDAOImpl.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
报错的basedao
public class BaseDAO {
private QueryRunner queryRunner = new QueryRunner(DBUtil.getdatasouce());
/**
* 专门用来执行 update insert delete 这样的sql
* @param sql
* @param params
*/
public void update(String sql, Object... params){
try {
queryRunner.update(sql, params);
} catch (SQLException e) {
e.printStackTrace();
}
}

 public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params){
     try {
        return queryRunner.query(sql, rsh, params);
    } catch (SQLException e) {
        e.printStackTrace();
    }
     return null;
 }


下列是add泛型实现

public void add(T t) {

    StringBuilder insertSql = new StringBuilder("insert into ").append(getTable()).append("(");
    // values(?,
    StringBuilder   valueSql =  new StringBuilder("values(");
    try {
        List<Object> params = new ArrayList<>();//专门用来存放数据对应的值,这些值要为sql中的占位符赋值.
        //>>1.通过javabean的自省机制出去this.clazz中的所有属性对应的get方法
        BeanInfo  beanInfo = Introspector.getBeanInfo(this.clazz,Object.class);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
              //>>2.根据属性的名字往sql上面拼?,并且属性的名字是id的话就不要拼了
              String proertyName = propertyDescriptor.getName();  //dir
              if("id".equals(proertyName)){
                  continue;
              }
              //>>3.得到属性的类型
              Class   propertyType = propertyDescriptor.getPropertyType();//ProductDir类型
              Object value = propertyDescriptor.getReadMethod().invoke(t);//productdir对象
              if(propertyType.isAnnotationPresent(Entry.class)){
                  proertyName = proertyName+"_id"; //dir_id
                          //得到productdir的类型,然后再从该类型上面得到getid方法, 再讲getid方法在productdir对象执行
                 value =  value.getClass().getMethod("getId").invoke(value);//得到dir属性的id值.
              }

              insertSql.append(proertyName).append(",");
              valueSql.append("?,");
              //>>3.从t对象中读到响应的属性值
              params.add(value);
        }
    /*  System.out.println(insertSql);
        System.out.println(valueSql);*/
        insertSql.replace(insertSql.length()-1, insertSql.length(), ")");//去掉
        valueSql.replace(valueSql.length()-1, valueSql.length(), ")");
        insertSql.append(valueSql);
        System.out.println(insertSql);
        System.out.println(params);

        update(insertSql.toString(), params.toArray());

    } catch (Exception e) {
        e.printStackTrace();
    }
}
  • 写回答

2条回答 默认 最新

  • only_yiyi_han 2015-12-02 08:58
    关注

    你把打印出来的SQL语句放到数据库中测试一下!

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器