Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassNotFoundException Mysql

 
Vince Black
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi !
I have a Database Connection Problem in WebSphere Studio Application Developer (Windows) Version: 5.1.0
This is my Source Code :

And i Get this Error Message :

An exception has been intercepted
java.lang.ClassNotFoundException: com.ibm.websphere.jdbcx.sqlserver.SQLServerDataSource
at java.net.URLClassLoader.findClass(URLClassLoader.java:240)
at java.lang.ClassLoader.loadClass(ClassLoader.java:516)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:441)
at java.lang.ClassLoader.loadClass(ClassLoader.java:448)
at java.lang.Class.forName1(Native Method)
at java.lang.Class.forName(Class.java:142)
at Database.main(Database.java:13)

i hope you can held me
thx
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "com.ibm.websphere.jdbcx.sqlserver.SQLServerDataSource" in your classpath? What type of project are you trying to access it from? If it is an EJB project, you need to include the .jar/.zip file in your module dependencies. Otherwise, you need to include it in your java build path.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Oh boy, where to start.
(1) You're not using a mysql driver. The driver class you mention is the DataDirect driver for Microsoft SQL Server -- not mySql.
(2) Don't do that! You shouldn't use DriverManager to try and instantiate a DataSource -- that doesn't work. Use WebSphere's J2EE connection pooling instead.
I'd suggest you start with one of the WebSphere tutorials and modify that to see how connection pooling works. Look in the InfoCenter under "DataSource" and it should lead you where you need to be.
Kyle
 
Sander Klooster
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
You can try this code to make a connection.
first make a class ConnectionManager, like this:
package dbMySql;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* Sander Klooster
* This class provides access to the MYSQL database.
*/
public class ConnectionManager
{
static
{
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch (Exception e)
{
e.printStackTrace(System.out);
}
}
public static Connection getConnection()
{
try
{
return DriverManager.getConnection("jdbc:mysql://localhost/sir?user=multi&password=media");
} catch (Exception e)
{
e.printStackTrace(System.out);
throw new Error("Could not resolve database connection.");
}
}
}

Then another class BusinessObject, like this:
package dbMySql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import dbMySql.ConnectionManager;
/**
* Sander klooster.
* This class is the base class for all business objects.
*/
public abstract class BusinessObject {
abstract protected void unpackResultSet(ResultSet rs)
throws java.sql.SQLException;
/**
* This method retreives data from the database. It uses the query
* to make a ResultSet. This ResultSet is passed to the retreiveResultSet
* method which is declared in this class.
*/
protected boolean retrieveData(String query) {
try {
Connection connection = ConnectionManager.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
if (!resultSet.next()) {
resultSet.close();
statement.close();
connection.close();
return false; // no rows in result set
} else {
unpackResultSet(resultSet);
resultSet.close();
statement.close();
connection.close();
return true;
}
} catch (Exception e) {
e.printStackTrace(java.lang.System.out);
throw new Error("Database error.");
}
}
}
You have to modify the DataBase class of course..
good luck.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to ask what kind of client you're building. Is this a class that is supposed to work with WebSphere Application Server, or simply a standalone Java class? If the former, then you need to use a DataSource to obtain the Connection. If the latter, then Sander's method will work.
In the second case, you'll also need to make sure the runtime classpath of your client (which is set through the client agent when you choose Run... in WSAD) points to the MySQL Jar files.
Kyle
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic