首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

经典的jdbc连接数据库话语

2012-11-23 
经典的jdbc连接数据库语句public ListUser query() {ListUser userList new ArrayListUser()Stri

经典的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(); 
                 } 
             } 
         } catch (SQLException e) { 
             e.printStackTrace(); 
         } 
          
     } 
     return userList; 



热点排行