2016-11-05 52 views
0

我有一個問題,我想創建一個「全局數據庫」,應該可以從任何地方的所有計算機訪問,但我找不到解決方案。我使用MAMP和phpMyAdmin創建了一個數據庫,並且它只能在我的計算機上正常工作,但是當我嘗試從其他計算機登錄時,我收到消息「java.sql.SQLException:Acces denied for user」rood @ localhost「(使用密碼:是)」。從本地到全局數據庫

這是我的數據庫連接代碼,它是一個登錄框架。

//Login button 
    JButton btnNewButton = new JButton("Login"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) 
     { 
      try { 

//    connection to databse 
       Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myusers", "root", "root"); 

       String query = "select * from brugere where username=? and password=?"; 
       PreparedStatement myStnt = myConn.prepareStatement(query); 
       myStnt.setString(1, textField.getText()); 
       myStnt.setString(2, passwordField.getText()); 

////   Create statement 
       ResultSet myRs = myStnt.executeQuery(); 
//     
       int count = 0; 
       while (myRs.next()) { 
        count = count + 1; 
       } 

       if (count == 1) { 
        myRs.close(); 
        myStnt.close(); 
        myConn.close(); 

        MainMenu mm = new MainMenu(); 
        mm.setVisible(true); 
        frame.dispose(); 

        frame.dispose(); 
       } 
       else if (count > 1) 
       { 
        JOptionPane.showMessageDialog(null, "Duplicate Username and password"); 
       } 
       else 
       { 
        JOptionPane.showMessageDialog(null, "Sorry wrong username or password"); 

       }   

      }catch (Exception e){ 
       JOptionPane.showMessageDialog(null, e); 
      } 
     } 
    }); 
    btnNewButton.setBounds(288, 96, 89, 67); 
    frame.getContentPane().add(btnNewButton); 

回答

0

你的問題不是在你的代碼,你需要在你的數據庫上創建新用戶 「(從任何機器accessable)」,看圖片:

picture

,因爲的帳戶只能從本地主機訪問。

我希望這可以幫到你。好運。