アットウィキロゴ

ouchi > Dustbox > CustomerSelectDAO的な

  1. package ppp.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import ppp.exception.AppSQLException;
  9. import ppp.vo.CustomerVO;
  10.  
  11. public class CustomerSelectDAO extends SelectNoTxDAOBase<CustomerVO> {
  12.  
  13. 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";
  14. private static final String SQL_ORDER_BY = " ORDER BY customer_id";
  15.  
  16. private static final String[] WHERE_PHRASE = {
  17. " AND customer_id = ?", " AND password = ?"
  18. };
  19.  
  20. public CustomerSelectDAO(Connection conn) {
  21. super(conn);
  22. }
  23.  
  24. @Override
  25. protected String makeSql(Object[] params) throws AppSQLException {
  26.  
  27. if (params == null || params.length > 2) {
  28. throw new AppSQLException("パラメータ数が不正です");
  29. }
  30.  
  31. StringBuilder sb = new StringBuilder(SQL_BASE);
  32. System.out.println(sb.toString());
  33. for (int i = 0; i < params.length; i++) {
  34. if (params[i] != null) {
  35. sb.append(WHERE_PHRASE[i]);
  36. System.out.println(sb.toString());
  37. }
  38. }
  39. sb.append(SQL_ORDER_BY);
  40.  
  41. System.out.println(sb.toString());
  42. return sb.toString();
  43. }
  44.  
  45. @Override
  46. protected CustomerVO makeRowObject(ResultSet rs) throws SQLException {
  47. CustomerVO customer = new CustomerVO();
  48. customer.setCustomerId(rs.getString("customer_id"));
  49. customer.setCompanyName(rs.getString("company_name"));
  50. customer.setAddress(rs.getString("address"));
  51. customer.setTelno(rs.getString("telno"));
  52. customer.setDept(rs.getString("dept"));
  53. customer.setCustomerName(rs.getString("customer_name"));
  54. customer.setCustomerKana(rs.getString("customer_kana"));
  55. customer.setEmail(rs.getString("email"));
  56. customer.setPassword(rs.getString("password"));
  57. return customer;
  58. }
  59.  
  60. public int getMaxId() throws AppSQLException {
  61. PreparedStatement stmt = null;
  62. ResultSet rs = null;
  63. int maxId = 0;
  64. try {
  65. String sql = "SELECT max(customer_id) FROM customer";
  66. stmt = conn.prepareStatement(sql);
  67. rs = stmt.executeQuery();
  68. while (rs.next()) {
  69. maxId = rs.getInt("max");
  70. }
  71. } catch (SQLException e) {
  72. throw new AppSQLException("データベースからの抽出依頼で障害が発生しました", e);
  73. } finally {
  74. try {
  75. if (rs != null) rs.close();
  76. } catch (SQLException e) {
  77. throw new AppSQLException("結果セットの解放に失敗しました", e);
  78. }
  79. try {
  80. if (stmt != null) stmt.close();
  81. } catch (SQLException e) {
  82. throw new AppSQLException("ステートメントの解放に失敗しました", e);
  83. }
  84. }
  85. return maxId;
  86. }
  87.  
  88. }
  89.  
最終更新:2013年06月08日 15:45