我在多个类上使用getter和setter访问变量时遇到问题。我抬起头this one,但我很困惑。

我有3个类型的用户:mysql表中的Admin (Position No: 0Manager (Position No: 1)Clerk (Position No:2)

我有SeeReportsAndFeedbacks课。我想通过选择position_no = 0的行来显示所有报告,其中1代表管理员和经理,2代表职员。 if语句已经完成了。

因此,店员只能看到带有position_no=2的报告
经理只能看到带有position_no=0和1的报告
管理员只能看到带有position_no=0和1的报告

请帮我。我在这里呆了很长时间。我的吸气器安装程序有什么问题?

如果我在Login_Form上设置,并调用get在girisyap()函数中显示正确,但是如果我在名为SeeReportsAndFeedbacks的其他类中调用get,它将显示Users () constructor中的第一个初始值,而不是girisyap()函数上的设置值Login_Form

tip值将mysql db中的position_no作为字符串,new1值将if语句解析(转换)为int字符串

screenshot

GIST

用户类别


    public class Users {

    private  int id;
    private String username;
    private String fullname;
    private String password;
    private String phone;
    private String gender;
    private byte[] image;
    private  int position_no;

public Users () {
    setPno(1); //firsst initialize
    //getFullname();
}
public Users (int uid ,String uname, String fname, String upassword, String uphone, String ugender, byte[] uimage, int pno){

this.id = uid;
this.username = uname;
this.fullname = fname;
this.password = upassword;
this.phone = uphone;
this.gender =ugender;
this.image =uimage;
this.position_no = pno;
}
public Users (int pno){

setPno(pno);
}

public int getPno(){
    return position_no;
    }
public void setPno(int pno){
this.position_no = pno;
    }}


SeeReportsAndFeedbacks类(针对该问题,我删除了不相关的功能或其他一些gui东西。

public class SeeReportsAndFeedbacks { // extends javax.swing.JFrame

    //CLIENT client = new CLIENT();
    int new1 = 9999; //testing something
    int PositionNoGetiren;
    //sers loginf = new Users(0, null,null,null,null,null,null,new1);


    public SeeReportsAndFeedbacks() {
        //initComponents();

        Users loginf = new Users();

        PositionNoGetiren = loginf.getPno(); //gets initial value instead of set value on login_form
        System.out.println("Babakingg " + PositionNoGetiren);

        //int ananas = loginf.getPno();

        //fillFeedbackJTable(jTable2);


    }

        public void fillReportJTable(){//JTable table

        //loginf.setPno(2); it works if i manually set but it's useless
      //System.out.println("Loginfvalue in see reports: " + loginf.getPno() + loginf.getUsername());
        //new1 = loginf.getPno(); //not works shows 0

        //see.getNo();

        new1=PositionNoGetiren;

        String selectQuery = "SELECT * FROM `users` WHERE `position_no` = ?";


        if(new1==0){//admin

            selectQuery = "SELECT * FROM `reports`";

        }
        if(new1==1){//manager

            selectQuery = "SELECT * FROM `reports` WHERE `position_no` = 1";

        }
        if(new1==2){//clerk
            selectQuery = "SELECT * FROM `reports` WHERE `position_no` = 2";
        }
        //}


    }}



登录表单

public class Login_Form {

    int positionNoGetiren;

    /**
     * Creates new form Login_Form
     */
    public Login_Form() {

        //initComponents();

        //positionNoGetiren = 9999;

    }

    private void girisyap() {


                //I DELETED ALL DATABASE RELATED THINGS FOR QUESTION.
                //String tip = rs.getString("position_no"); //DETECTS CORRECTLY POSITION NO FROM DATABASE
                String tip = "IT'S rs.getString(\"position_no\")"; //for posting question
                System.out.println(tip);
                int new1 = Integer.parseInt(tip);

                //Users loginf = new Users(new1); //welcome yazisi icin
                Users loginf = new Users(); //ONLY WORKS IN THIS CLASS.

                loginf.setPno(new1); //set user type for reports class BUT IT'S NOT WORKING
                System.out.println("Loginf degeri login_formdaki: " + loginf.getPno());

                //THIS IF IS WORKING CORRECTLY.

                if(new1==0){

                //Admin form = new Admin();
                //form.setVisible(true);
                //form.pack();
                //form.setLocationRelativeTo(null);
              //  form.setExtendedState(JFrame.MAXIMIZED_BOTH);

                }
                if(new1==1){

                //Manager form = new Manager();
                //form.setVisible(true);
                //form.pack();
                //form.setLocationRelativeTo(null);

                }
                if(new1==2){

                //Clerk form = new Clerk();
                //form.setVisible(true);
                //form.pack();
                //form.setLocationRelativeTo(null);
               // form.setExtendedState(JFrame.MAXIMIZED_BOTH);
                }

                //this.dispose();




        }

    private void jButton_LoginActionPerformed(java.awt.event.ActionEvent evt) {
        girisyap();}

    }

最佳答案

您的获取者和安装者没有错。

这很可能是与解析TIP值有关的一个问题,无法将其解析为INT,可能是其浮点值具有类似2.00000004之类的浮点数,或者仅仅是null。尝试编写测试或记录查询返回的值,然后检查这是否是您要查找的值。

10-06 02:27