📄 customerdaoimpl.java
字号:
package com.lc.zyz.model.persistence.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.lc.zyz.model.domain.CustomerInfo;
import com.lc.zyz.model.persistence.dao.CustomerDao;
public class CustomerDaoImpl implements CustomerDao {
CustomerInfo ci=null;
Connection conn=null;
PreparedStatement ps=null;
boolean result=false;
public boolean addCustomer(CustomerInfo ci){
try{
conn=DBConn.getConn();
ps=conn.prepareStatement("insert into customers values (?,?,?,?,?)");
ps.setString(1, ci.getCusId());
ps.setString(2, ci.getCusName());
ps.setString(3, ci.getCusSex());
ps.setInt(4, ci.getCusAge());
ps.setString(5, ci.getCusIdCard());
if(ps.executeUpdate()!=0)
{
result=true;
}
else
{
result=false;
}
}
catch(SQLException e){
e.printStackTrace();
}
return result;
}//end method addCustomer
public boolean deleteCustomer(String cusId) throws Exception {
try{
conn=DBConn.getConn();
ps=conn.prepareStatement("delete from customers where cusId=?");
ps.setString(1, cusId);
if(ps.executeUpdate()!=0)
{
result=true;
}
else
{
result=false;
}
}
catch(SQLException e){
e.printStackTrace();
}
return result;
}//end method deleteCustomer
public boolean updateCustomer(CustomerInfo ci) throws Exception {
try{
conn=DBConn.getConn();
ps=conn.prepareStatement("update customers set cusName=?,cusSex=?,cusAge=?,cusIdCard=? where cusId=?");
ps.setString(1, ci.getCusName());
ps.setString(2, ci.getCusSex());
ps.setInt(3, ci.getCusAge());
ps.setString(4, ci.getCusIdCard());
ps.setString(5, ci.getCusId());
if(ps.executeUpdate()!=0)
{
result=true;
}
else
{
result=false;
}
}
catch(SQLException e){
e.printStackTrace();
}
return result;
}//end method updateCustomer
public CustomerInfo getCustomerById(String cusId) throws Exception {
try{
ci=new CustomerInfo();
conn=DBConn.getConn();
ps=conn.prepareStatement("select * from customers where cusId=?");
ps.setString(1, cusId);
ResultSet rs=ps.executeQuery();
if(null!=rs)
{
while(rs.next())
{
ci.setCusId(rs.getString(1));
ci.setCusName(rs.getString(2));
ci.setCusSex(rs.getString(3));
ci.setCusAge(rs.getInt(4));
ci.setCusIdCard(rs.getString(5));
}
}
}
catch(SQLException e){
e.printStackTrace();
}
return ci;
}//end method getCustomerById
public Iterator getAllCustomer() throws Exception {
List list=null;
try{
ci=new CustomerInfo();
conn=DBConn.getConn();
ps=conn.prepareStatement("select * from customers");
ResultSet rs=ps.executeQuery();
list=new ArrayList();
if(null!=rs)
{
while(rs.next())
{
list.add(rs.getString(1));
list.add(rs.getString(2));
list.add(rs.getString(3));
list.add(Integer.valueOf(rs.getInt(4)));
list.add(rs.getString(5));
}
}
}
catch(SQLException e){
e.printStackTrace();
}
return list.iterator();
}//end method getAllCustomer
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -