无法将Java(Netbeans)连接到MariaDB
我无法将java连接到MariaDB。我看到了所有类似的问题和答案,但没有一个解决了我的问题。这是我的密码:无法将Java(Netbeans)连接到MariaDB,java,mariadb,Java,Mariadb,我无法将java连接到MariaDB。我看到了所有类似的问题和答案,但没有一个解决了我的问题。这是我的密码: /** To change this license header, choose License Headers in Project * Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ pack
/** To change this license header, choose License Headers in Project
* Properties. * To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mariadbtest;
import java.sql.*;
/**
*
* @author AAAA
*/
public class MariaDBTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args){
try {
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
String url = "jdbc:MariaDB://localhost:3306/customer"; // url to access the DB
String user = "root";
String pwd="password";
Connection connection = DriverManager.getConnection(url,user,pwd);
System.out.println("Connected to MariaDB");
}
catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
catch (Exception ex) {
System.out.println("Exception: "+ ex.toString());
}
// TODO code application logic here
}
}
注意以下几点:
- 我正在使用连接器mysql-connector-java-8.0.22
- 我有最新的java版本
- MariaDB版本为10.3.27
- 我已将连接器复制到程序文件中java文件夹中的lib目录
- 我已通过属性-->库-->添加JAR/文件夹将连接器JAR文件添加到我的项目中
- MariaDB具有服务名称MariaDB
- 数据库名为customer
构建成功(总时间:3秒)我认为问题在于您使用的MySQL连接器/J JDBC驱动程序的URL无法识别。此URL
jdbc:MariaDB://localhost:3306/customer
表示从“MariaDB”提供商查找驱动程序。但是很明显,MySQL驱动程序并不(通过SPI)宣传自己是MariaDB驱动程序
解决方案
首先你们应该摆脱这个:
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
它是不需要的,并且您正在将驱动程序类名硬连接到代码中。也要去掉关联的异常处理程序子句。(这些都是东西。20年前可能就需要了……但现在不再需要了。)
您可以通过两种方式解决驱动程序分辨率问题:
jdbc:mysql://localhost:3306/customer
根据,MySQL连接器/J驱动程序与MariaDB兼容。但是在MySQL Connector/J文档中没有提到在JDBC url中使用“MariaDB”
jdbc:mariadb://localhost:3306/customer
或
文件中不清楚“协议”部分的情况是否重要。然而,MariaDB文档中的语法规范使用的是“MariaDB”而不是“MariaDB”,因此为了风格一致性,我建议使用小写。我认为问题在于您使用的MySQL Connector/J JDBC驱动程序的URL无法识别。此URL
jdbc:MariaDB://localhost:3306/customer
表示从“MariaDB”提供商查找驱动程序。但是很明显,MySQL驱动程序并不(通过SPI)宣传自己是MariaDB驱动程序
解决方案
首先你们应该摆脱这个:
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
它是不需要的,并且您正在将驱动程序类名硬连接到代码中。也要去掉关联的异常处理程序子句。(这些都是东西。20年前可能就需要了……但现在不再需要了。)
您可以通过两种方式解决驱动程序分辨率问题:
jdbc:mysql://localhost:3306/customer
根据,MySQL连接器/J驱动程序与MariaDB兼容。但是在MySQL Connector/J文档中没有提到在JDBC url中使用“MariaDB”
jdbc:mariadb://localhost:3306/customer
或
文件中不清楚“协议”部分的情况是否重要。但是,MariaDB文档中的语法规范使用“MariaDB”而不是“MariaDB”,因此,为了保持风格的一致性,我建议使用小写。可能尝试使用@DevilsHnd。我是否使用与问题中提到的插入连接器相同的步骤?请阅读上面提供的链接中包含的信息。可能尝试使用@DevilsHnd。我是否使用与问题中提到的插入连接器相同的步骤?阅读上面提供的链接中包含的信息。