gpt4 book ai didi

java - 连接失败!检查输出控制台

转载 作者:行者123 更新时间:2023-11-29 21:54:56 27 4
gpt4 key购买 nike

在 mac Yosemite 上使用 java 连接到 mysql 数据库,并连接到 CLASS PATH 中 jdbcdriver jar 文件的完整 url。 netbeans 控制台给出“java.sql.SQLException:用户'root'@'localhost'访问被拒绝(使用密码:YES)”。试了很多论坛的方法,还是不行。

这是我的代码

//STEP 1. Import required packages
import java.sql.*;

public class JDBCExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3307/library";

// Database credentials
static final String USER = "root";
static final String PASS = "default$";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
//STEP 2: Register JDBC driver (automatically done since JDBC 4.0)
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 database...");
stmt = conn.createStatement();
rs = stmt.executeQuery("select uid uname from User");

//STEP 5: Process the results
while(rs.next()){
System.out.println("User ID="+rs.getInt("uid")+", Name="+rs.getString("uname"));
}

}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 JDBCExample

结果如下:

Connection Failed! Check output console
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1686)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1207)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2254)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:422)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:134)
at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:105)
at cs157a_test.DataSourceTester.main(DataSourceTester.java:54)

最佳答案

您得到的异常是告诉您已成功连接到服务器,但是不允许/授予用户rootlocalhost连接。

您必须检查 mysql 用户权限表,并确保有一行允许 root 使用正确的密码从 localhost 连接。

以下 SQL 将显示这一点:

SELECT host, user FROM mysql.user;

如果存在 rootlocalhostroot% 的行,那么您必须重置您的使用此 SQL 的密码:

set password for 'root'@'localhost' = password('your_password');

如果没有行,您必须创建它,然后设置要使用该用户访问的数据库的权限。

您可以使用以下 SQL 来实现此目的:

CREATE USER 'root'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL ON YOU_DATABASES.* TO 'root'@'localhost';

最后你必须刷新权限或重新启动 mysql。再试一次,确保您能够连接。

关于java - 连接失败!检查输出控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251890/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com