Solved

How to Update a Record from a database?

Posted on 2003-11-27
5
264 Views
Last Modified: 2010-03-31
hie, i have a very simple question, i am a java beginner, i am able to add a record to a database from my java which i am using MS Access as my database, i am able to add a record by using this code...
public void add() { // Add New Guest to Database
      
       String a = super.getLastName();
       String b = super.getFirstName();
       String c = address.getAddress();
       String d = address.getCity();
       String e = address.getPostCode();
       String f = address.getState();
       String g = address.getCountry();
       String h = super.getPhone();
       String i = super.getFax();
       String j = super.getRemarks();

       openConnection();
        
        try
             {
                   Statement statement = connection.createStatement();
                               
                  String query = "INSERT INTO guest (guest_ID,lastname,firstname,address," +
                                       " city,postcode, state, country, phone," +
                                        " fax, remarks) VALUES ('" +
                                                a +"','" +
                                                b +"','" +
                                             c +"','" +
                                             d +"','" +
                                             e +"','" +
                                             f +"','" +
                                             g +"','" +
                                             h +"','" +
                                             i +"','" +
                                             j +"')";
                        
                  int result = statement.executeUpdate(query);
                  
                  if (result == 1) {
                         JOptionPane.showMessageDialog(null, "Insertion succesfull");
                   } else {
                         JOptionPane.showMessageDialog(null, "Insertion Not succesfull");
                   }
      
                   statement.close();
            }
         catch(SQLException sqlex) {
       
         }
       }


and now i want to retrieve back my record and i am searching by the guest_id, so when i want to update the cord, what should i write ?please give me some guide

it should be write as this
public void update() {

//the code
}
0
Comment
Question by:kennykoid
  • 3
5 Comments
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9835286
something like:

            String query = "UPDATE addresses SET " +
                   "firstname='" + fields.first.getText() +
                   "', lastname='" + fields.last.getText() +
                   "', address='" + fields.address.getText() +
                   "', city='" + fields.city.getText() +
                   "', stateorprovince='" +
                   fields.state.getText() +
                   "', postalcode='" + fields.zip.getText() +
                   "', country='" + fields.country.getText() +
                   "', emailaddress='" +
                   fields.email.getText() +
                   "', homephone='" + fields.home.getText() +
                   "', faxnumber='" + fields.fax.getText() +
                   "' WHERE id=" + fields.id.getText();
                   output.append( "\nSending query: " +
                   connection.nativeSQL( query ) + "\n" );

            int result = statement.executeUpdate( query );
           
            if ( result == 1 )
               output.append( "\nUpdate successful\n" );
0
 
LVL 3

Accepted Solution

by:
InNoCenT_Ch1ld earned 20 total points
ID: 9835301
that's an example for you.
you can do like this:

               String query = "UPDATE guest SET" +
                                     "lastname='" + b +"' ," +
                                     "firstname="' + c +"' ," +
                                     //..and so on +
                                     "' WHERE guest_ID="a;

               int result = statement.executeUpdate(query);
               if(result == 1)
                    //update successful...(your code here)
                                   

hope it helps ;)
-Child-
0
 

Expert Comment

by:KarthikAnandan
ID: 9835380
Hi,
  First you have to search whether the given guest_id is present or not. Then only u can update a record.

      String a = super.getLastName();
      String b = super.getFirstName();
      String c = address.getAddress();
      String d = address.getCity();
      String e = address.getPostCode();
      String f = address.getState();
      String g = address.getCountry();
      String h = super.getPhone();
      String i = super.getFax();
      String j = super.getRemarks();


Now to Search for the given guest_id

the query will be
        String query =  "select 1 from guest where guest_ID = '"+a+"'

If this query returns 1. then the Guestid is present. Now you have to update the table. for this
the query will be
     
"UPDATE guest set firstname = '"+a+"', lastname = '"+b+"', address = '"+c+"',city='"+d+"',
                        stateorprovince='"+e+"', postalcode = '"+f+"',country = '"+g+"',
                        emailaddress = '"+h+"',homephone = '"+i+"', faxnumber = '"+j+"' WHERE
                         guest_id = a;

this query will update the table with the specified guest id.

Now, If the first query, that is checking for guest_id in the table, if this fails then that means that the record is not present. The guest id is not present. So we have to go for inserting a new Guest rather than updating. The insert query is the same as what you said.
If you have any doubt over this then reply plz..

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9835743
>> First you have to search whether the given guest_id is present or not. Then only u can update a record.

He says:

>> i want to retrieve back my record and i am searching by the guest_id,

Anyway, kennykoid, is it simply the update part which is troubling you or something else? If you're being able to insert a row, then I don't see a problem with updating it - merely, the query will change in your add () code. Is it something else that you want to know?

Mayank.
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9835794
KarthikAnandan>> hello, you're just repeating my comment...but more complete. so i will hate you for the rest of my life if i dont get the pts.. hehehee ;p (just kidding)

-Child-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Core Java. What output will be and why ? 6 64
java  and programming certification ? 4 86
eclipse java  build path 6 43
object oriented programming comparison 5 52
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now