[英]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).
有 3 种类型的用户,“position_no”为 0(管理员)、1(经理)、2(职员)。
Relations are correctly set.关系设置正确。
I want to我想要
I already wrote codes for this.我已经为此编写了代码。 My problem is I couldn't detect which user is signed in.
rs.getString("position_no");
我的问题是我无法检测到哪个用户登录。
rs.getString("position_no");
gives error: "unreported exception SQLException" , if I do try-catch, still same.给出错误: “未报告的异常 SQLException” ,如果我尝试捕获,仍然相同。
If I manually define new1 as 0, 1 or 2, it works correctly, but doesn't solve my problem.如果我手动将 new1 定义为 0、1 或 2,它可以正常工作,但不能解决我的问题。 For example I defined for 0, if signed in user is 3, he will see as 0. I don't want this.
例如我定义为 0,如果登录用户是 3,他会看到为 0。我不想要这个。
jframe window of the "see reports": “查看报告”的 jframe 窗口:
phpmyadmin table for users:用户的 phpmyadmin 表:
phpmyadmin table for reports:用于报告的 phpmyadmin 表:
As alternative method, I added getter and setter to login_form.java then set to current user.作为替代方法,我将 getter 和 setter 添加到 login_form.java 然后设置为当前用户。
If I print getter in same file (loginform), it shows correct user position_no.如果我在同一个文件 (loginform) 中打印 getter,它会显示正确的用户 position_no。 If I set as 2 and print getter on other file like clerk.java, it prints "0" instead of 2. Why?
如果我设置为 2 并在其他文件(如 clerk.java)上打印 getter,它会打印“0”而不是 2。为什么?
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:这是 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: login_form 顶部:
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: login_form 中的 getter 和 setter:
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.
如果您不需要使用该参数,则只需将其用作您的第一个 sql 语句。
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.
所以也许检查你是否真的在执行语句,有或没有参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.