This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access database using JDBC error

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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.
 
Srividya Shiv
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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
  • 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:
 
angel aracena
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to add the "}catch(ClassNotFoundException" to your program.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please use code-tags?

I don't respond to unreadable code anymore. I only get angry.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34196
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angel,
Note that the original post is 4 years old. I doubt the poster still needs an answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic