• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database update

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I have created the following code to update a paticular mysql record. Could any one inform me of how if the record is not present to add it to the database.
Thanks - heres the relevant code

try {
String query1 = "UPDATE system SET cox='"+first+"', def='"+second+"' WHERE name_d='"+name+"'";
System.out.println(query1);
Statement state1 = connect.createStatement();
ResultSet resultset1=state1.executeQuery(query1);
JOptionPane.showMessageDialog(null,"Database updated: ");
}
catch (Exception exp)
{
JOptionPane.showMessageDialog(null,"Problem with SQL query" + input );
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An update is not a query. Try executeUpdate() instead of executeQuery(). And what are you hoping to return to a ResultSet from an Update? Anyway, executeUpdate doesn't return a ResultSet.
[ February 16, 2004: Message edited by: Gregg Bolinger ]
 
john omeara
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry that resultset snippet can be taken out - i just copied and pasted it from a previous select statement. Is there anyway of seeing if there is no record to update then to enter the update statement as a fresh record.
john
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by john omeara:
Sorry that resultset snippet can be taken out - i just copied and pasted it from a previous select statement. Is there anyway of seeing if there is no record to update then to enter the update statement as a fresh record.
john


exectute update returns an int which represents the number of rows updated.
[code]
int rows_updated = statement.executeUpdate( update_string );
if( rows_updated == 0 )
{
reply
    Bookmark Topic Watch Topic
  • New Topic