示例#1
0
  public void addStore(Store toAdd) {
    connect();
    try {
      // Create a stmt object
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

      // Query the database, storing the result
      // in an object of type ResultSet
      rs =
          stmt.executeQuery(
              "SELECT * from Store WHERE "
                  + "Name='"
                  + toAdd.getName()
                  + "' AND "
                  + "Address='"
                  + toAdd.getAddress()
                  + "'");

      // Check if User is already in the database
      while (rs.next()) {
        System.out.println("Store already exists in DB.");
        disconnect();
        return;
      } // end while loop

      stmt.executeUpdate(
          "INSERT INTO Store(Name, Address) VALUES ('"
              + toAdd.getName()
              + "', '"
              + toAdd.getAddress()
              + "')");
    } catch (Exception e) {
      e.printStackTrace();
    } // end catch
    finally {
      disconnect();
    }
  }