gpt4 book ai didi

java - 无法创建 PoolableConnectionFactory 客户端不支持身份验证

转载 作者:行者123 更新时间:2023-11-29 17:29:10 29 4
gpt4 key购买 nike

我正在尝试在数据源设施上连接 MySQL。我创建了一个数据库、sql 脚本、获取连接的类、context.xml 配置文件。

java.sql.SQLException: Cannot create PoolableConnectionFactory (Client does not support authentication protocol requested by server; consider upgrading MySQL client)

CONNECT 'jdbc:mysql://localhost:3306/testDB?user=root&password=root';
CREATE TABLE roles(

id INTEGER NOT NULL PRIMARY KEY,
name VARCHAR(10) NOT NULL UNIQUE
);
INSERT INTO roles VALUES(0, 'admin');
INSERT INTO roles VALUES(1, 'client');

CREATE TABLE users(
id INTEGER NOT NULL generated always AS identity PRIMARY KEY,
login VARCHAR(10) NOT NULL UNIQUE,
password VARCHAR(10) NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,

ON DELETE CASCADE
ON UPDATE RESTRICT
);

INSERT INTO users VALUES(DEFAULT, 'admin', 'admin', 'Harry', 'Potter', 0);
INSERT INTO users VALUES(DEFAULT, 'client', 'client', 'Peter', 'Parker', 1);

сcontext.xml

<Context>
<Resource name="jdbc/testDB_MySQL"
auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
defaultAutoCommit="false"
defaultTransactionIsolation="READ_COMMITTED"
url="jdbc:mysql://localhost:3306/testDB"/>
</Context>

还有 DBManager 类,我在其中获取连接和表中的数据

private static DBManager instance;

public static synchronized DBManager getInstance() throws DBException {
if (instance == null) {
instance = new DBManager();
}
return instance;
}

private DBManager() throws DBException {
try {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
// ST4DB - the name of data source
ds = (DataSource) envContext.lookup("jdbc/testDB_MySQL");
LOG.trace("Data source ==> " + ds);
} catch (NamingException ex) {
ex.printStackTrace();
LOG.error(Messages.ERR_CANNOT_OBTAIN_DATA_SOURCE, ex);
throw new DBException(Messages.ERR_CANNOT_OBTAIN_DATA_SOURCE, ex);
}
}

private DataSource ds;
public Connection getConnection() throws DBException {
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException ex) {
ex.printStackTrace();
LOG.error(Messages.ERR_CANNOT_OBTAIN_CONNECTION, ex);
throw new DBException(Messages.ERR_CANNOT_OBTAIN_CONNECTION, ex);
}
return con;
}

public User findUserByLogin(String login) throws DBException {
User user = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Connection con = null;
try {
con = getConnection();
pstmt = con.prepareStatement(SQL_FIND_USER_BY_LOGIN);
pstmt.setString(1, login);
rs = pstmt.executeQuery();
if (rs.next()) {
user = extractUser(rs);
}
con.commit();
} catch (SQLException ex) {
rollback(con);
ex.printStackTrace();
throw new DBException(Messages.ERR_CANNOT_OBTAIN_USER_BY_LOGIN, ex);
} finally {
close(con, pstmt, rs);
}
return user;
}

以及数据源和驱动程序 enter image description here可能是什么问题呢?我将不胜感激您的回答!

最佳答案

确保您拥有最新的 MySQL JDBC 驱动程序:

https://dev.mysql.com/downloads/connector/j/

关于java - 无法创建 PoolableConnectionFactory 客户端不支持身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783004/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com