コード例 #1
0
 public void addMusician(Musician m) {
   try {
     PreparedStatement stmt =
         conn.prepareStatement(
             "insert into musicians values(0, ?, ?, ?, ?, ?, ?)",
             ResultSet.TYPE_FORWARD_ONLY,
             ResultSet.CONCUR_READ_ONLY);
     stmt.setString(1, m.getUsername());
     stmt.setString(2, m.getPassword());
     stmt.setString(3, m.getFirstName());
     stmt.setString(4, m.getLastName());
     if (m.getBirthdate() == null) {
       stmt.setTimestamp(5, null);
     } else {
       stmt.setTimestamp(5, new java.sql.Timestamp(m.getBirthdate().getTime()));
     }
     if (m.getHabitation() == null) {
       stmt.setNull(6, java.sql.Types.INTEGER);
     } else {
       stmt.setInt(6, m.getHabitation().getId());
     }
     stmt.executeUpdate();
   } catch (SQLException e) {
     System.out.println(e.getMessage());
   }
 }
コード例 #2
0
  public void updateMusician(Musician m) {
    try {
      PreparedStatement stmt =
          conn.prepareStatement(
              "update musicians m set first_name = ?,"
                  + "last_name = ?, birthdate = ?, habitation_id = ?, password = ?",
              ResultSet.TYPE_FORWARD_ONLY,
              ResultSet.CONCUR_READ_ONLY);
      stmt.setString(1, m.getFirstName());
      stmt.setString(2, m.getLastName());
      stmt.setTimestamp(3, new java.sql.Timestamp(m.getBirthdate().getTime()));
      stmt.setInt(4, m.getHabitation().getId());
      stmt.setString(5, m.getPassword());
      stmt.executeUpdate();

      // set instrument_skills
      stmt =
          conn.prepareStatement(
              "delete from instrument_skills where musician_id = ?",
              ResultSet.TYPE_FORWARD_ONLY,
              ResultSet.CONCUR_READ_ONLY);
      stmt.setInt(1, m.getId());
      stmt.executeUpdate();

      for (Instrument i : m.getSkills()) {
        stmt =
            conn.prepareStatement(
                "insert into instrument_skills values(?, ?)",
                ResultSet.TYPE_FORWARD_ONLY,
                ResultSet.CONCUR_READ_ONLY);
        stmt.setInt(1, m.getId());
        stmt.setInt(2, i.getId());
        stmt.executeUpdate();
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }
  }