This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Access database using JDBC error  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the problem has nothing to do with JDBC. It's just that you have a bunch of statements, beginning with the try, which are not contained in any method. A class can contain field declarations, methods, constructors, and static or instance initializers, but a class can't contain a statement unless it's enclosed in one of the previous constructs. The likely solution you'll want is to put all those statements inside one or more methods.
[This message has been edited by Jim Yingst (edited July 30, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
thanks for your reply.
i fprgot to put main().
now i'm getting this error:
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");
here is the java file:
import java.sql.*;
public class myJdbc
{
public 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())
while( 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();
}
}
public static void main(String args[])
{
myJdbc my = new myJdbc();
}
}
Thanks again for your time.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This error is just what it says: the method Class.forName() can throw the checked exception ClassNotFoundException. Therefore to use it, you must either (a) put it inside a try block, and catch the ClassNotFoundException, or (b) add "throws ClassNotFoundException" to the declaration of the method which calls it, namely main() in this case.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!