📄 customerdaoimpl.java
字号:
package com.magic.mobile.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import com.magic.mobile.dao.CustomerDAO;
import com.magic.mobile.exception.NotFoundCustomerException;
import com.magic.mobile.util.DBUtil;
import com.magic.mobile.vo.Customer;
public class CustomerDAOImpl implements CustomerDAO {
//添加客户
public boolean addCustomer(Customer customer) {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "insert into tcustomer values(customer_sequence.nextval,?,?,?,?,?,?)";
try {
conn = DBUtil.getConn();
pstmt = DBUtil.getpStmt(conn, sql);
pstmt.setString(1,customer.getIdType());
pstmt.setString(2,customer.getIdNumber());
pstmt.setString(3,customer.getCustomerName());
pstmt.setTimestamp(4, new Timestamp(customer.getCustomerBirthday().getTime()));
pstmt.setString(5,customer.getCustomerSex());
pstmt.setString(6,customer.getCustomerAddress());
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
return false;
} finally {
DBUtil.closeStmt(pstmt);
DBUtil.closeConn(conn);
}
}
//更新客户
public boolean updateCustomer(Customer customer) {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update tcustomer set id_type=?,id_number=?,customer_name=?,customer_birthday=?,customer_sex=?,customer_address=? where customer_id=?";
try {
conn = DBUtil.getConn();
pstmt = DBUtil.getpStmt(conn, sql);
pstmt.setString(1,customer.getIdType());
pstmt.setString(2,customer.getIdNumber());
pstmt.setString(3,customer.getCustomerName());
pstmt.setTimestamp(4, new Timestamp(customer.getCustomerBirthday().getTime()));
pstmt.setString(5,customer.getCustomerSex());
pstmt.setString(6,customer.getCustomerAddress());
pstmt.setLong(7, customer.getCustomerId());
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
return false;
} finally {
DBUtil.closeStmt(pstmt);
DBUtil.closeConn(conn);
}
}
public void getCustomer() {
}
//查询客户
public Customer selectCustomer(Customer customer) throws NotFoundCustomerException {
Connection conn = null;
ResultSet rs = null;
String sql = "select * from tcustomer where id_type='"
+ customer.getIdType() + "' and id_number='"
+ customer.getIdNumber() + "'";
try {
conn = DBUtil.getConn();
rs = DBUtil.getRs(conn, sql);
if (rs.next()) {
// 客户已经存在
customer.setCustomerId(rs.getLong(1));
customer.setIdType(rs.getString(2));
customer.setIdNumber(rs.getString(3));
customer.setCustomerName(rs.getString(4));
customer.setCustomerBirthday(rs.getDate(5));
customer.setCustomerSex(rs.getString(6));
customer.setCustomerAddress(rs.getString(7));
} else {
// 客户不存在
throw new NotFoundCustomerException();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeRs(rs);
DBUtil.closeConn(conn);
}
return customer;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -