如何发送变量" getId"到另一个JFrame" Student_Info"?

时间:2017-06-05 18:52:59

标签: java

我希望我的student_info.java getId中的JFrame变量的值只能通过它给我null或者没有任何内容的方法,并且构造函数重载它会给出正确的值,但仅限于当我打印该值并使用该值时,或者当我将该值存储到变量中时,它会给出null

  1. student_login.java:

        public String getId;
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                             
        ConnectionProvider con = new ConnectionProvider();
        student_Password = new String(this.student_password.getPassword());
        student_Username= this.student_username.getText();
        String getStudentLoginTableId = "select student_id from Student_Login where student_username='"+student_Username+"' and student_password='"+student_Password+"'";
        Connection con1 = con.getConn();
        try {
            Statement stmt = con1.createStatement();
            ResultSet rs = stmt.executeQuery(getStudentLoginTableId);
            while(rs.next()){
                getId = rs.getString("student_id");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Student_Login.class.getName()).log(Level.SEVERE, null, ex);   }
    }
    public String getid(){
    if("101".equals(getId)){
        JOptionPane.showMessageDialog(null, "yes");
    }
    else{
        JOptionPane.showMessageDialog(null, "no");
    }
    return getId;   }
    
  2. student_info.java:

    String a = new Student_Login().getid();
    JOptionPane.showMessageDialog(null, a);
    String get = "select student_id , student_name from student_info where student_id='"+a+"'";
    ConnectionProvider con = new ConnectionProvider();
    Connection con1 = con.getConn();
    try{
        Statement stmt = con1.createStatement();
        ResultSet rs = stmt.executeQuery(get);
        while(rs.next()){
            i = rs.getString("student_id");
            n = rs.getString("student_name");
        }
        //JOptionPane.showMessageDialog(null, getId);
        jLabel13.setText(i);
        jLabel14.setText(n);
    } catch (SQLException ex) {
        Logger.getLogger(Student_Login.class.getName()).log(Level.SEVERE, null, ex);
    }
    

1 个答案:

答案 0 :(得分:0)

<强>原因:

因为您有两个单独的类,一个是student_login,另一个是student_info。因此,为了获得getId的值,您已经从student_login创建了新对象。

String a = new Student_Login().getid();

记住每次从类创建对象时,该类成员变量值都是对象的个体。换句话说,vales对于您正在创建的每个对象都不常见。

<强>解决方案:

您需要使getId变量对每个对象都是通用的。换句话说,getId值应该在所有对象之间共享。

为此,您可以使用static关键字(修饰符)。

public static String getId;

Learn more about static modifier