This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
 
Similar Threads
More Simplified version of the question bellow !
Dialog Box Problem
help for jdbc
HTTP Status 405 - HTTP method GET is not supported by this URL
Problem with comparing results in jdbc?