📄 customersourceaccess.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 CustomerSourceAccess
{
private IDBResource dbRes;
/** Creates a new instance of CustomerAccess */
public CustomerSourceAccess(IDBResource dbRes)
{
this.dbRes = dbRes;
}
public int insert(CustomerSourceInfo customerSourceInfo)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "INSERT INTO CustomerSourceInfo(SourceId, SourceName, Remark) " +
" VALUES(?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, customerSourceInfo.getSourceId());
ps.setString(2, customerSourceInfo.getSourceName());
ps.setString(3, customerSourceInfo.getRemark());
int rs = ps.executeUpdate();
return rs;
}
catch (SQLException ex)
{
ex.printStackTrace();
return -1;
}
}
public int update(CustomerSourceInfo customerSourceInfo)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "UPDATE CustomerSourceInfo SET SourceId = ?, SourceName = ?, Remark = ?) " +
"WHERE SourceId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, customerSourceInfo.getSourceId());
ps.setString(2, customerSourceInfo.getSourceName());
ps.setString(3, customerSourceInfo.getRemark());
int rs = ps.executeUpdate();
return rs;
}
catch (SQLException ex)
{
ex.printStackTrace();
return -1;
}
}
public int delete(CustomerSourceInfo customerSourceInfo)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "DELETE FROM CustomerSourceInfo WHERE SourceId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, customerSourceInfo.getSourceId());
int rs = ps.executeUpdate();
return rs;
}
catch (SQLException ex)
{
ex.printStackTrace();
return -1;
}
}
public CustomerSourceInfo getSourceById(String sourceId)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "SELECT * FROM CustomerSourceInfo WHERE SourceId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, sourceId);
ResultSet rs = ps.executeQuery();
ArrayList<CustomerSourceInfo> customers = new ArrayList<CustomerSourceInfo>();
readData(customers, rs);
if (customers.size() != 1)
{
return null;
}
return customers.get(0);
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
}
public ArrayList<CustomerSourceInfo> getAllCustomerSources()
{
try
{
Connection conn = dbRes.getConnection();
String sql = "SELECT * FROM CustomerSourceInfo";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
ArrayList<CustomerSourceInfo> customers = new ArrayList<CustomerSourceInfo>();
readData(customers, rs);
return customers;
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
}
public ArrayList<CustomerSourceInfo> getCustomerSourcesByCondition(ArrayList<String> names, ArrayList<Object> values)
{
try
{
Connection conn = dbRes.getConnection();
String sql = "SELECT * FROM CustomerSourceInfo " + 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<CustomerSourceInfo> customers = new ArrayList<CustomerSourceInfo>();
readData(customers, rs);
return customers;
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
}
private void readData(ArrayList<CustomerSourceInfo> customers, ResultSet rs) throws SQLException
{
while(rs.next())
{
CustomerSourceInfo customer = new CustomerSourceInfo();
customer.setSourceId(rs.getString("SourceId"));
customer.setSourceName(rs.getString("SourceName"));
customer.setRemark(rs.getString("Remark"));
customers.add(customer);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -