从LAN计算机访问带有MySQL DB的Java应用程序

时间:2017-03-01 16:15:47

标签: java mysql swing user-interface

我使用java和带有netbeans的mysql创建了一个桌面学校管理系统应用程序。有3个GUI作为ADMIN GUI,TEACHER GUI和STUDENT GUI。现在我想做的是,用户可以实时访问不同计算机的GUI。我的意思是,当管理员从管理员计算机访问系统时,教师可以通过教师GUI从任何其他计算机访问系统。这里所有计算机都通过LAN网线连接。我怎样才能做到这一点..?请帮忙。 (附上代码)。

  

数据库连接类(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();

        }
    }    

}                       

1 个答案:

答案 0 :(得分:1)

您必须使用服务器的IP认为您的数据库存在于另一台计算机或服务器中,因此您应该使用该计算机的IP来创建连接,因此您应该使用该计算机或服务器的IP而不是localhost。所以你需要使用:

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

注意

您可以使用Preprepd Statement doc代替Statement它更安全,更有帮助,因为它可以导致SQL注入,您可以在此处详细了解SQL Injection