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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access database using jdbc error

Status
Not open for further replies.

darine

Programmer
Jun 23, 2000
28
0
0
US
Hi All <br>I'm trying to access database using jdbc and i'm gettin this error when i compile java file.<br>here is my simple java file:<br>import java.sql.*;<br><br>public&nbsp;&nbsp;class myJdbc<br>{<br> String url=&quot;jdbc:eek:dbc:sample&quot;;<br> String query= &quot;SELECT * FROM PERSON &quot;;<br> boolean more;<br> Statement stmt ;<br><br><br> try<br> {<br> Class.forName( &quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);<br> Connection con =DriverManager.getConnection(url,&quot;matt&quot;,&quot;matt&quot;);<br> stmt= con.createStatement();<br> ResultSet rs = stmt.executeQuery(query);<br><br> while(more = rs.next())<br> {<br> int number = rs.getInt(&quot;PERSON#&quot;);<br> String firstName = rs.getString(&quot;FIRST&quot;);<br> String lastName = rs.getString(&quot;LAST&quot;);<br> System.out.println(number + &quot; &quot; + firstName + &quot; &quot; + lastName);<br><br> }<br> rs.close();<br> stmt.close();<br> con.close();<br><br> }<br> catch(SQLException ex )<br> {<br> ex.printStackTrace();<br> }<br><br>}<br><br>The error is:<br>C:\WINNT\PROFILES\mseif\Desktop\myJdbc.java:12: Type expected.<br> try<br> ^<br>1 error<br><br>Tool completed with exit code 1<br><br>i registered sample.mdb as an odbc data source.<br>i will appreciate your help.<br>thanks.<br>
 
Hi darine,<br><br>It appear to me that your <i>try,catch</i> block is not in a method. You need to place your code in a method or in a contructor for the class. Thats why it doesn't recognise the <i>try</i> statement.<br><br>HTH <p>Cal<br><a href=mailto: > </a><br><a href= > </a><br>
 
Thanks ,you are right,but i fixed it and i'm still getting this error:<br>Exception java.lang.ClassNotFoundException must be caught, or it must be declared in the throws clause of this constructor.<br>Class.forName <br>sometime in this statment :<br>Class.forName( &quot;microsoft.jdbc.odbc.JdbcOdbcDriver&quot;);<br>I put instead microsoft sun and i still getting the same error.<br>here the java file:<br><br>import java.sql.*;<br>import java.lang.*;<br><br><br>public class myJdbc<br>{<br><br>public myJdbc()<br>{<br><br>String url=&quot;jdbc dbc:sample&quot;;<br>String query= &quot;SELECT * FROM PERSON &quot;;<br>//boolean more;<br>Statement stmt ;<br><br><br>try<br>{<br>Class.forName( &quot;microsoft.jdbc.odbc.JdbcOdbcDriver&quot;);<br>Connection con =DriverManager.getConnection(url,&quot;matt&quot;,&quot;matt&quot;);<br>stmt= con.createStatement();<br>ResultSet rs = stmt.executeQuery(query);<br><br>//while(more = rs.next())<br>while( rs.next())<br>{<br>int number = rs.getInt(&quot;PERSON#&quot;);<br>String firstName = rs.getString(&quot;FIRST&quot;);<br>String lastName = rs.getString(&quot;LAST&quot;);<br>System.out.println(number + &quot; &quot; + firstName + &quot; &quot; + lastName);<br><br>}<br>rs.close();<br>stmt.close();<br>con.close();<br><br>}<br>catch(SQLException ex )<br>{<br>ex.printStackTrace();<br>}<br><br>}<br>public static void main(String args[])<br>{<br>myJdbc my = new myJdbc();<br>}<br>}<br>thanks for your time.<br><br>
 
Thanks for your help.<br>it is working now.<br>thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top