3
import java.io.IOException;
import java.sql.DriverManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.*;

*// Here,in below 2 statement, I got 2 the same error as shown in title*

**Connection con = DriverManager.getConnection("localhost:3306", "root","simple");               
Statement stmt = con.createStatement();**
    if(stmt.execute(sql)){
    System.out.println("Sql Statement Executed.");
    }
    else
    {
        System.out.println("Sql Statement not eexecuted.");
    }
    if((unm != null) && (pwd != null)){
        RequestDispatcher rd =request.getRequestDispatcher("Home.jsp");
        rd.forward(request, response);
    }
}

}

I am trying to send my data from servlet to mysql server but in servlet I got the

error : "Type mismatch: cannot convert from java.sql.Connection to com.mysql.jdbc.Connection"

Can anyone suggest me the way to remove the error for my code. I am not getting any idea.

4 Answers 4

8

Remove the import com.mysql.jdbc.* and use import java.sql.Connection. I think it would be ok.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it is even works but I want to use mysql as a back-end tool and the DriverManager can not be resolved error still come..
In Hurry ness, I forgot, it's a Connection interface provided in javase spec which handle the object given by DriverManger>getConnection()...!!
1

I think you're using the wrong connection string and the driver manager doesn't know how to instantiate the connection. The documentation has more info about the format.

It think the code should be something like

con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","simple");

And I think there's a problem with the imports, as described in this other question: Type mismatch: cannot convert from Connection to Connection, and you need to import java.sql.Connection;

edit

To fix the DriverManager error you need to register the driver with the following code (taken from the mysql documentation)

    try {
        // The newInstance() call is a work around for some
        // broken Java implementations

        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (Exception ex) {
        // handle the error
    }

2 Comments

Thanks, Augusto for you reply, Yes it is resolved but still I got "DriverManager can not be resolved" error..
you probably haven't registered the mysql driver. Please read this documentation
0

try import java.sql.Connection and java.sql.* I hope it will work. Also I had gotten this error I try lot of ways to solve it but I got it also from your quiz in this page. And I was able to solve it.

THANK YOU ALL LIFE SAVER

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1

I am answering an old question but I ran into the same issue and I resolved it by importing all the libraries:

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

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.