Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JDBC class not fully operational

Status
Not open for further replies.

wduty

Programmer
Jun 24, 2000
271
US
I have a class set up which simply inserts into a database. When I call this class repeatedly in a loop it works ok but when I call it once it doesn't perform the insert. Calling a short loop and placing the loop index in the database, I can see that it seems not to be performing the LAST insert. For example if I write <br><br>for (int i=0;i&lt;4;i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;{call insert class}<br><br>I see in the database column containing the i value:<br><br>0<br>1<br>2<br><br>But NOT 3.<br><br>This means that a single call, which is also the <i>last</i> call, does nothing. Here is the class:<br><br><font color=red><br>import java.util.*;<br>import java.sql.*;<br><br>public class searchJDBC <br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Connection connobj = null;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Statement stmtobj = null;<br> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void setConnection(String DBstr)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connobj = DriverManager.getConnection(&quot;jdbc:eek:dbc:&quot;+DBstr);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stmtobj = connobj.createStatement();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(ClassNotFoundException e) {e.getMessage();} <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(SQLException e) {System.out.println(e.getMessage());} <br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;public void insertSQLstr(String SQLstr)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stmtobj.executeUpdate(SQLstr);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(SQLException e) {System.out.println(e.getMessage());}<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br> <br> }</font><br><br>&nbsp;There's hardly anything to this and it does work but only under repeated calls. It must be something really simple. Any help appreciated. <p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br>
 
Actually I made a mistake in the above presentation which was an earlier version of the code. Here's the class:<br><br><br>import java.util.*;<br>import java.sql.*;<br><br>public class searchJDBC <br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Connection connobj = null;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Statement stmtobj = null;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void setConnection(String DBstr)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connobj = DriverManager.getConnection(&quot;jdbc:eek:dbc:&quot;+DBstr);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch(ClassNotFoundException e) {e.getMessage();} <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch(SQLException e) {System.out.println(e.getMessage());} <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void insertQuery(String url,String keywords)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stmtobj = connobj.createStatement();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String SQLstr = &quot;INSERT INTO keywords (url,keywords) VALUES ('&quot;+url+&quot;','&quot;+keywords+&quot;')&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stmtobj.executeUpdate(SQLstr);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stmtobj.close();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch(SQLException e) {System.out.println(e.getMessage());}<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br> <br> } <p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br>
 
what program are you using for debugging? it would be helful to step through and see what's going on... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Actually I wasn't using a debugger. Any you might recommend? <p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br>
 
depends on what OS you're running on and how much money you're willing to spend. I use the Symantec Visual Cafe IDE, but it has it's share of issues (as just about any program will if you use it enough to discover them all). It's really up to you.<br><br>With regards to your program... I don't understand how this line would execute as you don't have the <FONT FACE=monospace>url, keywords</font> outside of quotes.<br><br>String SQLstr = &quot;INSERT INTO keywords (url,keywords) VALUES ('&quot;+url+&quot;','&quot;+keywords+&quot;')&quot;;<br><br>should be<br><br>String SQLstr = &quot;INSERT INTO keywords (&quot; + url + &quot;,&quot; + keywords + &quot;) VALUES ('&quot;+url+&quot;','&quot;+keywords+&quot;')&quot;;<br><br>I think... unless I'm misunderstanding your program/database structure. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
I'm running windows 98 (it's a local test project for a potential customer that might want some specific searching capabilities for certain URLs). I do have Visual Studio Enterprise 6 which I assume comes with some kind of java debugger although I haven't installed the j++ part of VS. (The thing comes with a pile of CDs). Do you know anything about this? <br><br>As for the SQL syntax, the values in the first set of parens are the <i>column names</i> which aren't variable (one column is called &quot;url&quot; and the other is called &quot;keywords&quot;). It might look confusing because the variables containing the string literals to be inserted happen to have the same names as the column in the database. There are examples of this syntax at:<br><br><A HREF=" TARGET="_new"> HREF=" TARGET="_new"> HREF=" TARGET="_new"> HREF=" TARGET="_new"> <p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br>
 
Hi,<br><br>Just thinking, but if its insert all but the last record then it could be something to do with SQL Commit.&nbsp;&nbsp;The subsequent transactions could be forcing the COMMIT on the previous statement. Maybe the fact that you code does not explicitly close connection gracefully (no offence!!!) could be causing ROLLBACK of the SQL update for the last statement ... <p>Cal<br><a href=mailto: > </a><br><a href= > </a><br>
 
Well, this last suggestion seems to have solved the problem. When I first wrote this class I had a line in the method insertSQLstr() which closed the database immediately after the executeUpdate statement.<br><br><font color=red>...<br>stmtobj.executeUpdate(SQLstr);<br>connobj.close();<br>...</font><br><br>But I thought that perhaps the close statement was causing the problem somehow so I took it out, but that didn't make any difference.<br><br>But now that &quot;gracefully&quot; was mentioned I thought maybe I should add a <i>separate method</i> to my searchJDBC class which just closes the connection like this:<br><br><font color=red>public void closeConnection()<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;try{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connobj.close();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;catch(SQLException e) {System.out.println(e.getMessage());} <br>&nbsp;&nbsp;&nbsp;&nbsp;}</font><br><br>This seems to have solved the problem. Now I can make a single insert by invoking the class as follows:<br><br><font color=red>searchJDBC insert = new searchJDBC();<br>insert.setConnection(&quot;javatest&quot;);<br>insert.insertSQLstrDBstr(&quot;INSERT INTO test (test) VALUES ('testing ok')&quot;);<br>insert.closeConnection();</font><br><br>I'm used to working with ADO so I'm not familiar with the particular idiosyncrasies of java JDBC programming though I'm finding it pretty cool so far. Thanks a lot Cal and Liam for your time and suggestions!<br><br>I have another question also. I see that there are a lot of useful looking methods in the java2 class specification at sun for the SQL classes. But most of them are marked JDBC2.0 and when I try to call any of these (like resultset.isLast() or resultset.first()) I get a method not found error. I'm using jdk1.2.2 on windows98. Do either of you know what I have to do to use JDBC2.0?<br><br> <p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br>
 
Hmm... as you can see at [ <A HREF=" TARGET="_new"> ], JDBC2.0 was released with the JDK 1.2. Might you possibly have another older JDK in your classpath? How long ago did you download the JDK?<br><br>For more interesting JDBC classes, you might want to take a look at the standard extension classes... The javax.sql package has some more useful stuff. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
I downloaded the jdk1.2.2 about four or five months ago. I also downloaded microsoft's java SDK though that's in another directory entirely so I can't see how that would be affecting it. I'll go back and see what exactly the errors were that I was getting and make sure it's not something else.<br>I'll also take a look at the javax.sql package. <br><br>Thanks. <p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br>
 
try this:<br><br>echo %PATH%<br><br>If your path includes both directories, there might be a conflict. You might want to try downloading it again? Depends on the speed of your connection- if you have a fast connection, that might not be a big deal, but if you have a slower connection, I'd definitely check other options out first.<br><br>Just curious... I haven't dealt with the MS JDK, what does it have that the Sun JDK doesn't have? <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
I'll give the echo %PATH% a shot. Otherwise, I'm on a pretty fast DSL so I can redownload the jdk if all else fails.<br><br>As to the MS java, I didn't really explore it much but the one thing it has is a program called javareg.exe which registers a java class as a COM object. I didn't actually get it to work because although it apparently registered the class ok, I then got a permission error in the calling asp page and I was too busy to work on it further. If I can get to know jsp better it seems like it would be easier to just use that than fiddle around with registering asp components. <p>--Will Duty<br><a href=mailto:wduty@radicalfringe.com>wduty@radicalfringe.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top