public List<DNS53ResourceRecord> listResourceRecords(
     String tableName, String type, String name, String rdata) throws ErrorResponse {
   List<DNS53ResourceRecord> records = new LinkedList<DNS53ResourceRecord>();
   boolean whereFlag = false;
   if (tableName == null) {
     // TODO throw InternalError
   }
   String query = "select * from `" + tableName + "`";
   if (type != null && !type.equals("")) {
     type = "\'" + type + "\'";
     if (whereFlag) {
       // probably never reached:
       query += " AND rdtype = " + type;
     } else {
       query += " WHERE rdtype = " + type;
       whereFlag = true;
     }
   }
   if (name != null && !name.equals("")) {
     name = "\'" + name + "\'";
     if (whereFlag) {
       query += " AND name = " + name;
     } else {
       query += " WHERE name = " + name;
       whereFlag = true;
     }
   }
   if (rdata != null && !rdata.equals("")) {
     rdata = "\'" + rdata + "\'";
     if (whereFlag) {
       query += " AND rdata = " + rdata;
     } else {
       query += " WHERE rdata = " + rdata;
       whereFlag = true;
     }
   }
   query += ";";
   // now query is created
   try {
     Statement stmt = this.sqlConnection.createStatement();
     ResultSet rs = stmt.executeQuery(query);
     while (rs.next()) {
       DNS53ResourceRecord temp = new DNS53ResourceRecord();
       temp.setName(rs.getString("name"));
       temp.setTtl(rs.getLong("ttl"));
       temp.setRdtype(rs.getString("rdtype"));
       temp.setRdata(rs.getString("rdata"));
       records.add(temp);
     }
   } catch (SQLException e) {
     e.printStackTrace();
     throw DNS53Faults.InternalError();
   }
   return records;
 }
  @Override
  public List<DNS53ResourceRecord> listResourceRecords(
      Session sess,
      String zoneId,
      String name,
      String value,
      String type,
      String sid,
      long ttl,
      long weight)
      throws ErrorResponse {
    List<DNS53ResourceRecord> records = new LinkedList<DNS53ResourceRecord>();

    String zoneName = getZoneName(zoneId);
    if (zoneName.equals("FAILED")) {
      throw DNS53Faults.NoSuchHostedZone(zoneId);
    }

    String query = "SELECT * FROM `msi`.`rrSet` WHERE zoneName = \'" + zoneName + "\'";
    if (type != null && !type.equals("")) {
      type = "\'" + type + "\'";
      query += " AND rdtype = " + type;
    }
    if (name != null && !name.equals("")) {
      String nameDot = name;
      if (name.charAt(name.length() - 1) != '.') {
        nameDot += ".";
      } else {
        name = name.substring(0, name.length() - 1);
      }
      name = "\'" + nameDot + "\'";
      query += " AND name = " + name;
    }
    if (value != null && !value.equals("")) {
      value = "\'" + value + "\'";
      query += " AND rdata = " + value;
    }
    if (sid != null && !sid.equals("")) {
      sid = "\'" + sid + "\'";
      query += " AND sid = " + sid;
    }
    if (ttl != -1) {
      query += " AND ttl = " + ttl;
    }
    if (weight != -1) {
      query += " AND weight = " + weight;
    }
    query += ";";
    // now query is created
    try {
      Statement stmt = this.sqlConnection.createStatement();
      ResultSet rs = stmt.executeQuery(query);
      while (rs.next()) {
        DNS53ResourceRecord temp = new DNS53ResourceRecord();
        temp.setName(rs.getString("name"));
        temp.setTtl(rs.getLong("ttl"));
        temp.setRdtype(rs.getString("rdtype"));
        temp.setRdata(rs.getString("rdata"));
        temp.setSid(rs.getString("sid"));
        temp.setWeight(rs.getLong("weight"));
        records.add(temp);
      }
    } catch (SQLException e) {
      e.printStackTrace();
      throw DNS53Faults.InternalError();
    }
    return records;
  }
  /**
   * Populate the new hosted zone data into MySQL database
   *
   * @param domainName name of the hosted zone
   * @param callerRef unique caller reference for this request
   * @param comment user's comment for this request
   * @return ID of the new hosted zone (automatically issued); "DUPLICATE_NAME" is returned if
   *     target name already exists
   * @throws InternalErrorException
   */
  @Override
  public List<String> createHostedZone(
      final Session session, String domainName, String callerRef, String comment, long acid)
      throws ErrorResponse {
    List<String> result = new LinkedList<String>();
    // String tableName = acid + callerRef;
    String tableName = "B" + UUID.randomUUID().toString().replace("-", "").toUpperCase();
    if (callerReferenceIsUsed(callerRef, acid)) {
      result.add("DUPLICATE_REFERENCE");
      return result; // response: signal that the name is already in use
    }
    String domainNameDot = domainName;
    if (domainName.charAt(domainName.length() - 1) != '.') {
      domainNameDot += ".";
    } else {
      domainName = domainName.substring(0, domainName.length() - 1);
    }

    if (domainExists(domainNameDot, acid)) {
      result.add("DUPLICATE_NAME");
      return result;
    }
    String query =
        "CREATE TABLE `"
            + tableName
            + "` ("
            + "		  name varchar(255) default NULL,"
            + "		  ttl int(11) default NULL,"
            + "		  rdtype varchar(255) default NULL,"
            + "		  rdata varchar(255) default NULL"
            + "		) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
    try {
      Statement stmt = this.sqlConnection.createStatement();
      stmt.executeUpdate(query);
      stmt.close();
    } catch (SQLException e) {
      System.err.println("Failed to create a new schema in MySQL database.");
      e.printStackTrace();
      throw DNS53Faults.InternalError(); // just put some bogus UUID
      // unless RequestID becomes
      // important this layer
    }
    // insert this new ID-name mapping entry including caller reference and
    // comment
    String uniqueID = "Z" + UUID.randomUUID().toString().replace("-", "").toUpperCase();
    final DNS53HostedZone zone = new DNS53HostedZone();
    zone.setId(uniqueID);
    zone.setName(domainNameDot);
    zone.setCallerRefernce(callerRef);
    zone.setComment(comment);
    zone.setAccount(acid);
    zone.setTableName(tableName);
    session.save(zone);

    addRecord(
        tableName,
        domainName,
        259200,
        "SOA",
        domainName + ". hostmaster." + domainName + ". 200309181 28800 7200 86400 28800");
    DNS53ResourceRecord recordSOA = new DNS53ResourceRecord();
    recordSOA.setName(domainNameDot);
    recordSOA.setTtl(259200);
    recordSOA.setRdata(
        domainName + ". hostmaster." + domainName + ". 200309181 28800 7200 86400 28800");
    recordSOA.setRdtype("SOA");
    recordSOA.setZoneId(uniqueID);
    recordSOA.setZoneName(domainNameDot);
    session.save(recordSOA);

    addRecord(tableName, domainName, 259200, "NS", "ns0." + domainName + ".");
    DNS53ResourceRecord recordNS0 = new DNS53ResourceRecord();
    recordNS0.setName(domainNameDot);
    recordNS0.setTtl(259200);
    recordNS0.setRdata("ns0." + domainName + ".");
    recordNS0.setRdtype("NS");
    recordNS0.setZoneId(uniqueID);
    recordNS0.setZoneName(domainNameDot);
    session.save(recordNS0);

    addRecord(tableName, domainName, 259200, "NS", "ns1." + domainName + ".");
    DNS53ResourceRecord recordNS1 = new DNS53ResourceRecord();
    recordNS1.setName(domainNameDot);
    recordNS1.setTtl(259200);
    recordNS1.setRdata("ns1." + domainName + ".");
    recordNS1.setRdtype("NS");
    recordNS1.setZoneId(uniqueID);
    recordNS1.setZoneName(domainNameDot);
    session.save(recordNS1);

    final String nameserverIp =
        (String) ConfigurationUtil.getConfiguration(Arrays.asList(new String[] {"DNS_IP"}));
    String ns0name = "ns0." + domainName;
    String ns1name = "ns1." + domainName;
    String ns0nameDot = "ns0." + domainNameDot;
    String ns1nameDot = "ns1." + domainNameDot;
    addRecord(tableName, ns0name, 259200, "A", nameserverIp);
    DNS53ResourceRecord recordA0 = new DNS53ResourceRecord();
    recordA0.setName(ns0nameDot);
    recordA0.setTtl(259200);
    recordA0.setRdata(nameserverIp);
    recordA0.setRdtype("A");
    recordA0.setZoneId(uniqueID);
    recordA0.setZoneName(domainNameDot);
    session.save(recordA0);

    addRecord(tableName, ns1name, 259200, "A", nameserverIp);
    DNS53ResourceRecord recordA1 = new DNS53ResourceRecord();
    recordA1.setName(ns1nameDot);
    recordA1.setTtl(259200);
    recordA1.setRdata(nameserverIp);
    recordA1.setRdtype("A");
    recordA1.setZoneId(uniqueID);
    recordA1.setZoneName(domainNameDot);
    session.save(recordA1);

    result.add(uniqueID);
    result.add(tableName);
    result.add(ns0name);
    result.add(ns1name);

    session.save(zone);

    return result;
  }