Hi All I'm trying to access database using jdbc and i'm gettin this error when i compile java file. here is my simple java file: import java.sql.*; public class myJdbc { String url="jdbc dbc:sample"; String query= "SELECT * FROM PERSON "; boolean more; Statement stmt ;
try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); Connection con =DriverManager.getConnection(url,"matt","matt"); stmt= con.createStatement(); ResultSet rs = stmt.executeQuery(query); while(more = rs.next()) { int number = rs.getInt("PERSON#"); String firstName = rs.getString("FIRST"); String lastName = rs.getString("LAST"); System.out.println(number + " " + firstName + " " + lastName); } rs.close(); stmt.close(); con.close(); } catch(SQLException ex ) { ex.printStackTrace(); } } The error is: C:\WINNT\PROFILES\mseif\Desktop\myJdbc.java:12: Type expected. try ^ 1 error Tool completed with exit code 1 i registered sample.mdb as an odbc data source. i will appreciate your help. thanks.
Originally posted by enirad: Hi All I'm trying to access database using jdbc and i'm gettin this error when i compile java file. here is my simple java file: import java.sql.*; public class myJdbc { String url="jdbc dbc:sample"; String query= "SELECT * FROM PERSON "; boolean more; Statement stmt ;
try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); Connection con =DriverManager.getConnection(url,"matt","matt"); stmt= con.createStatement(); ResultSet rs = stmt.executeQuery(query); while(more = rs.next()) { int number = rs.getInt("PERSON#"); String firstName = rs.getString("FIRST"); String lastName = rs.getString("LAST"); System.out.println(number + " " + firstName + " " + lastName); } rs.close(); stmt.close(); con.close(); } catch(SQLException ex ) { ex.printStackTrace(); } } The error is: C:\WINNT\PROFILES\mseif\Desktop\myJdbc.java:12: Type expected. try ^ 1 error Tool completed with exit code 1 i registered sample.mdb as an odbc data source. i will appreciate your help. thanks.
I hope this will help you. In looking at your code and your error, I didn't see a "public static void main(String args[])". This is possibly why you are getting the error at your "try" statement. The other thing I noticed was a boolean more. I believe that all you need is "while (rs.next())". This will get all the records one at a time and process them in your program. If you have a copy of "Just Java 2", look on page 451. If you don't have a copy of this GREAT book, you should get one. I hope I have been of some help. Good Luck!
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Thanks you are right,it was a big mistake because i forget to put main(). but i fixed and i still getting error: C:\WINNT\PROFILES\mseif\Desktop\myJdbc.java:18: Exception java.lang.ClassNotFoundException must be caught, or it must be declared in the throws clause of this constructor. Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); Thanks again for your time.
Move this line: Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); out of the try block and put it in it's own try block above the existing one. try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException e) { System.out.println(e); } Matt
Originally posted by enirad: Thanks you are right,it was a big mistake because i forget to put main(). but i fixed and i still getting error: C:\WINNT\PROFILES\mseif\Desktop\myJdbc.java:18: Exception java.lang.ClassNotFoundException must be caught, or it must be declared in the throws clause of this constructor. Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); Thanks again for your time.