类型不匹配:无法从 java.sql.Connection 转换为 com.mysql.jdbc.Connection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21282491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:53:30  来源:igfitidea点击:

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

mysqlservletsjdbc

提问by Zealous

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

我试图将我的数据从 servlet 发送到 mysql 服务器,但在 servlet 中我得到了

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

错误:“类型不匹配:无法从 java.sql.Connection 转换为 com.mysql.jdbc.Connection”

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

任何人都可以建议我删除代码错误的方法。我不明白。

回答by Erkan Ak?n

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

删除import com.mysql.jdbc.*并使用import java.sql.Connection. 我想会没事的。

回答by Augusto

I think you're using the wrong connection string and the driver manager doesn't know how to instantiate the connection. The documentationhas 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;

而且我认为导入存在问题,如另一个问题中所述:类型不匹配:无法从 Connection 转换为 Connection,您需要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)

要修复 DriverManager 错误,您需要使用以下代码注册驱动程序(取自 mysql 文档)

    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
    }

回答by Papa Kojo

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.

希望能帮助到你。