Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Calling classes

  Asked By: Joel    Date: Oct 10    Category: Java    Views: 682
  

I am trying to write a connection class to be able to connect to a data sourcr
however I
keep getting this error
"connection.java": unreported exception java.lang.ClassNotFoundException; must
be
caught or declared to be thrown at line 25, column 13
When I code the connection in the interface and use a button to connect I have
no
problems and cannot find an answer.
Here is my class. The error is in the line Class.forName(driver);
I appreciate any help I can get.

package database_application;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

// the original framework
// the event handler
import java.sql.*; // sql objects
import javax.swing.*; // the new widgets and components
import java.awt.*;

public class connection {
public connection() {
}

public void connect() {
try {
String driver = "com.borland.datastore.jdbc.DataStoreDriver";
Class.forName(driver);
String conn_string =
"jdbc:borland:dslocal:/Developer/Applications/JBuilderX/
JBuilder.framework/samples/JDataStore/datastores/employee.jds";
Connection db_connection = DriverManager.getConnection(conn_string,
"duncan", "");

}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "There has been an" +
" error connecting " + ex.toString(),
"Connection Error",
JOptionPane.YES_NO_OPTION);
}
}

}

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Teresa Rogers     Answered On: Oct 10

I am trying to write  a connection  class to be able to connect  to a data  sourcr
however I
keep getting this error
"connection.java": unreported exception  java.lang.ClassNotFoundException; must
be
caught or declared to be thrown at line  25, column  13
When I code  the connection in the interface  and use a button  to connect I have
no
problems and cannot find  an answer.
Here is my class. The error  is in the line Class.forName(driver);
I appreciate any help  I can get.

package database_application;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

// the original framework
// the event handler
import java.sql.*; // sql objects
import javax.swing.*; // the new widgets and components
import java.awt.*;

public class  connection {
public connection() {
}

public void connect() {
try {
String driver = "com.borland.datastore.jdbc.DataStoreDriver";
Class.forName(driver);
String conn_string =
"jdbc:borland:dslocal:/Developer/Applications/JBuilderX/
JBuilder.framework/samples/JDataStore/datastores/employee.jds";
Connection db_connection = DriverManager.getConnection(conn_string,
"duncan", "");

}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "There has been an" +
" error connecting " + ex.toString(),
"Connection Error",
JOptionPane.YES_NO_OPTION);
}
}

}

 
Answer #2    Answered By: Tammy Sanders     Answered On: Oct 10

You should change the declaration of the method:
public void connect() throws ClassNotFoundException

But I don't understand why the line  Class.forName(driver); is there,
because you don't assign the return value to a variable. If you remove
the line, you don't need to change the declaration of the method and the
code will do the same.

 
Answer #3    Answered By: Hilma Miller     Answered On: Oct 10

lezzaboy is making a reference to the JDBC driver
using Class.forName() method, even though he not
assigned the value returned by Class.forName() to a
variable, the method actually initializes a Class(in
the parameter), so he can refer the Class further to
create a Connection by using DriverManager...

somebody plz tell me is it the "import" keyword
related to "Class.forName()" method, becauz it's
looking like these two methods doing the same thing
(Im not sure)..

 
Answer #4    Answered By: Earl Stone     Answered On: Oct 10

> assigned the value returned by Class.forName() to a
> variable, the method actually initializes a Class(in
> the parameter), so he can refer the class  further to
> create a Connection by using DriverManager...

I see, I wasn't aware of this technique.

> somebody plz tell me is it the "import" keyword
> related to "Class.forName()" method, becauz it's
> looking like these two methods doing the same thing
> (Im not sure)..

It definitely can't have to do with the import statements in the file.
That only applies to classes  that are used directly, e.g. you need
'import java.util.Vector;' if you want to write  'Vector v = new
Vector();' The argument of Class.forName has to be fully stated, e.g.
'Class.forName("java.util.Vector");' even if you specified 'import
java.util.Vector;'

 
Answer #5    Answered By: Anna Hill     Answered On: Oct 10

actually yesterday im going JSP Complete Reference,
there accidentaly i saw abt <%@ page import="" %>
there i learnt the follwing, so plz clarify me abt the
following..

import statements are just like shorthand way(Vector
vMain = new Vector()) of using fully qualified class
names(java.util.Vector vMain = new java.util.Vector())

will all static members initialized automatically??
(even though im not refering tht perticular class
anywhere in a package) (i tested & it not initialized
any of the static parameters)

 
Answer #6    Answered By: Alexander Fields     Answered On: Oct 10

CATCH THE java.lang.ClassNotFoundException also in the try block

 
Didn't find what you were looking for? Find more on Calling classes Or get search suggestion and latest updates.




Tagged: