JDBC MySQL 认证
JDBC MySQL authentication
我是 JDBC 的新手,我开始学习这个。我正在尝试使用以下代码建立与我的数据库的连接。
当我尝试运行下面的代码时
import java.sql.*;
class DBMS{
public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","2910");
Statement stmt = con.createStatement();
ResultSet re = stmt.executeQuery("select * from mytable");
while(re.next()){
System.out.println(re.getInt(1)+" "+re.getString(2)+" "+re.getString(3));
}
con.close();
}
}
使用CMD中的命令
java -cp .;mysql-connector.jar DBMS
其中 DBMS 是 class 名称。
我收到以下错误
Exception in thread "main" com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:921)
.......
at DBMS.main(dbms.java:7)
我需要做什么来解决这个问题?
很明显,异常表明您正在使用旧版本的 mysql 连接器来访问最新版本的 mysql 服务器。考虑升级 mysql 连接器 jar。由于您没有提到 mysql 服务器和连接器 jar 版本,我建议您采取以下措施来解决您面临的问题:
为您的 mysql 服务器使用兼容的连接器。如果你正在使用
mysql server version > 5.6
那么你可以使用 MySQL Connector/J 8.0
(即 mysql-connector-java-8.0.26.jar
)
更改代码中的以下内容。
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false","root","2910");
我是 JDBC 的新手,我开始学习这个。我正在尝试使用以下代码建立与我的数据库的连接。
当我尝试运行下面的代码时
import java.sql.*;
class DBMS{
public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","2910");
Statement stmt = con.createStatement();
ResultSet re = stmt.executeQuery("select * from mytable");
while(re.next()){
System.out.println(re.getInt(1)+" "+re.getString(2)+" "+re.getString(3));
}
con.close();
}
}
使用CMD中的命令
java -cp .;mysql-connector.jar DBMS
其中 DBMS 是 class 名称。 我收到以下错误
Exception in thread "main" com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:921)
.......
at DBMS.main(dbms.java:7)
我需要做什么来解决这个问题?
很明显,异常表明您正在使用旧版本的 mysql 连接器来访问最新版本的 mysql 服务器。考虑升级 mysql 连接器 jar。由于您没有提到 mysql 服务器和连接器 jar 版本,我建议您采取以下措施来解决您面临的问题:
为您的 mysql 服务器使用兼容的连接器。如果你正在使用
mysql server version > 5.6
那么你可以使用 MySQL Connector/J 8.0 (即mysql-connector-java-8.0.26.jar
)更改代码中的以下内容。
Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false","root","2910");