DAO介面及實作類別

來源:互聯網
上載者:User

標籤:

DAO介面中定義了所有的使用者操作,如添加記錄、刪除記錄及查詢記錄。

1 package chapter13;2 import java.util.*;3 public interface UserDAO {4     public void insert(User user) throws Exception;5     public void update(User user) throws Exception;6     public void delete(int userid) throws Exception;7     public User queryById(int userid) throws Exception;8     public List<User> queryAll() throws Exception;9 }

DAO實作類別實現了DAO介面,並且實現了介面中定義的所有方法。

  1 package chapter13;  2   3 import java.util.*;  4 import java.sql.*;  5 import java.sql.Date;  6   7 public class UserDAOImpl implements UserDAO {  8   9     @Override 10     public void insert(User user) throws Exception { 11         // TODO Auto-generated method stub 12         String sql="insert into user(username,password) values(?,?)"; 13         PreparedStatement preparedStatement=null; 14         DataBaseConnection dbc=null; 15         try{ 16             dbc=new DataBaseConnection(); 17             preparedStatement=dbc.getConnection().prepareStatement(sql); 18             preparedStatement.setString(1, user.getUserName()); 19             preparedStatement.setString(2, user.getPassword()); 20             preparedStatement.executeUpdate(); 21             preparedStatement.close(); 22         } 23         catch(Exception e){ 24             throw new Exception("操作出現異常"); 25         }finally { 26             dbc.close(); 27         } 28  29     } 30  31     @Override 32     public void update(User user) throws Exception { 33         // TODO Auto-generated method stub 34         String sql="update user set username=?,password=? where userid=?"; 35         PreparedStatement preparedStatement=null; 36         DataBaseConnection dbc=null; 37         try{ 38             dbc=new DataBaseConnection(); 39             preparedStatement=dbc.getConnection().prepareStatement(sql); 40             preparedStatement.setString(1, user.getUserName()); 41             preparedStatement.setString(2, user.getPassword()); 42             preparedStatement.setInt(3, user.getUserId()); 43             preparedStatement.executeUpdate(); 44             preparedStatement.close(); 45         } 46         catch(Exception e){ 47             throw new Exception("操作出現異常"); 48         }finally { 49             dbc.close(); 50         } 51     } 52  53     @Override 54     public void delete(int userid) throws Exception { 55         // TODO Auto-generated method stub 56         String sql="delete from user where userid=?"; 57         PreparedStatement preparedStatement=null; 58         DataBaseConnection dbc=null; 59         try{ 60             dbc=new DataBaseConnection(); 61             preparedStatement=dbc.getConnection().prepareStatement(sql); 62             preparedStatement.setInt(1, userid); 63             preparedStatement.executeUpdate(); 64             preparedStatement.close(); 65         } 66         catch(Exception e){ 67             throw new Exception("操作出現異常"); 68         }finally { 69             dbc.close(); 70         } 71     } 72  73     @Override 74     public User queryById(int userid) throws Exception { 75         // TODO Auto-generated method stub 76         User user=null; 77         String sql="select * from user where userid=?"; 78         PreparedStatement preparedStatement=null; 79         DataBaseConnection dataBaseConnection=null; 80         try{ 81             dataBaseConnection=new DataBaseConnection(); 82             preparedStatement=dataBaseConnection.getConnection().prepareStatement(sql); 83             ResultSet rSet=preparedStatement.executeQuery(); 84             if(rSet.next()){ 85                 user=new User(); 86                 user.setUserId(rSet.getInt(1)); 87                 user.setUserName(rSet.getString(2)); 88                 user.setPassword(rSet.getString(3)); 89             } 90             rSet.close(); 91             preparedStatement.close(); 92         } 93         catch(Exception e){ 94             throw new Exception("操作出現異常"); 95         }finally { 96             dataBaseConnection.close(); 97         } 98         return user; 99     }100 101     @Override102     public List<User> queryAll() throws Exception {103         // TODO Auto-generated method stub104         List<User> all=new ArrayList<User>();105         String sql="select * from user";106         PreparedStatement preparedStatement=null;107         DataBaseConnection dataBaseConnection=null;108         try{109             dataBaseConnection=new DataBaseConnection();110             preparedStatement=dataBaseConnection.getConnection().prepareStatement(sql);111             ResultSet rSet=preparedStatement.executeQuery();112             if(rSet.next()){113                 User user=new User();114                 user=new User();115                 user.setUserId(rSet.getInt(1));116                 user.setUserName(rSet.getString(2));117                 user.setPassword(rSet.getString(3));118                 all.add(user);119             }120             rSet.close();121             preparedStatement.close();122         }123         catch(Exception e){124             throw new Exception("操作出現異常");125         }finally {126             dataBaseConnection.close();127         }128         return all;129     }130 131 }

 

DAO介面及實作類別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.