2017-03-01 83 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

相关问题