samedi 31 mars 2018

Creating a unique listing_no (Java/PostgreSQL)

I am working on a option in a Menu function that posts the car for the sale in a database. The option asks for the user to enter the year, make, condition and price, which is then inserted into the table car_sale in the database. However, a unique listing_no must also be generated during this option. I cannot define my tables to uniquely generate the 10 digit number the option but I must code the program to insert uniquely generated listing_no. Below you will find the code of me trying to do this, however the code only works in Oracle but I cannot use Oracle. I can only PostGreSQL and Java. Therefore, my problem arises as the functions and relations I am using cannot be used in PostGre.

Code to Generate Listing No:

 public int generateListingNo() throws SQLException
 {
  int listingSeq = 0;
  Statement select = connection.createStatement();
  result = select.executeQuery("select (to_char(sysdate,'yyyymmdd')||AUDIT_SEQ.NEXTVAL)valnext from dual");;
  if(result.next())
  {
     listingSeq = result.getInt(1);
  }

  int seq = listingSeq;

  return seq;
 }

Code in The Option Function to insert the lisitng_no generated from generateListingNo()

public void option() throws SQLException
   {
int listing_no = generateListingNo();
         // insert information into books_for_sale table
         sql_insert = "INSERT INTO car_sale VALUES(" + listing_no +", "
                       + "'" + year + "'" + ", " +
                       "'" + make + "'" +", " +
                       "'" + condition + "'" + ", "
                       + price + ")";




Aucun commentaire:

Enregistrer un commentaire