日期:2014-05-16  浏览次数:20555 次

经典的jdbc连接数据库语句
public List<User> query() { 
 
    List<User> userList = new ArrayList<User>(); 
    String sql = "select * from User"; 
 
    Connection con = null; 
    PreparedStatement pst = null; 
    ResultSet rs = null; 
    try { 
         con = HsqldbUtil.getConnection(); 
         pst = con.prepareStatement(sql); 
         rs = pst.executeQuery(); 
  
         User user = null; 
         while (rs.next()) { 
  
             user = new User(); 
             user.setId(rs.getInt("id")); 
             user.setUserName(rs.getString("user_name")); 
             user.setBirth(rs.getDate("birth")); 
             user.setCreateDate(rs.getDate("create_date")); 
             userList.add(user); 
         } 
  
  
     } catch (SQLException e) { 
         e.printStackTrace(); 
     }finally{ 
         if(rs != null){ 
             try { 
                 rs.close(); 
             } catch (SQLException e) { 
                 e.printStackTrace(); 
             } 
         } 
         try { 
             pst.close(); 
         } catch (SQLException e) { 
             e.printStackTrace(); 
         } 
         try { 
             if(!con.isClosed()){ 
                 try { 
                     con.close(); 
                 } catch (SQLException e) { 
                     e.printStackTrace(); 
                 } 
             }