File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database update

 
john omeara
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • 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 );
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • 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 )
{
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic