zoukankan      html  css  js  c++  java
  • DAO接口及实现类

    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     @Override
    102     public List<User> queryAll() throws Exception {
    103         // TODO Auto-generated method stub
    104         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 }
  • 相关阅读:
    axios 修改头部请求数据格式的方法
    基于VUE的可以滚动的横向时间轴
    25.客户端多线程分组模拟高频并发数据
    24.原子操作
    23.线程锁的使用
    22.线程自解锁
    21.多线程-锁与临界区域
    20.多线程-基本代码
    19.添加高精度计时器测量处理能力
    18.windows使用select突破64个socket
  • 原文地址:https://www.cnblogs.com/xingzhui/p/5759116.html
Copyright © 2011-2022 走看看