標籤:
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介面及實作類別