2013-07-28 196 views
1

我試圖從數據庫中提取記錄。但我面臨這個拒絕訪問的問題。我嘗試了堆棧溢出中提到的其他解決方案,如授予用戶權限..但沒有工作。訪問被拒絕用戶'root'@'localhost'

代碼,用於訪問數據庫:錯誤的

public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{ 
    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 
    out.println("<html>"); 
    out.println("<head><title>Servlet JDBC</title></head>"); 
    out.println("<body>"); 
    out.println("<h1>Servlet JDBC</h1>"); 
    out.println("</body></html>"); 
    // connecting to database 
    Connection con = null; 
    Statement stmt = null; 
    ResultSet rs = null; 
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees","root","root"); 
     stmt = con.createStatement(); 
     rs = stmt.executeQuery("SELECT * FROM employee"); 
     // displaying records 
     while(rs.next()){ 
     out.print(rs.getObject(1).toString()); 
     out.print("\t\t\t"); 
     out.print(rs.getObject(2).toString()); 
     out.print("<br>"); 
    } 
    } catch (SQLException e) { 
     throw new ServletException("Servlet Could not display records.", e); 
    } catch (ClassNotFoundException e) { 
     throw new ServletException("JDBC Driver not found.", e); 
    } finally { 
     try { 
      if(rs != null) { 
       rs.close(); 
       rs = null; 
      } 
      if(stmt != null) { 
       stmt.close(); 
       stmt = null; 
      } 
      if(con != null) { 
       con.close(); 
       con = null; 
      } 
     } catch (SQLException e) {} 
    } 
    out.close(); 
    } 

堆棧跟蹤:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) 
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187) 
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119) 
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:927) 
    com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4686) 
    com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1304) 
    com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483) 
    com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516) 
    com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301) 
    com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834) 
    com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) 
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    java.lang.reflect.Constructor.newInstance(Unknown Source) 
    com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416) 
    com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346) 
    java.sql.DriverManager.getConnection(Unknown Source) 
    java.sql.DriverManager.getConnection(Unknown Source) 
    WebAppAss.DatabaseAccess.service(DatabaseAccess.java:36) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 

什麼能在這種情況下的問題。我試圖創建一個新的數據庫,但這並不起作用。

+0

密碼可能是錯誤的。 –

+0

no it isnt ..我使用相同的密碼在mysql中登錄 –

+0

您能告訴我們您用來授予訪問權限的命令嗎? – Claudio

回答

1

的問題的權限授予根information.schema表。 'root'@'%'沒有任何權限。而且,因爲我使用的是127.0.0.1作爲我的連接地址,所以它給出訪問被拒絕的錯誤。%是ip地址的通配符。因此mysql將[email protected]視爲任何其他ip地址,但不包括localhost。所以只是授予它的權限解決了我的問題..嘗試使用一些像SQLYog等Mysql客戶端。它很容易授予權限,並且也可以查看用戶的權限。

+0

我有相同的錯誤,甚至在添加用戶'root'@'%'後,授予它所有權限也會出現相同的錯誤。訪問拒絕'root'@'localhost'(使用密碼(是)) – kavita

+0

您嘗試將root @ localhost添加到information.schema嗎? –

3

在控制檯中啓動mysql客戶端並執行此查詢:select Host, User from mysql.user;。你必須有一排這樣的:

 
+----------------+------------------+ 
| Host   | User    | 
+----------------+------------------+ 
| localhost  | root    | 
+----------------+------------------+ 

一排「本地主機」,在主機和「根」在用戶。如果你沒有,那是你的問題的原因(用戶

如果你沒有這樣的行,添加一個新用戶與此:

CREATE USER 'appUser'@'localhost' IDENTIFIED BY 'appPassword'; 

改變「APPUSER」的「根」,如果你想要的,但我強烈建議使用其他用戶。然後,通過在MySQL客戶端執行此權限添加到您的新用戶:

GRANT ALL PRIVILEGES ON employees.* TO 'appUser'@'localhost'; 

(再次,變「APPUSER」的「根」,如果你想)

+0

我已經在用戶的根用戶...我創建了一個新用戶,但是您提到的授予權限的命令給出了以下錯誤: 錯誤1064(42000):您在SQL語法中有錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在'previliges on employees。* 到'dbUser'@'localhost''在第1行附近使用正確的語法。 –

+0

您有一個錯字:「previliges」而不是「privileges」 – morgano

+0

該命令運行成功..但仍然是相同的錯誤..我創建的新用戶名爲'dbUser',但仍然當我運行我的應用程序時,它給出同樣的錯誤,即使當我改變時,拒絕訪問'root'的訪問連接字符串中的dbUser的名稱 –

1

試試:

mysql --no-defaults --force --user=root --host=localhost --database=mysql 

更改新密碼:

UPDATE user SET Password=PASSWORD('NEWPASSWORD') where USER='root'; 
FLUSH PRIVILEGES; 
+0

謝謝古斯塔沃。上述查詢幫助我刷新根的權限。 – ankit

相關問題