2012-03-06 46 views
0

我试图用ResultSetHandler通过学生一个servlet的名单,但得到以下错误DBUtils - 使用ResultSetHandler

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

在学生类中的方法是

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

的另一种方法,其中i我没有使用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; 
     } 

我该如何解决这个问题?

+0

您可以添加完整的堆栈跟踪吗? – gregor 2012-03-06 16:56:55

+0

当你从dbclient执行相同的查询'select student_id,student_name from tbl_student'时,它返回什么?它是否返回student_id列中的值'id'? – jambriz 2012-03-06 17:21:25

回答

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

使用BeanHandler和ArrayListHandler有什么区别? – Muhammad 2012-03-06 17:47:15

+1

BeanListHandler创建给定类的实例,并将给定列的值放入匹配属性中。因此,您必须重命名您的列以匹配您的bean属性的名称。 SQL应该是:'SELECT student_id id,student_name name FROM tbl_student', – gregor 2012-03-06 17:59:42

+0

别名可能发生为数据库关键字。例如:日期,系统日期,... – 2015-04-09 05:43:53

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); 
} 
相关问题