2016-04-08 121 views
3
package practice; 

import java.sql.*; 
import java.util.Properties; 

public class DBTEst 
{ 

    private static final String dbClassName = "com.mysql.jdbc.Driver"; 

    private static final String CONNECTION = 
          "jdbc:mysql://127.0.0.1/emotherearth"; 

    public static void main(String[] args) throws 
          ClassNotFoundException,SQLException 
    { 
    System.out.println(dbClassName); 
    Class.forName(dbClassName); 

     Properties p = new Properties(); 
    p.put("user","paulr"); 
    p.put("password","welcome"); 

    Connection c = DriverManager.getConnection(CONNECTION,p); 

    System.out.println("It works !"); 
    c.close(); 
    } 
} 

我是新來的數據庫,所以對於sql ..我只是在ubuntu操作系統中使用eclipse配置mysql。但是當我試圖運行該程序時,我得到如下的錯誤。用eclipse創建數據庫的問題

com.mysql.jdbc.Driver 
Exception in thread "main" java.sql.SQLException: Access denied for user 'paulr'@'localhost' (using password: YES) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086) 
    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.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1750) 
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1290) 
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2493) 
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526) 
    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(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    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:664) 
    at java.sql.DriverManager.getConnection(DriverManager.java:208) 
    at practice.DBTEst.main(DBTEst.java:32) 

我發現在論壇上同樣的問題,但並沒有解決我的問題......可能這是由於操作系統的改變,如果不是請原諒我的無知和請通過this..thanks指導您的幫助...安裝mySQL時設置的root密碼是「welcome」..

+1

例外情況是清楚的:即用戶名和密碼不具有對數據庫的訪問。與DBA交談。您必須授予對該主機連接到數據庫的用戶名和密碼的訪問權限。 – duffymo

+0

如何與DBA交談...什麼是DBA ...請原諒abt數據庫什麼都不知道......試圖學習.. :) – Sud

+0

數據庫管理員 - 擁有數據庫的人。如果那是你,你需要了解如何去做。您通常在Java之外創建數據庫和表。您可以與存在的電路圖進行交互。 – duffymo

回答

0

要麼你沒有在MySQL中正確設置用戶paulr,要麼你設置了它,或者你可能犯了常見的錯誤MySQL根據客戶端的IP地址或主機名進行帳戶訪問。許多人忘記在用戶設置中包含localhost和/或127.0.0.1

https://stackoverflow.com/a/26234768/639520