?? customerdao.java~21~
字號:
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(int 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);
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 j2eecustmoter 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 + -