有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

拒绝用户“root”@“localhost”的java c3p0访问(使用密码“是”)

当我使用以下命令时,我似乎无法使用c3p0连接数据库:

DriverManager.getConnection("jdbc:mysql://localhost:3306/rdmssql", "root", "");

没有抛出异常。 但是,当我尝试使用c3p0连接池连接时:

  private ComboPooledDataSource connectionPool;

  ...other code

  /**
   * 
   * @return
   * @throws Exception 
   */
  public Connection getConnection() throws Exception {
    return getInstance().connectionPool.getConnection();
  }  

/**
   * 
   * @param driver
   * @param hostname
   * @param databaseName
   * @param username
   * @param pwd
   * @throws Exception
   * @throws PropertyVetoException 
   */
  private void setConnectionDetails(String driver, String hostname, String databaseName, String username, String pwd) throws Exception, PropertyVetoException {
    connectionPool.setDriverClass("com.mysql.jdbc.Driver");
    connectionPool.setJdbcUrl(driver + "://" + hostname + "/" + databaseName);
    connectionPool.setUser(username);
    connectionPool.setPassword(pwd);

    // the settings below are optional -- c3p0 can work with defaults
    // will work on this in the future
    connectionPool.setInitialPoolSize(5);
    connectionPool.setMinPoolSize(5);
    connectionPool.setAcquireIncrement(5);
    connectionPool.setMaxPoolSize(20);
    connectionPool.setMaxStatements(180);
  }

爪哇。sql。SQLException:用户的访问被拒绝:'root@127.0.0.1“(使用密码:YES)被抛出。我不确定我是否错过了c3p0的一些配置


共 (1) 个答案

  1. # 1 楼答案

    例外情况是这样的(“使用密码”:是)。 驱动程序管理器版本不使用密码(并且成功),而连接池版本使用密码(并且失败)。 检查传递给pwd的参数值(并使其发送“”)