2012-03-06 43 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); 
} 
相關問題