繁体   English   中英

MYSQL 找不到合适的驱动程序

[英]MYSQL No suitable driver found

我目前使用 Intellij,事实证明我正在尝试连接到 MySQL 数据库,但是在添加库并使用与 MariaDB 驱动程序完美配合的连接类后,我发现问题标题中的错误

public void conectar() {
        try {
            conexion = DriverManager.getConnection(url, usuario, contraseña);
            if (conexion != null) { JOptionPane.showMessageDialog(null, "Conexión establecida a : \n" + url,  "ACDA2", JOptionPane.INFORMATION_MESSAGE);
                Class.forName("com.mysql.jdbc.Driver");
                stm = conexion.createStatement();//crea un objeto que permite enviar instrucciones a la base de datos
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Conexión fallida a : \n " + url, "", JOptionPane.ERROR_MESSAGE);
            System.out.println(ex.getMessage());
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Error al cargar el driver", "", JOptionPane.ERROR_MESSAGE);
            System.out.println(e.getMessage());
        }
    }

在此处输入图片说明

在此处输入图片说明

这些是我发送给我的班级连接的值

c= new conexion("jdbc:mysql://", "127.0.0.1/", "root", "", "sanciones"); c.conectar();

然后我详细介绍了连接类构造函数

 public conexion(String driver,String host, String usuario, String 
 contraseña, String baseDatos) {
    this.usuario = usuario;
    this.contraseña = contraseña;
    this.baseDatos = baseDatos;
    this.driver = driver;
    this.host = host;
    this.url = driver + this.host  + this.baseDatos;
}

更新

驱动版本com.mysql.jdbc_5.1.5不允许隐式加载驱动,因为缺少META-INF中的Services子文件夹及其对应的内容,即使是JDBC4,在mysql手册中也是这样说的有可能,至少在这个特定版本中不是,问候

在获取连接之前应调用 Class.forName

需要首先调用“Class.forName”以首先加载正确的驱动程序。试试这个,

    try {
        Class.forName("com.mysql.jdbc.Driver");
        conexion = DriverManager.getConnection(url, usuario, contraseña);
        if (conexion != null) { JOptionPane.showMessageDialog(null, "Conexión establecida a : \n" + url,  "ACDA2", JOptionPane.INFORMATION_MESSAGE);
            stm = conexion.createStatement();//crea un objeto que permite enviar instrucciones a la base de datos
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM