java - 从局域网计算机访问带有 MySQL DB 的 Java 应用程序

标签 java mysql swing user-interface

<分区>

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

DB Connection Class(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;

 }
}

Login JFrame Code(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();

        }
    }    

}                       

最佳答案

您必须使用服务器的 IP 考虑到您的数据库存在于另一台计算机或服务器中,因此您应该使用该计算机的 ip 来创建连接,因此您应该使用该计算机或服务器的 IP 而不是本地主机所以你需要使用:

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

注意

您可以使用 Preprepd Statement doc它比 Statement 更安全、更有用,因为它可能导致 SQL 注入(inject),您可以在此处阅读更多信息 SQL Injection

关于java - 从局域网计算机访问带有 MySQL DB 的 Java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42536751/

相关文章:

mysql - SQL:插入过期行

java 如何将字符串发送到服务器(jave.net/sockets)

java - 将 JPanel 和 JScrollingPane 添加到 JFrame

java - 颜色.xml : Content is not allowed in trailing section

java - 通过 STS eclipse 连接到 ClearCase(CCRC) 时出错

mysql - 什么可能导致此 Rails 方法错误?

java - 使用 TableColumnModelListener 时如何知道哪些列已调整大小

java - 公共(public)最终字段的序列化

java - Java 8 的队列方法

mysql - "Localhost Error establishing a database connection "