• 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 databaseusing jdbc and 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
sometime in this statment :
Class.forName( "microsoft.jdbc.odbc.JdbcOdbcDriver");
I put instead microsoft sun and i still getting the same error.
here the java file:

import java.sql.*;
import java.lang.*;

public class myJdbc
{
public myJdbc()
{
String url="jdbc dbc:sample";
String query= "SELECT * FROM PERSON ";
//boolean more;
Statement stmt ;

try
{
Class.forName( "microsoft.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 for your time.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by enirad:
[B]Hi
-----------------------------------------------------------------
I'm trying to access databaseusing jdbc and 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
sometime in this statment :
Class.forName( "microsoft.jdbc.odbc.JdbcOdbcDriver");
I put instead microsoft sun and i still getting the same error.
here the java file:
-----------------------------------------------------------------There is a class in java called 'Class'. So must catch the exception that is thrown when 'Class' is not found.Otherwise instead of catching just SQLException, U can catch Exception' itself.
Hope this solves ur problem
---------------------------------------
Your String 'url' should read "jdbc dbc:sample"(maybe u missed the colon).

[This message has been edited by Srividya Shiv (edited August 01, 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!
The error means that the class "microsoft.jdbc.odbc.JdbcOdbcDriver" was not found in your class path.
Make sure of it's location, but I think it should be "sun" instead of "microsoft".

Originally posted by enirad:
[B]Hi All
I'm trying to access databaseusing jdbc and 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
sometime in this statment :
Class.forName( "microsoft.jdbc.odbc.JdbcOdbcDriver");
I put instead microsoft sun and i still getting the same error.
here the java file:

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to add the "}catch(ClassNotFoundException" to your program.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please use code-tags?

I don't respond to unreadable code anymore. I only get angry.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angel,
Note that the original post is 4 years old. I doubt the poster still needs an answer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic