• 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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot
it is working now.
thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic