簡體   English   中英

無法從Java程序連接到數據庫

[英]Can't Connect to Database from Java program

我安裝了MySQL數據庫和JDBC。 我通過My​​SQL命令行客戶端創建了一個名為feedback的數據庫,我想通過java程序訪問該數據庫。

這是我在MySQL命令行客戶端上使用的命令創建數據庫。

create database feedback;
use feedback; 
CREATE USER sqluser IDENTIFIED BY 'sqluserpw'; 

grant usage on *.* to sqluser@localhost identified by 'sqluserpw'; 
grant all privileges on feedback.* to sqluser@localhost; 
CREATE TABLE COMMENTS (id INT NOT NULL AUTO_INCREMENT, 
MYUSER VARCHAR(30) NOT NULL);
INSERT INTO COMMENTS values (default, 'lars');

然后,Java中的代碼:

package de.vogella.mysql.first;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.swing.JOptionPane;


public class MYSQLACCESS {
private Connection connect = null;
  private Statement statement = null;
  private PreparedStatement preparedStatement = null;
  private ResultSet resultSet = null;


  public void readDataBase() throws Exception {

      JOptionPane.showMessageDialog(null, "Before try" );
      try {
      // This will load the MySQL driver, each DB has its own driver
      Class.forName("com.mysql.jdbc.Driver");
      // Setup the connection with the DB
      JOptionPane.showMessageDialog(null, "1" );

      connect = DriverManager.getConnection("jdbc:mysql://localhost/feedback?"
              + "user=sqluser&password=sqluserpw");


      // Statements allow to issue SQL queries to the database
      statement = connect.createStatement();
      // Result set get the result of the SQL query
      resultSet = statement.executeQuery("select * from FEEDBACK.COMMENTS");
    //  writeResultSet(resultSet);

      String my_user = resultSet.getString("MYUSER");
      System.out.println(my_user);
      JOptionPane.showMessageDialog(null, my_user );
    } catch (Exception e) {
        throw e;
      } 

    }
  }

主類:

package de.vogella.mysql.first;
import de.vogella.mysql.first.MYSQLACCESS;

public class Main {
public static void main(String[] args) throws Exception {
MYSQLACCESS first_access = new MYSQLACCESS();
first_access.readDataBase();
}

}

但是,我得到以下異常:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:              
 Access denied for user 'sqluser'@'localhost' to database 'feedback'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
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(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at de.vogella.mysql.first.MYSQLACCESS.readDataBase(MYSQLACCESS.java:30)
at de.vogella.mysql.first.Main.main(Main.java:7)

我按照以下鏈接中的教程

有幫助嗎?

用戶sqluser沒有訪問localhost上的數據庫feedback的權限。

使用mysql命令行工具並將權限授予用戶。

不要忘記刷新權限

通常在遇到此異常時:

用戶'sqluser'@'localhost'拒絕訪問數據庫'反饋'

意味着該用戶沒有足夠的權限來訪問數據庫,我建議你做的是轉到你的SQL shell並編寫這個命令:

show grants;

它會告訴您哪些用戶可以訪問數據庫以及他們的權限級別

如果此用戶似乎沒有訪問數據庫的必要權限,您可以通過在SQL Shell中編寫此查詢來將其授予他

GRANT ALL PRIVILEGES ON *.* TO 'sqluser'@'localhost' WITH GRANT OPTION;

祝好運;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM