File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Database update Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Database update" Watch "Database update" New topic
Author

Database update

john omeara
Greenhorn

Joined: Nov 12, 2003
Posts: 15
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

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 ]

GenRocket - Experts at Building Test Data
john omeara
Greenhorn

Joined: Nov 12, 2003
Posts: 15
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

Joined: Jul 09, 2001
Posts: 1879

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
 
subject: Database update