Reputation: 33
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.
Upvotes: 3
Views: 25706
Reputation: 41
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
Upvotes: 0
Reputation: 813
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.
Upvotes: -1
Reputation: 29827
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
}
Upvotes: 1
Reputation: 106
Remove the import com.mysql.jdbc.*
and use import java.sql.Connection
. I think it would be ok.
Upvotes: 8