This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JDBC and the fly likes driver problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "driver problem" Watch "driver problem" New topic
Author

driver problem

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 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.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 pf try loop and stmt in catch is executed.

please solve my doubt......i would really apreciate that..thanks in advance...
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30580
    
154

First thing is to confirm the error by putting e.printStackTrace() in the catch block.

From the position, it is likely that you are experiencing a classpath problem. What server are you using? Tomcat, JBoss, WebSphere, WebLogic, etc.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
mahesh lanka
Greenhorn

Joined: Oct 19, 2006
Posts: 8
hi,

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:

i am using apache tomcat server.



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
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Note that the exception is:

java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver


The JVM can't find the class org.gjt.mm.mysql.Driver. Be sure that the jar containing this class is in your app's WEB-INF/lib directory or the tomcat common/lib directory.
[ October 19, 2006: Message edited by: Scott Johnson ]
mahesh lanka
Greenhorn

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

thanks man..it worked..and you solved my problem..thanks once again...thanks to all who helped me.....

regards,
mahesh
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
You are most welcome!
 
Consider Paul's rocket mass heater.
 
subject: driver problem