This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Updating if file exists...

 
Michael Winner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Sheriff
Posts: 35977
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Winner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? Does that mean I need to also create a second database connection? Oh, yeah.. thanks for the reply
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35977
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the same database connection. For example,
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35977
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg,
I forgot that some drivers let you do that. Sorry for the confusion.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!