3 Replies - 591 Views - Last Post: 11 September 2013 - 02:12 PM Rate Topic: -----

#1 MahlanguNT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-August 13

I am trying to check if a PK exists if it does then i update

Posted 10 September 2013 - 06:13 AM

I am trying to check if a PK exists if it does then i update on that record
here is what i tried i don't know where is my mistake.help anyone

String strCode = jtxtCode.getText().toString();
            String sql = "Select * From job ";
            int id;
            int rID = 0;
            JobTitle = jtxtDesc.getText();

            ResultSet resultSet = null;
          
            

                id = Integer.parseInt(jtxtCode.getText().toString());
                resultSet = statement.executeQuery(sql);

                while (resultSet.next()) {
                    rID = resultSet.getInt("JobCode");
                    System.out.print(rID + " " + id);
                    System.out.print('\n');

                    if (rID == id) {


                        String sqlUPDATE = "UPDATE job SET JobDesc =  '" + JobTitle + "' WHERE JobCode = " + id + "";
                        statement.executeUpdate(sqlUPDATE);

                        JOptionPane.showMessageDialog(null,
                                "Record has been updated",
                                "Info",
                                JOptionPane.INFORMATION_MESSAGE);
                        
                        break;

                    } else {

                        if (resultSet.last()) 
                            JOptionPane.showMessageDialog(null,
                                    "Job Code not found in the database",
                                    "Info",
                                    JOptionPane.INFORMATION_MESSAGE);
                        
                    }
                }

                resultSet.close();



Is This A Good Question/Topic? 0
  • +

Replies To: I am trying to check if a PK exists if it does then i update

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: I am trying to check if a PK exists if it does then i update

Posted 10 September 2013 - 06:56 AM

Personally I would modify your SELECT statement to select the PK from the 'job' table WHERE your column equals that jobcode. If the value comes back with something then you have a PK and you do your update.. if it comes back with nothing then you have to do an insert.
Was This Post Helpful? 1
  • +
  • -

#3 MahlanguNT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-August 13

Re: I am trying to check if a PK exists if it does then i update

Posted 11 September 2013 - 12:32 AM

Thanks i will try that
Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: I am trying to check if a PK exists if it does then i update

Posted 11 September 2013 - 02:12 PM

Another approach is available, if your database is able to return the number of affected rows, or information on what was affected. PostgreSQL, for example, has the RETURNING clause, which can return data on the affected rows of a query.

If your DB does have a way of returning the affected rows, then you should be able to run the UPDATE query, and then put a conditional around your message dialog based on the results.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1