• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access database using JDBC error

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic