使用mac Yosemite上的java连接到mysql数据库,并在CLASS PATH中连接到jdbcdriver jar文件的完整url。 netbeans控制台提供了" java.sql.SQLException:拒绝访问用户&root;' @' localhost' (使用密码:是)。"我作为论坛尝试了很多方法,但它仍然不起作用。
这是我的代码
//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)
答案 0 :(得分:0)
您获得的例外情况是告诉您已成功连接到服务器,但不允许/允许用户root
从localhost
进行连接。
您必须查看您的mysql用户权限表,并确保您有一行允许root
使用正确的密码从localhost
进行连接。
以下SQL将显示:
SELECT host, user FROM mysql.user;
如果root
和localhost
或root
和%
有一行,则必须使用此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。再试一次,确保你会得到连接。
答案 1 :(得分:0)
你想看看吗?它对我有用。
import java.*;
import java.sql.*;
import java.util.*;
//STEP 1. Import required packages
import java.sql.*;
public class Memory {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/library";
// Database credentials
static final String USER = "root";
static final String PASS = "root";
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 uisend");
//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
答案 2 :(得分:0)
我不知道为什么它可以作为我的代码而且不需要改变。
Qualtrics.SurveyEngine.addOnload(function () {
$('NextButton').hide();
$('NextButton').insert({
before: "<input id=\"checkButton\" type=\"button\" value=\" >> \" title=\" >> \">"
});
$('checkButton').onclick = function fullScreen() {
launchIntoFullscreen(document.documentElement);
$('NextButton').click();
};
function launchIntoFullscreen(element) {
if(element.requestFullscreen) {
element.requestFullscreen();
} else if(element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if(element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if(element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}
});