2015-04-22 23 views
1

我爲我們公司創建了一個應用程序。起初,我試圖在MySQL-xampp的數據庫中創建沒有密碼的密碼,並且它工作正常。現在,我的問題是我把密碼在我的數據庫,我的應用程序現在已經拋出了我這個錯誤,每當我登錄:Netbeans和mysql通信

SQL值java.sql.SQLException:拒絕訪問用戶 「根」 @'CITSP- MOB7' (使用密碼:YES)

我也得到一個錯誤的連接我的數據庫:

無法連接。無法建立與 的連接jdbc:mysql // localhost:3306/mydb using com.mysql.jdbc.Driver(Access denied for user'root'@'CITSP-MOB7'(using password:YES))。

這裏是我的數據庫連接代碼:

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
static final String DB_URL = "jdbc:mysql://192.168.1.112:3306/citsp"; 
static final String USER = "root"; 
static final String PASS = "mypassword"; 

try{ 
    Class.forName("com.mysql.jdbc.Driver"); 
    }catch(ClassNotFoundException ex){ 
      } 
    //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 statement..."); 
    stmt = conn.createStatement(); 

有人可以幫我解決連接問題? 您的回答將不勝感激。提前致謝! :)

+0

你在你的數據庫設置一個密碼,讓您的應用程序知道它應該在訪問密碼之前知道密碼。請發佈您的java代碼[只有與mysql部分的連接] – 1000111

+0

首先,您應該設置除root以外的其他用戶。 –

+0

Hi @Subrata Dey,這是我的代碼:static final String JDBC_DRIVER =「com.mysql.jdbc.Driver」; static final String DB_URL =「jdbc:mysql:// localhost:3306/citsp」; static final String USER =「root」; static final String PASS =「mypassword」; – nhix

回答

0

請測試在mysql中創建一個新用戶,如果這適合你。

首先在mysql中創建一個新用戶: 以root身份從mysql命令行登錄。然後逐個運行以下命令。

MySQL的

  1. CREATE USER 'mydbuser' @ 'localhost' 的IDENTIFIED BY 'ASDF';

  2. 授予特權 TO'mydbuser'@'localhost';

  3. FLUSH PRIVILEGES;

更改您上面的Java代碼如下: 我假定你的數據庫名稱爲'citsp'

JAVA

 String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
     String DB_URL = "jdbc:mysql://localhost/citsp"; 
     String USER = "mydbuser"; 
     String PASS = "asdf"; 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
     } catch (ClassNotFoundException ex) { 
      //JOptionPane.showMessageDialog(null, "There's an error connecting with the database. Please contact your administrator."); 
     } 
     //STEP 3: Open a connection 
     System.out.println("Connecting to database..."); 
     java.sql.Connection conn = null; 
     try { 
      conn = DriverManager.getConnection(DB_URL, USER, PASS); 
     } catch (SQLException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
      ex.printStackTrace(); 
     } 

     //STEP 4: Execute a query 
     System.out.println("Creating statement..."); 
     try { 
      Statement stmt = conn.createStatement(); 
      String query = ""; 
      ResultSet rs = stmt.executeQuery(query); 
      while(rs.next()){ 
       // Iterate through the result 
      } 
     } catch (SQLException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } 
+0

它仍然不能解決我的問題。但非常感謝你的幫助。 – nhix

+0

@nhix,您是否能夠在給定的命令之後在mysql中創建一個新用戶?你現在得到什麼錯誤信息? – 1000111

+0

是的,我已經能夠使用您提供的命令創建一個新用戶。同樣的錯誤,根改爲mydbuser。 :( – nhix