【问题标题】:DBUtils - using ResultSetHandlerDBUtils - 使用 ResultSetHandler
【发布时间】:2012-03-24 04:29:41
【问题描述】:

我正在尝试使用 ResultSetHandler 将学生列表传递给 servlet,但出现以下错误

java.lang.NumberFormatException: For input string: "id"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

Student类的方法是

          public List<Student> list2() throws SQLException {
          Connection connection = null;
          List<Student> studentList = null;
           try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)
              envCtx.lookup("jdbc/TestDB");
             connection = ds.getConnection();

             ResultSetHandler h = new ArrayListHandler();

              QueryRunner run = new QueryRunner(ds);

              String sql = "select student_id, student_name from tbl_student";

              studentList = (List<Student>)run.query(sql, h);


          }catch(SQLException sqle) {
              sqle.printStackTrace();
          }

          catch(Exception e) {
              e.printStackTrace();
          }

          finally {
              DbUtils.closeQuietly(connection);
          }

          return studentList;
      }

我不使用 DButils 的替代方法工作正常。

      public List<Student> list() throws SQLException {
            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;
            List<Student> studentList = new ArrayList<Student>();

            try {
              Context initCtx = new InitialContext();
              Context envCtx = (Context) initCtx.lookup("java:comp/env");
              DataSource ds = (DataSource)
                envCtx.lookup("jdbc/TestDB");
               connection = ds.getConnection();
                statement = connection.createStatement();
                resultSet = statement.executeQuery("select student_id, student_name from tbl_student");
                while (resultSet.next()) {
                    Student student = new Student();
                    student.setId(resultSet.getInt("student_id"));
                    student.setName(resultSet.getString("student_name"));
                    studentList.add(student);
                }
            }catch(SQLException e) {
                e.printStackTrace();
            }

            catch(Exception e) {
                e.printStackTrace();
            }

            finally {
                if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
                if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
                if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
            }

            return studentList;
        }

我该如何解决这个问题?

【问题讨论】:

  • 你能添加完整的堆栈跟踪吗?
  • 当您从 dbclient 执行相同的查询 select student_id, student_name from tbl_student 时,它会返回什么?它会在 student_id 列中返回值“id”吗?

标签: java jsp servlets jdbc


【解决方案1】:

我建议您使用 BeanListHandler 从 ResultSet 中获取所有行并将它们转换为 JavaBean 列表,如下所示:

QueryRunner queryRunner = new QueryRunner(dataSource);
ResultSetHandler<List<Student>> resultSetHandler = new BeanListHandler<Student>(Student.class);
List<Student> studentList = queryRunner.query("SELECT student_id, student_name FROM tbl_student", resultSetHandler);

【讨论】:

  • 使用BeanHandler和ArrayListHandler有什么区别?
  • BeanListHandler 创建给定类的实例并将给定列的值放入匹配的属性中。因此,您必须重命名列以匹配 bean 属性的名称。 SQL 应该是:SELECT student_id id, student_name name FROM tbl_student,
【解决方案2】:

如果列名与Bean的属性不匹配,请使用columntoPropertyOverrrides,这样可以完美解决这个问题。

 public <T> List<T> queryBeanList(String sql, final Class<T> clazz,final Map<String, String> columnToPropertyOverrides) throws SQLException { 
    ResultSetHandler<List<T>> rsh = new ResultSetHandler<List<T>>(){ 
        @Override
        public List<T> handle(ResultSet rs) throws SQLException { 
            BeanProcessor bp = new BeanProcessor(columnToPropertyOverrides); 
            return bp.toBeanList(rs, clazz);
        }

    };
    return query(sql, rsh);
 }

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 2013-02-16
    • 2018-09-06
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    相关资源
    最近更新 更多