简体   繁体   中英

Java can't read one of data ResultSet for if statement, getter returns "0"

I have "users" and "reports" tables. There are 3 types of users, with "position_no" 0 (admin), 1 (manager), 2 (clerk).

Relations are correctly set.

I want to

  • show reports with position_no 1 and 2 to admin.
  • show reports with position_no 1 and 2 to manager.
  • show reports with position_no 2 to clerk.

I already wrote codes for this. My problem is I couldn't detect which user is signed in. rs.getString("position_no"); gives error: "unreported exception SQLException" , if I do try-catch, still same.

If I manually define new1 as 0, 1 or 2, it works correctly, but doesn't solve my problem. For example I defined for 0, if signed in user is 3, he will see as 0. I don't want this.

jframe window of the "see reports": jframe 窗口

phpmyadmin table for users:用户表

phpmyadmin table for reports:报告表

As alternative method, I added getter and setter to login_form.java then set to current user.

If I print getter in same file (loginform), it shows correct user position_no. If I set as 2 and print getter on other file like clerk.java, it prints "0" instead of 2. Why?

public void fillReportJTable(JTable table){       

    loginf.setUserTip(0);
  //System.out.println("Loginf value: " + loginf.getUserTip()); //getter shows 0 for all users
    //int new1 = loginf.getUserTip(); //temporary non-working solution with getter setter
    PreparedStatement st;
    ResultSet rs = null;
    String selectQuery = "SELECT * FROM `users` WHERE `position_no` = ?";

    //String selectQuery = "SELECT * FROM `reports`";

    //if(rs.next()){ //it gives error. works without it.

    //this gets position no from db then goes to if statement 

    String tip = rs.getString("position_no");
    //System.out.println("new1 degeri: " + new1);
    int new1 = Integer.parseInt(tip);

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

    try {
        st= mcas.getConnection().prepareStatement(selectQuery);
        rs= st.executeQuery();

        DefaultTableModel tableModel = (DefaultTableModel)table.getModel();

        Object[] row;
        while(rs.next()){
            row = new Object[5];
            row[0] = rs.getInt(1); // productid
            row[1] = rs.getString(2); //product name
            row[2] = rs.getString(3); //report topic
            row[3] = rs.getString(4); //report text
            row[4] = rs.getString(5); //position_no

            tableModel.addRow(row);
        }
    } catch (SQLException ex) {
        Logger.getLogger(CLIENT.class.getName()).log(Level.SEVERE, null, ex);
    }
}

here is some code from login_form.java:

            String tip = rs.getString("position_no");

            System.out.println(tip);
            int new1 = Integer.parseInt(tip);

            Login_Form loginf = new Login_Form(); //welcome yazisi icin
            //int girisdegeri = new1;
            loginf.setUserTip(new1); //reports icin user tip
            System.out.println("Loginf degeri: " + loginf.getUserTip());

login_form top:

public class Login_Form extends javax.swing.JFrame {

private String publicusername; //username get set almak icin lazim
private int publicusertip; //user tipi alma see reports icin

Getter and setters in login_form:

public int getUserTip() {
return publicusertip;
}

public void setUserTip(int newName) {  // Usertip Setter    
this.publicusertip = newName;
}  

It looks like you're defining a prepared statement with a parameter, but not actually setting that parameter. If you don't need to use the parameter then just use this as your first sql statement.

SELECT * FROM `users`

I'm also assuming there's some paraphrasing going on here, because it doesn't look like you're actually getting a result set from your current or commented code. So maybe check that you're actually executing the statement, with or without a parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM