⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcsubscriberdao.java

📁 看了《 使用JSF 构建数据库驱动的应用程序》 一文后
💻 JAVA
字号:
package jsfdb.model.dao;

import jsfdb.model.LoginInfo;
import jsfdb.model.Subscriber;
import jsfdb.model.ModelUtils;
import jsfdb.model.err.IncorrectPasswordException;
import jsfdb.model.err.LoginException;
import jsfdb.model.err.ProfileException;
import jsfdb.model.err.SubscribeException;
import jsfdb.model.err.UnknownSubscriberException;
import jsfdb.model.err.UnsubscribeException;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;

public class JDBCSubscriberDAO implements SubscriberDAO {
  //  private DataSource dataSource;

    public JDBCSubscriberDAO() {
        try{  
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch(ClassNotFoundException e){
             ModelUtils.log(e);
        }
        

     
           
    }

    private void close(Connection conn, PreparedStatement ps)
            throws SQLException {
        if (ps != null)
            ps.close();
        if (conn != null)
            conn.close();
    }

    public Subscriber select(LoginInfo loginInfo)
            throws LoginException,
            UnknownSubscriberException,
            IncorrectPasswordException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn =  DriverManager.getConnection("jdbc:odbc:jsfdb");
            ps = conn.prepareStatement(
                ModelUtils.getResource("SelectStatement"));
            ps.setString(1, loginInfo.getEmail());
            ResultSet rs = ps.executeQuery();
            if (!rs.next())
                throw new UnknownSubscriberException();
            String password = rs.getString(1);
            if (!loginInfo.getPassword().equals(password))
                throw new IncorrectPasswordException();
            Subscriber subscriber = new Subscriber();
            subscriber.setEmail(loginInfo.getEmail());
            subscriber.setPassword(loginInfo.getPassword());
            subscriber.setName(rs.getString(2));
            subscriber.setManager(rs.getBoolean(3));
            subscriber.setDeveloper(rs.getBoolean(4));
            subscriber.setAdministrator(rs.getBoolean(5));
            subscriber.setSubscriptionType(rs.getInt(6));
            return subscriber;
        } catch (SQLException x) {
            ModelUtils.log(x);
            throw new LoginException();
        } finally {
            try {
                close(conn, ps);
            } catch (SQLException x) {
                ModelUtils.log(x);
                throw new LoginException();
            }
        }
    }

    public void insert(Subscriber subscriber)
            throws SubscribeException {
        Connection conn =null;
        PreparedStatement ps = null;
        try {
            conn =  DriverManager.getConnection("jdbc:odbc:jsfdb");
            ps = conn.prepareStatement(
                ModelUtils.getResource("InsertStatement"));
            ps.setString(1, subscriber.getEmail());
            ps.setString(2, subscriber.getPassword());
            ps.setString(3, subscriber.getName());
            ps.setBoolean(4, subscriber.isManager());
            ps.setBoolean(5, subscriber.isDeveloper());
            ps.setBoolean(6, subscriber.isAdministrator());
            ps.setInt(7, subscriber.getSubscriptionType());
            int rowCount = ps.executeUpdate();
            if (rowCount != 1)
                throw new SubscribeException();
        } catch (SQLException x) {
            ModelUtils.log(x);
            throw new SubscribeException();
        } finally {
            try {
                close(conn, ps);
            } catch (SQLException x) {
                ModelUtils.log(x);
                throw new SubscribeException();
            }
        }
    }

    public void update(Subscriber subscriber)
            throws ProfileException {
        Connection conn =null;
        PreparedStatement ps = null;
        try {
            conn =  DriverManager.getConnection("jdbc:odbc:jsfdb");
            ps = conn.prepareStatement(
                ModelUtils.getResource("UpdateStatement"));
            ps.setString(1, subscriber.getPassword());
            ps.setString(2, subscriber.getName());
            ps.setBoolean(3, subscriber.isManager());
            ps.setBoolean(4, subscriber.isDeveloper());
            ps.setBoolean(5, subscriber.isAdministrator());
            ps.setInt(6, subscriber.getSubscriptionType());
            ps.setString(7, subscriber.getEmail());
            int rowCount = ps.executeUpdate();
            if (rowCount != 1)
                throw new ProfileException();
        } catch (SQLException x) {
            ModelUtils.log(x);
            throw new ProfileException();
        } finally {
            try {
                close(conn, ps);
            } catch (SQLException x) {
                ModelUtils.log(x);
                throw new ProfileException();
            }
        }
    }

    public void delete(Subscriber subscriber)
            throws UnsubscribeException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DriverManager.getConnection("jdbc:odbc:jsfdb");
            ps = conn.prepareStatement(
                ModelUtils.getResource("DeleteStatement"));
            ps.setString(1, subscriber.getEmail());
            int rowCount = ps.executeUpdate();
            if (rowCount != 1)
                throw new UnsubscribeException();
        } catch (SQLException x) {
            ModelUtils.log(x);
            throw new UnsubscribeException();
        } finally {
            try {
                close(conn, ps);
            } catch (SQLException x) {
                ModelUtils.log(x);
                throw new UnsubscribeException();
            }
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -