0

My app allows users to create an account (stored in database) and place orders.
When a client registers himself, I want to generate a primary key named CLIENT_CODE to identify him, starting from x value and increment it with y value. (I'm using oracle 11g atm)
I've tried this so far:

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String fname = jTextField9.getText();
        String lname = jTextField10.getText();
        String city = jTextField11.getText();
        String street = jTextField13.getText();
        String number = jTextField14.getText();
        String userClient = jTextField15.getText();
        String pass1 = String.valueOf(jPasswordField5.getPassword());
        String pass2 = String.valueOf(jPasswordField6.getPassword());

        if(verifyFields()){
            if(!checkUsername(userClient)){
                OraclePreparedStatement ps;
                OracleResultSet rs;
                String registerClient = "insert into CLIENT (FNAME_CL, LNAME, CITY, STREET, NUMBER, MONEY, CLIENT_CODE, USER_CLIENT, PASS) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";

                try {
                    ps = (OraclePreparedStatement) JavaConnectDb.ConnectDb().prepareStatement(registerClient);
                    ps.setString(1, fname);
                    ps.setString(2, lname);
                    ps.setString(3, city);
                    ps.setString(4, street);
                    ps.setString(5, number);
                    ps.setDouble(6, 0.0);
                    ps.setInt(7, ???); <--- here should be the generated primary key
                    ps.setString(8, userClient);
                    ps.setString(9, pass1);

                    if(ps.executeUpdate() != 0){
                        JOptionPane.showMessageDialog(null, "Account created!");
                    } else{
                        JOptionPane.showMessageDialog(null, "Error: Check your info");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        }
    } 
2
  • Start your Client database entry with a CLIENT_CODE of x then on any future Client entries, merely retrieve the CLIENT_CODE used in the last (previous) database entry and increment it by y. Commented Nov 28, 2019 at 12:16
  • @DevilsHnd Don't do that; if two entries are made (nearly) simultaneously then they will both request the latest CLIENT_CODE and will both try to set the same value. You need a way to atomically generate unique values. Commented Nov 28, 2019 at 12:45

1 Answer 1

1

Don't do it in Java; handle the primary key value creation in the database using a sequence:

CREATE SEQUENCE CLIENT__CLIENT_CODE__SEQ
START WITH 1
INCREMENT BY 1

Then just use your sequence in the INSERT statement and use the RETURNING clause to get the generated value as an OUT parameter of your prepared statement.

insert into CLIENT (
  FNAME_CL,
  LNAME,
  CITY,
  STREET,
  NUMBER,
  MONEY,
  CLIENT_CODE,
  USER_CLIENT,
  PASS
) values (
  ?,
  ?,
  ?,
  ?,
  ?,
  ?,
  CLIENT__CLIENT_CODE__SEQ.NEXTVAL,
  ?,
  ?
) RETURNING CLIENT_CODE INTO ?

If you were using Oracle 12c then you could use GENERATED AS IDENTITY in the table's CREATE DDL statement to generate the values without creating a separate sequence.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.