aspose file tools*
The moose likes Java in General and the fly likes Access database using JDBC error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Access database using JDBC error" Watch "Access database using JDBC error" New topic
Author

Access database using JDBC error

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 30, 2000
Posts: 18671
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).]


"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 30, 2000
Posts: 18671
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access database using JDBC error