📄 customerdaoimpl.java
字号:
package com.witbridge.netstore.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.witbridge.netstore.dao.CustomerDao;
import com.witbridge.netstore.model.Customers;
import com.witbridge.netstore.util.DBUtil;
public class CustomerDaoImpl implements CustomerDao {
Customers customer=null;
Connection conn=null;
ResultSet rs=null;
Statement st=null;
PreparedStatement pt=null;
public List<Customers> getAllCustomers(){
customer=new Customers();
List<Customers> customers=new ArrayList();
try{
conn=DBUtil.getIstance().getConn();
Statement st=conn.createStatement();
rs=st.executeQuery(Constants.SELECT_CUSTOMER);
while(rs.next()){
customer.setCustomerId(rs.getLong("ID"));
customer.setCustomerName(rs.getString("NAME"));
customer.setEmail(rs.getString("EMAIL"));
customer.setPassword(rs.getString("PASSWORD"));
customers.add(customer);
}
DBUtil.release(st, rs);
return customers;
}catch(SQLException e){
e.printStackTrace();
return null;
}
}
public Customers getCustomer(Long customerId) {
customer=new Customers();
try {
conn=DBUtil.getIstance().getConn();
pt=conn.prepareStatement(Constants.SELECT_CUSTOMER_KEY1);
pt.setLong(1,customerId);
rs=pt.executeQuery();
while(rs.next()){
customer.setCustomerId(rs.getLong("ID"));
customer.setCustomerName(rs.getString("NAME"));
customer.setEmail(rs.getString("EMAIL"));
customer.setPassword(rs.getString("PASSWORD"));
}
return customer;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}finally{
try {
DBUtil.release(pt, rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Customers getCustomer(String name, String password) {
customer=new Customers();
try {
conn=DBUtil.getIstance().getConn();
pt=conn.prepareStatement(Constants.SELECT_CUSTOMER_KEY2);
pt.setString(1,name);
pt.setString(2,password);
rs=pt.executeQuery();
while(rs.next()){
customer.setCustomerId(rs.getLong("ID"));
customer.setCustomerName(rs.getString("NAME"));
customer.setEmail(rs.getString("EMAIL"));
customer.setPassword(rs.getString("PASSWORD"));
}
return customer;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}finally{
try {
DBUtil.release(pt, rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int addCustomer(Customers customer) {
try {
conn=DBUtil.getIstance().getConn();
pt=conn.prepareStatement(Constants.ADD_CUSTOMER);
pt.setLong(1,customer.getCustomerId());
pt.setString(2,customer.getCustomerName());
pt.setString(3,customer.getPassword());
pt.setString(4,customer.getEmail());
int i=pt.executeUpdate();
DBUtil.release(pt);
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
public int deleteCustomer(Long customerId) {
try {
conn=DBUtil.getIstance().getConn();
pt=conn.prepareStatement(Constants.DELETE_CUSTOMER);
pt.setLong(1,customerId);
int i=pt.executeUpdate();
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
try {
DBUtil.release(pt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int updateCustomer(Customers customer) {
try {
conn=DBUtil.getIstance().getConn();
pt=conn.prepareStatement(Constants.UPDATE_CUSTOMER);
pt.setString(1,customer.getCustomerName());
pt.setString(2,customer.getPassword());
pt.setString(3,customer.getEmail());
pt.setLong(4,customer.getCustomerId());
int i=pt.executeUpdate();
DBUtil.release(pt);
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -