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

