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 Updating if file exists... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Updating if file exists..." Watch "Updating if file exists..." New topic
Author

Updating if file exists...

Michael Winner
Greenhorn

Joined: Sep 15, 2003
Posts: 4
Hello!
I've been working on this little problem for a while. I'm simply checking a database table to see if a record exists. If it does, it gets updated. If it doesn't, a new record is created. Can somebody lend a hand with the code? Thanks for any help!
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc dbc B", "bob", "jim");
String strSQL = "SELECT ID, FName, Lame FROM tblContacts WHERE ID = " + UserID;

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(strSQL);
if(!rs.next()){ //If record does not exist
rs.moveToInsertRow();
rs.updateInt("FName", FName);
rs.updateInt("LName", LName);
rs.insertRow();
}else{
rs.next();
rs.updateInt("FName", FName);
rs.updateInt("LName", LName);
rs.updateRow();
}
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

You need two SQL statements to do this. The first is to check if it is there. If it is there, you need a second to update the data. If not, you need a second to insert the data.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Michael Winner
Greenhorn

Joined: Sep 15, 2003
Posts: 4
Really? Does that mean I need to also create a second database connection? Oh, yeah.. thanks for the reply
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

You can use the same database connection. For example,
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

I don't think you need 2 SQL statements. I could be wrong.
Are you getting errors? What isn't working? We need more information


GenRocket - A Test Data Generation Platform
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

Gregg,
I forgot that some drivers let you do that. Sorry for the confusion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating if file exists...
 
Similar Threads
Problem with ejb transaction
Query database
null pointer..
Logic:iterate
search