DBUtils - 使用ResultSetHandler

时间:2012-03-06 16:42:55

标签: java jsp servlets jdbc

我正在尝试使用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;
        }

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:7)

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

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);

答案 1 :(得分:0)

如果列名与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);
 }