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

📄 customerdao.java~23~

📁 本程序是用java编写的一个关于银行存款取款的软件
💻 JAVA~23~
字号:
package dao;

import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.*;
import java.sql.Connection;
import java.sql.*;
import data.CustomerData;

public class CustomerDAO {
    private DataSource ds;
    public CustomerDAO() {
        System.out.println("enter Constructor CustomerDAO....");
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/eBank");
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
        System.out.println("exit Constructor CustomerDAO....");
    }

    public boolean addCustomer(String firstName, String lastName,
                               String address,
                               String phone) {
        System.out.println("enter add Customer....");
        try {
            Connection con = ds.getConnection();
            String sql =
                    "insert into j2eecustmoter values(seq_custmoter.nextval,?,?,?,?)";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, firstName);
            pstmt.setString(2, lastName);
            pstmt.setString(3, address);
            pstmt.setString(4, phone);

            pstmt.executeUpdate();

            pstmt.close();
            con.close();
            System.out.println("exit add Customer....");
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean deleteCustomer(Integer customerid) {
        System.out.println("enter delete Customer....");
        try {
            Connection con = ds.getConnection();
            String sql = "delete from j2eecustomer where customerid=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, customerid.intValue());
            pstmt.executeUpdate();
            pstmt.close();
            con.close();
            System.out.println("exit delete Customer....");
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }

    }

    public boolean storeCustomer(Integer id, String firstName, String lastName,
                                 String address,
                                 String phone) {
        System.out.println("enter store Customer....");
        try {
            Connection con = ds.getConnection();
            String sql =
                    "update j2eecustomer set firstname=?,lastname=?,address=?,phone=? where customerid=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, firstName);
            pstmt.setString(2, lastName);
            pstmt.setString(3, address);
            pstmt.setString(4, phone);
            pstmt.setInt(5, id.intValue());

            pstmt.executeUpdate();
            pstmt.close();
            con.close();
            System.out.println("exit store Customer....");
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }

    }

    public CustomerData loadCustomer(Integer id) {
        System.out.println("enter load Customer....");
        CustomerData cd = new CustomerData();
        try {
            Connection con = ds.getConnection();
            String sql = "select * from j2eecustomer where customerid=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, id.intValue());
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                cd.setCustomerID(id.intValue());
                cd.setFirstName(rs.getString("firstname"));
                cd.setLastName(rs.getString("lastname"));
                cd.setAddress(rs.getString("address"));
                cd.setPhone(rs.getString("phone"));

            }
            rs.close();
            pstmt.close();
            con.close();
            System.out.println("exit load Customer....");
            return cd;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return cd;
        }

    }

    public Integer getCustomerid(String firstName, String lastName,
                                 String address,
                                 String phone) {
        System.out.println("enter get Customer  ID....");
        try {
            Connection con = ds.getConnection();
            String sql = "select * from j2eecustomer where firstname=? and lastname=? and address=? and phone=?";
            PreparedStatement pstmt = con.prepareStatement(sql);
            pstmt.setString(1, firstName);
            pstmt.setString(2, lastName);
            pstmt.setString(3, address);
            pstmt.setString(4, phone);
            ResultSet rs = pstmt.executeQuery();
            Integer i = null;
            if (rs.next()) {
                i = new Integer(rs.getInt("customerid"));
            }
            rs.close();
            pstmt.close();
            con.close();
            System.out.println("exit get Customer  ID....");
            return i;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public boolean selectByPrimaryKey(Integer key) {
        System.out.println("enter load selectByPrimaryKey....");
     try {
         Connection con = ds.getConnection();
         String sql = "select * from j2eecustomer where customerid=?";
         PreparedStatement pstmt = con.prepareStatement(sql);
         pstmt.setInt(1, key.intValue());
         ResultSet rs = pstmt.executeQuery();
         boolean flag=false;
         if (rs.next()) {
             flag=true;
         }
         rs.close();
         pstmt.close();
         con.close();
         System.out.println("exit load selectByPrimaryKey....");
         return flag;
     } catch (SQLException ex) {
         ex.printStackTrace();
         return false;
     }

    }
}

⌨️ 快捷键说明

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