2017-03-01 82 views
1

我做了一個桌面學校管理系統應用程序使用java和MySQL netbeans。有3個GUI作爲ADMIN GUI,TEACHER GUI和STUDENT GUI。現在我想要做的是,用戶可以實時訪問來自不同計算機的GUI。我的意思是,當管理員從管理員計算機訪問系統時,教師可以通過教師GUI從任何其他計算機訪問系統。這裏所有的電腦都通過LAN網線連接。我怎樣才能做到這一點..?請幫忙。 (附代碼)。從局域網計算機訪問Java應用程序與MySQL數據庫

DB連接類(DBConnection.java):

import java.sql.DriverManager; 
import com.mysql.jdbc.Connection; 

public class DBConnection { 
public static Connection dbconmethod() throws Exception{ 

    Connection c; 

    Class.forName("com.mysql.jdbc.Driver"); 

    c=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/ssdb","root","250"); 

    return c; 

} 
} 

登錄JFrame的代碼(Login.java):

private void jBtn_LoginActionPerformed(java.awt.event.ActionEvent evt) {           

     if (txt_UserID.getText().equals("admin")&&txt_Password.getText().equals("123250")){ 
       new AdminHome().setVisible(true); 
       AdminHome.txt_Log.setText(Login.txt_UserID.getText()); 
       this.dispose(); 

     }else if (txt_UserID.getText().equals("teacher")&&txt_Password.getText().equals("123250")){ 
       new TeacherHome().setVisible(true); 
       TeacherHome.txt_Log.setText(Login.txt_UserID.getText()); 
       this.dispose(); 

     }else if (txt_UserID.getText().equals("student")&&txt_Password.getText().equals("123250")){ 
       new StudentHome().setVisible(true); 
       StudentHome.txt_Log.setText(Login.txt_UserID.getText()); 
       this.dispose(); 

     }else{ 

     String uid = txt_UserID.getText(); 
     String pass = txt_Password.getText(); 
     String s1 = ""; 

     String sql = "SELECT status FROM admin_data WHERE user_id='"+uid+"' and password='"+pass+"' UNION SELECT user_role FROM tch_data WHERE user_id='"+uid+"' and password='"+pass+"' UNION SELECT user_role FROM stu_data WHERE user_id='"+uid+"' and password='"+pass+"'"; 

     try { 
     java.sql.Connection c = DBConnection.dbconmethod(); 
     Statement s=c.createStatement(); 
     ResultSet rs= s.executeQuery(sql); 

      while (rs.next()){ 
       s1 = rs.getString(1); 

       }if(s1.equalsIgnoreCase("Active")){ 
         new AdminHome().setVisible(true); 
         AdminHome.txt_Log.setText(Login.txt_UserID.getText()); 
         this.dispose(); 

       }else if(s1.equalsIgnoreCase("PRINCIPAL")||s1.equalsIgnoreCase("D.PRINCIPAL")||s1.equalsIgnoreCase("V.PRINCIPAL")||s1.equalsIgnoreCase("TEACHER")){ 
         new TeacherHome().setVisible(true); 
         TeacherHome.txt_Log.setText(Login.txt_UserID.getText()); 
         this.dispose(); 

       }else if(s1.equalsIgnoreCase("STUDENT")){ 
         new StudentHome().setVisible(true); 
         StudentHome.txt_Log.setText(Login.txt_UserID.getText()); 
         this.dispose(); 

       }else { 
         UIManager.put("OptionPane.messageFont", new Font("Monospaced", Font.BOLD, 22)); 
         JOptionPane.showMessageDialog(rootPane, "UserID or Password Incorrect, Re-check and try again!","Error",JOptionPane.ERROR_MESSAGE); 
         txt_UserID.setText(null); 
         txt_Password.setText(null); 
       } 

     } catch (Exception ex) { 
      ex.printStackTrace(); 

     } 
    }  

}      
+0

那你的意思怎麼樣我能做到嗎? –

+0

你的問題不是GUI問題,mysql是一個數據庫服務器,它提供了被多個遠程客戶端同時訪問的能力。您需要配置數據庫以允許外部連接,這應該受子網的限制,以限制人們可以訪問數據庫以確保安全。更好的解決方案可能是僅允許本地連接併爲遠程客戶端提供Web服務,但這可能超出您嘗試執行的範圍。你需要做的是得到mysql文檔,並閱讀它的訪問配置 – MadProgrammer

回答

1

你有使用t你的服務器的IP地址認爲你的數據庫存在於另一臺計算機或服務器上,所以你應該使用該計算機的IP來創建一個連接,所以你應該使用該計算機或服務器的IP地址,以便你需要使用:

DriverManager.getConnection("jdbc:mysql://ip_of_your_computer:3306/ssdb","root","250"); 

注意

您可以使用Preprepd Statement doc代替Statement它是更安全,更有益的,因爲它可以導致SQL注入,你可以閱讀更多有關在這裏SQL Injection

相關問題