aspose file tools*
The moose likes Servlets and the fly likes unable to load mysql driver through servlet program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "unable to load mysql driver through servlet program" Watch "unable to load mysql driver through servlet program" New topic
Author

unable to load mysql driver through servlet program

mahesh lanka
Greenhorn

Joined: Oct 19, 2006
Posts: 8
--------------------------------------------------------------------------------
hi,

i am using one servlet program (simple java program) in which,i am calling another java program which is simple connection to mysql database.

i am new to this all and please be patient to my problem..thanks in advance...

i am unable to connect to the database..the reason for it i could not understand....it is driver problem i guess....i set classpaths and everthing correctly.

my code is as below:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Java Servlet done by L p Mahesh</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello,My name is lanka poojya mahesh</H1>\n" +
"</BODY></HTML>");

Exp ex = new Exp();

out.println("1");
ex.print();
out.println("2");

}//end of doGet method
}//end of helloservlet

and Exp.java program is below:

import java.io.*;
import java.sql.*;
import java.sql.*;
import java.util.*;


public class Exp
{
public Exp()
{
System.out.println("test");
Connection conn = null;

try
{
System.out.println("001");

String userName = "root";
String password = "admin";

System.out.println("012");

String url = "jdbc:mysql://localhost:3306/mysql";

System.out.println("123");

Class.forName ("org.gjt.mm.mysql.Driver");

System.out.println("234");

conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}//end of finally


} // nd of Exp()
public int print()
{
System.out.println("inside this");
return 2;
}
}

upto this line "System.out.println("123");"
prog is ruunig correctly ..after that it is coming out of try loop and stmt in catch is executed.

please solve my doubt..my mind is blowing out due to this....i would really apreciate that..thanks in advance...
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
conn = DriverManager.getConnection (url, userName, password);

is throwing exception.Investigate the exception by putting a e.printStackTrace().


Rahul Bhattacharjee
LinkedIn - Blog
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10043
    
163

conn = DriverManager.getConnection (url, userName, password);

is throwing exception.


May not be. He mentioned that only the System.out.println("123") is being printed. The other System.out.println("234") isnt printed which indicates that

is not able to load the class. Most likely reason being that the driver class(the jar containing this class) is not there in the classpath.

Mahesh, how have you set up the classpath. Does the classpath contain the jar which has the driver class. Also, just to confirm that this is the reason, as mentioned by Rahul - use the following statement in the catch block:


This will display the exception stacktrace and will help in identifying the issue.

[ October 19, 2006: Message edited by: Jaikiran Pai ]
[ October 19, 2006: Message edited by: Jaikiran Pai ]

[My Blog] [JavaRanch Journal]
mahesh lanka
Greenhorn

Joined: Oct 19, 2006
Posts: 8
hi rahul,

thank you very much for the reply...i am new to java and servlets...please help me....as you said ,I did ,and the following exception came:



java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoa
der.java:1340)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoa
der.java:1189)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at Exp.<init>(Exp.java:27)
at HelloServlet.doGet(HelloServlet.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)

at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:16
0)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:793)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:702)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:571)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:644)
at java.lang.Thread.run(Thread.java:595)

Cannot connect to database server

Please help me.....I set the classpath of mysql driver before running of the program..but still I could not able to connect...

regards,
mahesh
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Jaikiran Pai has already replied as what now needs to be done in the previous post.The driver class is not getting loaded properyl.Try putting the jat which is containing the driver class in WEB-INF/lib folder of your application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to load mysql driver through servlet program