【问题标题】:why No suitable driver found for mysql message here?为什么这里没有为 mysql 消息找到合适的驱动程序?
【发布时间】:2012-12-16 13:24:46
【问题描述】:

这是我的代码 sn-p,

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class Delete
{
    public static void main(String args[])
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");

            Connection con = DriverManager.getConnection("mysql:jdbc://localhost:3306/raja","root","459805");
            Statement stmt=con.createStatement();
            int count=stmt.executeUpdate("DELETE GENNU WHERE USER_ID=3;");
            if(count>0)
                System.out.println(" Ok Deletion done");
        }
        catch(ClassNotFoundException e)
        {
            System.out.println(e.getMessage());
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
        }
    }
}

当我执行它时,我得到了这样的结果。

【问题讨论】:

    标签: java mysql sql jdbc


    【解决方案1】:

    实际上您的DELETE 语句中有错误,您缺少FROM 关键字。应该是

    DELETE FROM GENNU WHERE USER_ID=3
    

    查看错误,它指向DELETE

    更新 1

    试试,jdbc:mysql 不是mysql:jdbc

    Class.forName("com.mysql.jdbc.Driver");
    connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/raja"
                  + "user=root&password=459805");
    

    【讨论】:

    【解决方案2】:

    试试

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    

    The documentation 说:

            // The newInstance() call is a work around for some
            // broken Java implementations
    
            Class.forName("com.mysql.jdbc.Driver").newInstance();
    

    另外,网址应该是

    jdbc:mysql://localhost/3306/raja
    

    而不是

    mysql:jdbc://localhost/3306/raja
    

    【讨论】:

    • 对不起,它不工作。错误:未报告的异常 InstantiationException;必须被捕获或声明被抛出 Class.forName("com.mysql.jdbc.Driver").newInstance(); ^ 1 个错误
    • 不是编译,因为你忘了处理异常。也就是说,我认为主要问题是错误的 URL。首先尝试更改 URL。并阅读the tutorial about exceptions。这是您在使用 JDBC 之前应该了解的基本知识。
    • 我已经更改了网址并解决了一半,谢谢您,剩下的部分问题已由@Kuya John 解决。
    【解决方案3】:

    您需要 mySQL Java 连接器。您可以在此处的下载页面找到:

    https://www.mysql.com/products/connector/

    【讨论】:

    • 谢谢你的回答,但我已经有了它并用 -cp 放在了路径中。
    • 如果 JDBC 驱动程序不在类路径中,则会抛出异常:ClassNotFoundException,来自该指令:Class.forName("com.mysql.jdbc.Driver");
    • MySql JDBC 驱动也可以通过 Maven 轻松找到:Maven Repo Search
    猜你喜欢
    • 2020-01-20
    • 2016-11-04
    • 2013-09-06
    • 1970-01-01
    • 2011-10-21
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多