package cn.itcast.jdbc.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Date;
public class Example01 {
public static void main(String[] args) throws SQLException {
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
try {
//
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection (url, username,password);
stmt = conn.createStatement();
//
String sql = "select * from users";
rs = stmt.executeQuery(sql);
//
System.out.println("id | name | password | email | birthday");
while (rs.next()) {
int id = rs.getInt("id"); //
String name = rs.getString("name");
String psw = rs.getString("password");
String email = rs.getString("email");
Date birthday = rs.getDate("birthday");
System.out.println(id + " | " + name + " | " + psw + " | " + email
+ " | " + birthday);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally{
//
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
}
這個是報錯:
Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3421)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1247)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at cn.itcast.jdbc.example.Example01.main(Example01.java:20)
網上該找到的錯誤都試過了 不行啊 找大神
uj5u.com熱心網友回復:
mysql資料庫user表中root對應的host改成%試試uj5u.com熱心網友回復:
select user,host from mysql.user 看一哈轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/76691.html
標籤:MySQL
