Meaningless Drivel is fun!*
The moose likes Beginning Java 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
JavaRanch » Java Forums » Java » Beginning Java
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.
Eric Edwards
Ranch Hand

Joined: Feb 12, 2000
Posts: 60
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
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.
Matt Siegel
Ranch Hand

Joined: Jul 18, 2000
Posts: 55
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.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks alot
it is working now.
thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access database using JDBC error
 
Similar Threads
Accessing a Microsoft Access Database
Access database using JDBC error
connecting to ms access database
Problem when Retreiving from DataBase
Access database using JDBC error