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

正在学习泛型接口 在底层代码中
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个回答

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

qq_31080089
一个努力的人儿 回复qq_29178991: at cn.bangbangjiao.cd.dao.impl.BaseDAO.update(BaseDAO.java:19) 你点击19行进去 一步一步追加代码
大约 4 年之前 回复
qq_29178991
___且听风吟 打印出来的sql语句中insert into web_student_school_name(errors,name)values(?,?) [{}, 四川大学]测试后是错误的
大约 4 年之前 回复

表中有errors字段码

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问