Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassNotFoundException while running jdbc program

 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am running a simple jdbc program called JDBCEX.java, it compiled successfully but while running i am getting the below exception:
Please help me with this: (below i have pasted the source code)

In sss
java.lang.ClassNotFoundException: com.jdbc.mysql.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at testpackage.JDBCEX.main(JDBCEX.java:14)

Source Code :
package testpackage;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Driver;
import java.sql.SQLException;
public class JDBCEX {
public static void main(String[] args) {

try{
System.out.println("In sss");
Class.forName("com.jdbc.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/sksoftwares","root","admin");
System.out.println("Ex");


}
catch(Exception e)
{
e.printStackTrace();
}

}


}

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to include a jar with that class in it (mostly likely mysql.jar) in your classpath. That tells Java where to find it. If you don't have mysql.jar, you need to download MySQL before you can run your program.
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Greg,

I included the jar file and it works as a stand alone program. But in eclipse i am doing a struts project there i am getting this error.

while starting up tomcat i am getting this exception:


org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://localhost/sksoftwares', cause:
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:743)
at org.apache.commons.dbcp.BasicDataSource.setLogWriter(BasicDataSource.java:576)
at org.apache.struts.action.ActionServlet.initModuleDataSources(ActionServlet.java:1087)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:472)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4364)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:924)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:887)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1147)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Jul 8, 2010 11:09:14 AM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet action as unavailable
Jul 8, 2010 11:09:14 AM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /SKSoftwares threw load() exception
javax.servlet.UnavailableException: Initializing application data source org.apache.struts.action.DATA_SOURCE
at org.apache.struts.action.ActionServlet.initModuleDataSources(ActionServlet.java:1091)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:472)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4364)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:924)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:887)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1147)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

Greg Charles wrote:You need to include a jar with that class in it (mostly likely mysql.jar) in your classpath. That tells Java where to find it. If you don't have mysql.jar, you need to download MySQL before you can run your program.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is basically the same. The driver should reside in the WEB-INF/folder of your web application.
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe,

I have following jar files in my WEB-INF/lib folder : 1) mysql-connector-java-3.0.17-ga-bin.jar 2)commons-dbcp-1.1.jar 3)commons-pool-1.1.jar and also other default jar files.

I am using eclipse and there if i run the stand alone jdbc program it runs. But if i start tomcat to run my struts project it gives the problem.

Please let me know if you need any further info.



Please help me with this.

Christophe Verré wrote:The problem is basically the same. The driver should reside in the WEB-INF/folder of your web application.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if i start tomcat to run my struts project it gives the problem.

Are you launching Tomcat from Eclipse using a plugin ? If so, you may have to configure the plugin. It might be using a temporary folder to execute the application.
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe,

Thanks for your response.

If i start tomcat from command prompt also the same problem. Exception will be thrown but finally it says tomcat started.



Christophe Verré wrote:
But if i start tomcat to run my struts project it gives the problem.

Are you launching Tomcat from Eclipse using a plugin ? If so, you may have to configure the plugin. It might be using a temporary folder to execute the application.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't look at the stack trace carefully. The error says : "Cannot create JDBC driver of class ''", which shows the the driver class is not set. Where did you configure the database properties ?
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I configured it in struts-config.xml as below:

<struts-config>
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassname" value="com.mysql.jdbc.Driver"/>
<set-property property="url" value="jdbc:mysql://localhost/sksoftwares"/> //sksofwares is the db name.
<set-property property="username" value="root"/>
<set-property property="password" value="admin"/>
</data-source>
</data-sources>
<form-beans />
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="forward1" path="/Error.jsp"></forward>
</global-forwards>
<action-mappings>
<action path="/login" type="com.sksoftwares.classes.LoginAction" name="loginForm" validate="true"
input="/login.html" >
<forward name="success" path="/home.jsp"></forward>
<forward name="failure" path="/login.html"></forward>
</action>
</action-mappings>
<message-resources parameter="com.sksoftwares.struts.ApplicationResources" />
</struts-config>


Christophe Verré wrote:I didn't look at the stack trace carefully. The error says : "Cannot create JDBC driver of class ''", which shows the the driver class is not set. Where did you configure the database properties ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
driverClassname -> driverClassName
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Christophe ... it worked..

You saved my day!!!

Christophe Verré wrote:driverClassname -> driverClassName
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic