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

时间:2018-06-10 11:02:45

标签: java mysql database intellij-idea

我正在尝试在DataSource工具上连接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;
    }

DataSources和Driver enter image description here 可能是什么问题呢?我将非常感谢答案!

1 个答案:

答案 0 :(得分:0)

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

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