コード例 #1
0
  // insertStamkund takes six strings as input and calls randomNumber for an integer
  public int insertStamkund(
      String pnrParam,
      String fnameParam,
      String snameParam,
      String addrParam,
      String mailParam,
      String cellParam) {

    // Call randomNumber() and stores the value to cardParam.
    int cardParam = randomNumber(1000, 9999);

    // Set the SQL statement into the query variable
    String query =
        "INSERT INTO Stamkund (kortnummer,personnummer,förnamn,"
            + "efternamn,adress,epost,mobilnummer) VALUES (?,?,?,?,?,?,?)";

    try {
      // Create a statement associated to the connection and the query.
      // The new statement is placed in the variable stmt.
      PreparedStatement stmt = con.prepareStatement(query);

      // Provide the values for the ?'s in the SQL statement, from 1 to 7.
      stmt.setInt(1, cardParam);
      stmt.setString(2, pnrParam);
      stmt.setString(3, fnameParam);
      stmt.setString(4, snameParam);
      stmt.setString(5, addrParam);
      stmt.setString(6, mailParam);
      stmt.setString(7, cellParam);

      // Execute the SQL statement that is prepared in the variable stmt
      stmt.executeUpdate();

      // Close the variable stmt and release all resources bound to it
      stmt.close();

      // Commit the changes made to the database.
      con.commit();
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Error on Data Insert");
    }

    // Returns card number for user display
    return cardParam;
  }
コード例 #2
0
  // Method for generating and returning a randomized card number in the range of 1000-9999.
  // The range can be edited in the insertStamkund method.
  public int randomNumber(int min, int max) {

    // Local variables
    String query;
    ResultSet rs;
    PreparedStatement stmt;
    int internalNumber;

    // A random number is generated in the selected range min-max (1000-9999)
    Random rand = new Random();
    internalNumber = rand.nextInt((max - min) + 1) + min;

    try {

      // Set the SQL statement into the query variable
      // We check if our generated number exists in the database.
      query = "SELECT kortnummer FROM Stamkund WHERE kortnummer = ?";
      stmt = con.prepareStatement(query);
      stmt.setInt(1, internalNumber);

      // Execute the SQL statement that is prepared in the variable stmt
      // and store the result in the variable rs.
      // If we receive a ResultSet, boolean result is set to 1, and the generated number exists.
      rs = stmt.executeQuery();
      boolean result = rs.next();

      // If result is true, we call ourselves for a new number
      if (result) {
        return randomNumber(min, max);
      }

      // Close the variable stmt and release all resources bound to it
      stmt.close();
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Data Insert error");
    }
    // If result is false, returns the unique card number
    return internalNumber;
  }