📄 customercardaccess.java
字号:
/*
* CustomerAccess.java
*
* Created on 2007年5月23日, 上午1:39
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package evan;
import java.sql.*;
import java.util.*;
import plugin.*;
import helper.*;
/**
*
* @author Evan Chan
*/
public class CustomerCardAccess
{
private IDBResource dbRes;
/** Creates a new instance of CustomerAccess */
public CustomerCardAccess(IDBResource dbRes)
{
this.dbRes = dbRes;
}
public int insert(CustomerCardInfo customerCardInfo)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "INSERT INTO CustomerCardInfo(CardId, Money, Password) " +
" VALUES(?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, customerCardInfo.getCardId());
ps.setString(2, Double.toString(customerCardInfo.getMoney()));
ps.setString(3, customerCardInfo.getPassword());
int rs = ps.executeUpdate();
return rs;
}
catch (SQLException ex)
{
ex.printStackTrace();
return -1;
}
}
public int update(CustomerCardInfo customerCardInfo)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "UPDATE CustomerCardInfo SET CardId = ?, CardName = ?, Remark = ?) " +
"WHERE CardId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, customerCardInfo.getCardId());
ps.setString(2, Double.toString(customerCardInfo.getMoney()));
ps.setString(3, customerCardInfo.getPassword());
int rs = ps.executeUpdate();
return rs;
}
catch (SQLException ex)
{
ex.printStackTrace();
return -1;
}
}
public int delete(CustomerCardInfo customerCardInfo)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "DELETE FROM CustomerCardInfo WHERE CardId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, customerCardInfo.getCardId());
int rs = ps.executeUpdate();
return rs;
}
catch (SQLException ex)
{
ex.printStackTrace();
return -1;
}
}
public CustomerCardInfo getCardById(String CardId)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "SELECT * FROM CustomerCardInfo WHERE CardId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, CardId);
ResultSet rs = ps.executeQuery();
ArrayList<CustomerCardInfo> customers = new ArrayList<CustomerCardInfo>();
readData(customers, rs);
if (customers.size() != 1)
{
return null;
}
return customers.get(0);
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
}
public ArrayList<CustomerCardInfo> getAllCustomerCards()
{
try
{
Connection conn = dbRes.getConnection();
String sql = "SELECT * FROM CustomerCardInfo";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ArrayList<CustomerCardInfo> customers = new ArrayList<CustomerCardInfo>();
readData(customers, rs);
return customers;
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
}
public ArrayList<CustomerCardInfo> getCustomerCardsByCondition(ArrayList<String> names, ArrayList<Object> values)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "SELECT * FROM CustomerCardInfo " + SQLHelper.createCondition(names);
PreparedStatement ps = conn.prepareStatement(sql);
for(int i = 0; i < values.size(); i++)
{
ps.setObject(i+1, values.get(i));
}
ResultSet rs = ps.executeQuery();
ArrayList<CustomerCardInfo> customers = new ArrayList<CustomerCardInfo>();
readData(customers, rs);
return customers;
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
}
private void readData(ArrayList<CustomerCardInfo> customers, ResultSet rs) throws SQLException
{
while(rs.next())
{
CustomerCardInfo customer = new CustomerCardInfo();
customer.setCardId(rs.getString("CardId"));
customer.setMoney(Double.parseDouble(rs.getString("Money")));
customer.setPassword(rs.getString("Password"));
customers.add(customer);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -