DAO接口中定义了所有的用户操作,如添加记录、删除记录及查询记录。
package chapter13;
import java.util.*;
public interface UserDAO {
public void insert(User user) throws Exception;
public void update(User user) throws Exception;
public void delete(int userid) throws Exception;
public User queryById(int userid) throws Exception;
public List<User> queryAll() throws Exception;
}
DAO实现类实现了DAO接口,并且实现了接口中定义的所有方法。
package chapter13; import java.util.*;
import java.sql.*;
import java.sql.Date; public class UserDAOImpl implements UserDAO { @Override
public void insert(User user) throws Exception {
// TODO Auto-generated method stub
String sql="insert into user(username,password) values(?,?)";
PreparedStatement preparedStatement=null;
DataBaseConnection dbc=null;
try{
dbc=new DataBaseConnection();
preparedStatement=dbc.getConnection().prepareStatement(sql);
preparedStatement.setString(1, user.getUserName());
preparedStatement.setString(2, user.getPassword());
preparedStatement.executeUpdate();
preparedStatement.close();
}
catch(Exception e){
throw new Exception("操作出现异常");
}finally {
dbc.close();
} } @Override
public void update(User user) throws Exception {
// TODO Auto-generated method stub
String sql="update user set username=?,password=? where userid=?";
PreparedStatement preparedStatement=null;
DataBaseConnection dbc=null;
try{
dbc=new DataBaseConnection();
preparedStatement=dbc.getConnection().prepareStatement(sql);
preparedStatement.setString(1, user.getUserName());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setInt(3, user.getUserId());
preparedStatement.executeUpdate();
preparedStatement.close();
}
catch(Exception e){
throw new Exception("操作出现异常");
}finally {
dbc.close();
}
} @Override
public void delete(int userid) throws Exception {
// TODO Auto-generated method stub
String sql="delete from user where userid=?";
PreparedStatement preparedStatement=null;
DataBaseConnection dbc=null;
try{
dbc=new DataBaseConnection();
preparedStatement=dbc.getConnection().prepareStatement(sql);
preparedStatement.setInt(1, userid);
preparedStatement.executeUpdate();
preparedStatement.close();
}
catch(Exception e){
throw new Exception("操作出现异常");
}finally {
dbc.close();
}
} @Override
public User queryById(int userid) throws Exception {
// TODO Auto-generated method stub
User user=null;
String sql="select * from user where userid=?";
PreparedStatement preparedStatement=null;
DataBaseConnection dataBaseConnection=null;
try{
dataBaseConnection=new DataBaseConnection();
preparedStatement=dataBaseConnection.getConnection().prepareStatement(sql);
ResultSet rSet=preparedStatement.executeQuery();
if(rSet.next()){
user=new User();
user.setUserId(rSet.getInt(1));
user.setUserName(rSet.getString(2));
user.setPassword(rSet.getString(3));
}
rSet.close();
preparedStatement.close();
}
catch(Exception e){
throw new Exception("操作出现异常");
}finally {
dataBaseConnection.close();
}
return user;
} @Override
public List<User> queryAll() throws Exception {
// TODO Auto-generated method stub
List<User> all=new ArrayList<User>();
String sql="select * from user";
PreparedStatement preparedStatement=null;
DataBaseConnection dataBaseConnection=null;
try{
dataBaseConnection=new DataBaseConnection();
preparedStatement=dataBaseConnection.getConnection().prepareStatement(sql);
ResultSet rSet=preparedStatement.executeQuery();
if(rSet.next()){
User user=new User();
user=new User();
user.setUserId(rSet.getInt(1));
user.setUserName(rSet.getString(2));
user.setPassword(rSet.getString(3));
all.add(user);
}
rSet.close();
preparedStatement.close();
}
catch(Exception e){
throw new Exception("操作出现异常");
}finally {
dataBaseConnection.close();
}
return all;
} }