当我为DriverManager.getConnection指定我的用户名和密码时,它返回一个错误,说我没有使用密码

时间:2011-07-14 15:52:11

标签: java mysql passwords

我正在尝试建立与MySQL数据库的连接,但是当我尝试连接时,我收到错误:

SQLException:用户'test'@'localhost'拒绝访问(使用密码:NO) SQLState:28000 VendorError:1045

这是我试图获取连接的Java程序,我正在为DriverManager.getConnection提供密码。

//import java.sql.Connection;

//import java.sql.DriverManager;

//import java.sql.SQLException; 

import java.sql.*;
//import com.mysql.jdbc.Driver;

public class jconnector_test{
   public static void main (String[] args) {

    try{
    Class.forName("com.mysql.jdbc.Driver");
    //DriverManager.registerDriver (new Driver()); 
    }
    catch(Exception x)
    {
    System.out.println("Unable to load the driver class!" + x);
    }

    Connection conn = null;

    String url = "jdbc:mysql://localhost/test";
    String user = "test";
    String password = "test";

    try {
    conn = DriverManager.getConnection(url, user, password);
    //do something with the Connection                                                  
    }
    catch (SQLException ex) {
    // handle any errors
    System.out.println("SQLException: " + ex.getMessage());
    System.out.println("SQLState: " + ex.getSQLState());
    System.out.println("VendorError: " + ex.getErrorCode());
    }
  }
}

SQLException:用户'test'@'localhost'拒绝访问(使用密码:NO) SQLState:28000 VendorError:1045

0 个答案:

没有答案