java.sql.SQLException:拒绝用户“用户名”@“本地主机”的访问(使用密码:是)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20787292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: Access denied for user 'username'@'localhost' (using pass word: YES)
提问by user3116798
I'm so grateful for the guys who have assisted me with advice on my previous post of the program below. This is my first sql-java (database) program, though I have some basic college knowledge of the two languages. I set my CLASSPATH via environment variable as per advice and YES the FirstExample.java / FirstExample.class is successfully compiled by Java Compiler and recognized at run time of the the JVM in the command line unlike before.
But then while trying to connect to the database I receive a long error message that I can't understand.
I'm a bit suspicious this could bit arising from 'username' issue, though I'm not really sure as I'm a newbie with database programing.
I'm wondering neither in the MySQL configuration wizard nor SQL -database design does 'username' appear though it's part of my tutorial FirstExample.java.
我非常感谢那些在我之前发布的程序中为我提供建议的人。这是我的第一个 sql-java(数据库)程序,尽管我对这两种语言有一些基本的大学知识。我根据建议通过环境变量设置了我的 CLASSPATH,是的,Java 编译器成功编译了 FirstExample.java / FirstExample.class,并在命令行中的 JVM 运行时识别,与以前不同。
但是在尝试连接到数据库时,我收到了一条我无法理解的长错误消息。
我有点怀疑这可能是由“用户名”问题引起的,尽管我不太确定,因为我是数据库编程的新手。
我不知道在 MySQL 配置向导和 SQL 数据库设计中都没有出现“用户名”,尽管它是我的教程 FirstExample.java 的一部分。
Please assist with solving this problem giving me head ache the whole day, thanks in advance.
请协助解决这个让我头疼一整天的问题,提前致谢。
Below is the command line error message:
下面是命令行错误信息:
Connecting to database...
java.sql.SQLException: Access denied for user 'username'@'localhost' (using pass
word: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:928)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4736)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1342)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2493)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2
526)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:347)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at FirstExample.main(FirstExample.java:22)
Goodbye!
Below is the basic information:
I saved the file as:C:\glassfish3\jdk\jre\lib\FirstExample.java
then compiled to FirstExample.class
以下是基本信息:
我将文件另存为:C:\glassfish3\jdk\jre\lib\FirstExample.java
然后编译为FirstExample.class
CLASSPATH:C:\glassfish3\jdk\jre\lib;C:\Program Files (x86)\MySQL\MySQL Connector J\mysql-connector-java-5.1.27-bin.ja
r
类路径:C:\glassfish3\jdk\jre\lib;C:\Program Files (x86)\MySQL\MySQL Connector J\mysql-connector-java-5.1.27-bin.ja
r
CATALINA:C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\jsp-api.jar
卡特琳娜:C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\jsp-api.jar
JAVA_HOME:C:\glassfish3\jdk
JAVA_主页:C:\glassfish3\jdk
path:C:\glassfish3\jdk\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin
小路:C:\glassfish3\jdk\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin
MySQL directory:C:\Program Files (x86)\MySQL\MySQL Server 5
.
MySQL目录:C:\Program Files (x86)\MySQL\MySQL Server 5
.
Here is the database info:
这是数据库信息:
database - 'EMP'.
table - 'Employees'.
password - 'password'
Columns; id; age; first; last.
data; 100; 28; Zaid; Khann.
Java program:
Java程序:
//STEP 1. Import required packages
import java.sql.*; // for standard JDBC programs
public class FirstExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName ( "com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating Statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.print(", Last: " + last);
}
//STEP 6: Clean-up enviroment
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
}catch (Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if (stmt !=null)
stmt.close();
} catch(SQLException se2) {
}// nothing we can do
try{
if(conn !=null)
conn.close();
} catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample
This is line 22 of the Java program in code editor:
这是代码编辑器中 Java 程序的第 22 行:
conn = DriverManager.getConnection(DB_URL,USER,PASS);
回答by Sibbo
You are using username
as username, password
as password and you are logging in from localhost
. You have to have an entry in the mysql.user
table that corresponds to this.
您正在使用username
用户名 password
和密码,并且您正在从localhost
. 您必须在mysql.user
表中具有与此相对应的条目。
Use CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'
to create the user you are logging in with.
使用CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'
以创建与正在登录用户。
回答by alexiegriffin
Check your username and password, you are using usernameas username and passwordas password, I think that your password is correct (because your database info said so), so you can try to change username to root:
检查您的用户名和密码,您使用用户名作为用户名和密码作为密码,我认为您的密码是正确的(因为您的数据库信息是这样),因此您可以尝试将用户名更改为root:
static final String USER = "root";
静态最终字符串 USER = "root";