package ppp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import ppp.exception.AppSQLException;
import ppp.vo.CustomerVO;
public class CustomerSelectDAO extends SelectNoTxDAOBase<CustomerVO> {
private static final String SQL_BASE
= "SELECT customer_id, company_name, address, telno, dept, customer_name, customer_kana, email, password FROM customer WHERE 1 = 1"; private static final String SQL_ORDER_BY
= " ORDER BY customer_id";
private static final String[] WHERE_PHRASE
= { " AND customer_id = ?", " AND password = ?"
};
super(conn);
}
@Override
protected String makeSql
(Object[] params
) throws AppSQLException
{
if (params == null || params.length > 2) {
throw new AppSQLException("パラメータ数が不正です");
}
StringBuilder sb = new StringBuilder(SQL_BASE);
System.
out.
println(sb.
toString()); for (int i = 0; i < params.length; i++) {
if (params[i] != null) {
sb.append(WHERE_PHRASE[i]);
System.
out.
println(sb.
toString()); }
}
sb.append(SQL_ORDER_BY);
System.
out.
println(sb.
toString()); return sb.toString();
}
@Override
CustomerVO customer = new CustomerVO();
customer.setCustomerId(rs.getString("customer_id"));
customer.setCompanyName(rs.getString("company_name"));
customer.setAddress(rs.getString("address"));
customer.setTelno(rs.getString("telno"));
customer.setDept(rs.getString("dept"));
customer.setCustomerName(rs.getString("customer_name"));
customer.setCustomerKana(rs.getString("customer_kana"));
customer.setEmail(rs.getString("email"));
customer.setPassword(rs.getString("password"));
return customer;
}
public int getMaxId() throws AppSQLException {
int maxId = 0;
try {
String sql
= "SELECT max(customer_id) FROM customer"; stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
maxId = rs.getInt("max");
}
throw new AppSQLException("データベースからの抽出依頼で障害が発生しました", e);
} finally {
try {
if (rs != null) rs.close();
throw new AppSQLException("結果セットの解放に失敗しました", e);
}
try {
if (stmt != null) stmt.close();
throw new AppSQLException("ステートメントの解放に失敗しました", e);
}
}
return maxId;
}
}
最終更新:2013年06月08日 15:45