Hi ppl! Im using a java servlet to access a mysql 5.x database, BUT when I run the servelt from my web browser I get this error : "No suitable driver found for jdbc:mysql://localhost:3306/spacemandb?User=root&Password=admin "

I have intsalled all the necessary drivers viz mysql driver and it is working.

here is my code

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class AuthClientServlet extends HttpServlet {   
    
    public String JDBC_DRIVER = null;
    public String errorMsg = null;
    
    protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String outMsg = "";
        //PrintWriter pw;
        Connection mySQLConn;
        ResultSet rs;
        String cellnr;
        String password;
        
        String dbHost = "localhost"; //host db server
        String dbPort = ":3306"; //db port
        String database = "spacemandb"; //db name
        String dbUsername = "root"; //db username
        String dbPassword = "admin"; //db password
        
   
        cellnr = request.getParameter("Cellnr");
        password = request.getParameter("Password");
        
        response.setContentType("text/html");
   
        //pw = response.getWriter();
        
        //debugging purpose
        ServletOutputStream out = response.getOutputStream();
            
        try{
            JDBC_DRIVER = getInitParameter("com.mysql.jdbc.driver");
            
            if (JDBC_DRIVER != null){
                Class driverClass =  Class.forName(JDBC_DRIVER);
                if(driverClass.isAssignableFrom(Driver.class)){
                    DriverManager.registerDriver(
                            (Driver)driverClass.newInstance());
                }
            }
        } 
        catch (ClassNotFoundException e)
        {
            errorMsg = "Driver " + JDBC_DRIVER + " not found ";
            out.println("<P>" + errorMsg+ "</P>");
            outMsg = "ERROR";
        }
        catch (ClassCastException e)
        {
            errorMsg = "Driver class " + JDBC_DRIVER +
                " not a valid driver ";
            out.println("<P>" + errorMsg+ "</P>");
            outMsg = "ERROR";
        }
        catch (InstantiationException e)
        {
            errorMsg = "Driver class " + JDBC_DRIVER +
                " could not be instantiated";
            out.println("<P>" + errorMsg+ "</P>");
            outMsg = "ERROR";
        }
        catch (IllegalAccessException e)
        {
            errorMsg = "Driver class " + JDBC_DRIVER +
            " could not be accesed";
            out.println("<P>" + errorMsg+ "</P>");
            outMsg = "ERROR";
        }
        catch (SQLException e)
        {
            errorMsg = "Exception when initializing driver "
            + e.getMessage();
            outMsg = "ERROR";
            out.println("<P>" + errorMsg+ "</P>");
        }       
        try{
            mySQLConn = DriverManager.getConnection("jdbc:mysql://" + dbHost +
                    dbPort + "/" + database + "?User=" + dbUsername + 
                    "&Password=" + dbPassword); 
            
            PreparedStatement statement = mySQLConn.prepareStatement(
                  "SELECT * FROM tblUsers WHERE cellphoneNr='" +
                    cellnr + " ';");      
        
            rs = statement.executeQuery();
            }            
        }catch(Exception e){
            //do something e.g print error message  
            //debugging
            out.println("<P>" + e.getMessage().toString());
            out.println("</P>");
            outMsg = "ERROR";
        }
        
        //debugging
	out.println("</body></html>");
        
        //pw.write(outMsg);
    }
}

The error reffers to Connector/J not to instalation of MySQL. Do you have connector?
If not download it here. Extract the JAR file and place it in your lib directory (Tomcat_Directory/webapps/YOUR_PROJECT/WEB-INF/lib)

i think this is wrong

JDBC_DRIVER = getInitParameter("com.mysql.jdbc.driver");

you have a param in your config with this?

try changing it to this, at least for debugging purposes

JDBC_DRIVER = "com.mysql.jdbc.driver";

Thanks y'all for your reply I'll try your suggestions and give you the feed back!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.