这个问题在这里已经有答案了:
错误:客户端不支持服务器请求的身份验证协议;考虑升级mysql客户端(4个答案)
去年关门了。
我正在尝试连接数据源设施上的mysql,我创建了一个数据库,sql脚本,从中获取连接的类,context.xml配置文件。
java.sql.sqlexception:无法创建poolableconnectionfactory(客户端不支持服务器请求的身份验证协议;考虑升级mysql客户端)
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);
сontext.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;
}
以及数据源和驱动程序
有什么问题吗?我会很感激你的回答!
1条答案
按热度按时间bbuxkriu1#
确保您拥有最新的用于mysql的jdbc驱动程序:
https://dev.mysql.com/downloads/connector/j/