📄 oraclecustomerdao.java
字号:
package dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import javabean.Customer;
public class OracleCustomerDAO implements CustomerDAO {
private final String INSERT_AN_CUSTOMER = "insert into tcustomer values(customer_sequence.nextval,?,?,?,?,?,?)";
private final String GET_AN_CUSTOMER = "select * from tcustomer where id_number=?";
private final String UPDATE_AN_CUSTOMER = "update tcustomer set customer_name=?,customer_birth=?,customer_sex=?,customer_addr=? where id_number=?";
public Customer getAnCustomer(Customer customer){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Customer cus = new Customer();
try{
conn = OracleDAOFactory.getConnection();
pstmt = conn.prepareStatement(GET_AN_CUSTOMER);
pstmt.setString(1,customer.getIdNumber());
rs = pstmt.executeQuery();
if(rs.next()){
cus.setCustomerId(rs.getInt("CUSTOMER_ID"));
cus.setIdType(rs.getString("ID_TYPE"));
cus.setIdNumber(rs.getString("ID_NUMBER"));
cus.setCustomerName(rs.getString("CUSTOMER_NAME"));
cus.setCustomerBirthday(rs.getDate("CUSTOMER_BIRTH"));
cus.setCustomerSex(rs.getString("CUSTOMER_SEX"));
cus.setCustomerAddress(rs.getString("CUSTOMER_ADDR"));
return cus;
}else{
System.out.println("no this person");
return null;
}
}catch(SQLException e){
e.printStackTrace();
}finally{
OracleDAOFactory.closeResultSet(rs);
OracleDAOFactory.closeStatement(pstmt);
OracleDAOFactory.closeConnection(conn);
}
return cus;
}
public boolean createCustomer(Customer customer){
Connection conn = null;
PreparedStatement pstmt = null;
boolean tmp = true;
try{
conn = OracleDAOFactory.getConnection();
pstmt = conn.prepareStatement(INSERT_AN_CUSTOMER);
pstmt.setString(1, customer.getIdType());
pstmt.setString(2, customer.getIdNumber());
pstmt.setString(3, customer.getCustomerName());
java.util.Date date = new Date(customer.getCustomerBirthday().getTime());
java.sql.Date ddate = new Date(date.getTime());
pstmt.setDate(4, ddate);
pstmt.setString(5, customer.getCustomerSex());
pstmt.setString(6, customer.getCustomerAddress());
int result = pstmt.executeUpdate();
if(result!=1){
tmp = false;
}
}catch(SQLException e){
//e.printStackTrace();
tmp = false;
}finally{
OracleDAOFactory.closeStatement(pstmt);
OracleDAOFactory.closeConnection(conn);
}
return tmp;
}
public boolean updateCustomer(Customer customer){
Connection conn = null;
PreparedStatement pstmt = null;
int customerId = 0;
boolean tmp = true;
try{
conn = OracleDAOFactory.getConnection();
pstmt = conn.prepareStatement(UPDATE_AN_CUSTOMER);
pstmt.setString(1, customer.getCustomerName());
java.util.Date date = new Date(customer.getCustomerBirthday().getTime());
java.sql.Date ddate = new Date(date.getTime());
pstmt.setDate(2, ddate);
pstmt.setString(3, customer.getCustomerSex());
pstmt.setString(4, customer.getCustomerAddress());
pstmt.setString(5, customer.getIdNumber());
int result = pstmt.executeUpdate();
System.out.println("update success !!!!!!!!!!!!!!!!");
if(result!=1){
tmp = false;
System.out.println("update failed !!!!!!!!!!");
}
}catch(SQLException e){
tmp = false;
}finally{
OracleDAOFactory.closeStatement(pstmt);
OracleDAOFactory.closeConnection(conn);
}
return tmp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -