Problem with servlet to connect to Mysqldatabase

Hi,

Below are my servlet and class file code.

The same code i used in a class file , complied and tested it is working fine and it is displaying the data

from the table with out any errors.

But when i use the same code in the servlet it is giving the following error.

I have my jdk1.2.2 in d:\jdk1.2.2

I have my jsdk2.0 in d:\jsdk1.2.2

I have my javawebserver in d:\javawebserver

I have my jdbc driver for mysql is in d:\mm.mysql.jdbc-1.2c

I copied my servlet into the servlets dirctory in the javawebserver directory.

It is giving me following error

Unable to loade driver

SQLException: no sutable driver

SQLState :08001

endererror :0

So please go through the code let me know what is the problem in my code.

Can we call the class which is working fine into the servlet.

This is my servlet code.

================================================================================================================

import java.sql.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class TakeTable extends HttpServlet

{

public void doPost(HttpServletRequest req,HttpServletResponse res)

throws ServletException, IOException

{

res.setContentType("Text/html");

PrintWriter out = res.getWriter();

String Tname = req.getParameter("name");

out.println("<html>");

out.println("<head><Title>Hello</title></head>");

out.println("<body>");

out.println("<h2>u r watching the data of </h2><h1><font color=red>" + Tname + "</font></h1><h2>.</h2>");

try

{

// The newInstance() call is a work around for some

// broken Java implementations

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

}

catch (Exception E)

{

out.println("<h1> Unable to load driver </h1> ");

E.printStackTrace();

}

try {

Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost/SVG? user=trigeo");

//System.out.println("Connected to MySQL");

Statement Stmt = Conn.createStatement();

ResultSet RS = Stmt.executeQuery("SELECT * from user");

while (RS.next())

{

out.print("<h3>" + RS.getString(2) + "</h3>" );

out.print(" ");

out.println("<h3>" + RS.getString(1) + "</h3> ");

}

out.print("</body></html>");

// Clean up after ourselves

RS.close();

Stmt.close();

Conn.close();

}

catch (SQLException E)

{

out.println("<h3> SQLException: " + E.getMessage() + " </h3>" );

out.println("<h3> SQLState:" + E.getSQLState() + "</h3>");

out.println("<h3> VendorError: " + E.getErrorCode() + "</h3>");

}

}

public String getServletInfo()

{

return "haiiiiii";

}

}

================================================================================================================

This is my class file which is working fine:

===================================================================================================================

import java.sql.*;

// Notice, do not import org.gjt.mm.mysql.*

// or you will have problems!

class mysql

{

public static void main(String[] Args)

{

try {

// The newInstance() call is a work around for some

// broken Java implementations

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

}

catch (Exception E)

{

System.err.println("Unable to load driver.");

E.printStackTrace();

}

try {

Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost/SVG? user=trigeo");

System.out.println("Connected to MySQL");

Statement Stmt = Conn.createStatement();

ResultSet RS = Stmt.executeQuery("SELECT * from user");

while (RS.next())

{

System.out.print(RS.getString(2));

System.out.print(" ");

System.out.println(RS.getString(1));

}

// Clean up after ourselves

RS.close();

Stmt.close();

Conn.close();

}

catch (SQLException E)

{

System.out.println("SQLException: " + E.getMessage());

System.out.println("SQLState:" + E.getSQLState());

System.out.println("VendorError: " + E.getErrorCode());

}

}

}

===================================================================================================================

Thanks

[4774 byte] By [servletforuma] at [2007-9-19]
# 1
Looks like a CLASSPATH issue - Java Web Server cannot find org.gjt.mm.mysql.Driver. Either modify the Web server classpath or include the MySql driver in the Web app (in WEB-INF/lib if jar file, or WEB-INF/classes).
GrayMana at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi,

Thanks for u r reply.

Yes u r reight that it is unable to find the path of the jdbc driver.

I tried by copying lib directory of javawebserver and also tried by copying

in classes directory of javawebserver still it is giving me same problem.

Is there is any problem in my code.

THERE ARE ANY OTHER SOLUTIONS FOR THIS PROBLEM , LET ME

KNOW ALL POSSIBLE SOLUTIONS FOR THIS.

Thanks

servletforuma at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3
Database url is one of the major issue when this sqlerror occurs.It seems there is an extra space before "user" try DriverManager.getConnection(jdbc:mysql://localhost/SVG, "trigeo", "") also.Good Luck ...
ksupadhyaa at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thaks for u r reply but with those space and other things it is workingfine in class file ...but it is not working in the servlet..
servletforuma at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5
Hi allThanks to all the members who gave me so many solutions on servlets..Finally i am able to connect to database by copying the mysql driver intojavawebserver directory and setting classpath for mysq jdbc driver in command prompt to that
servletforuma at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...