我试图从两个不同的表中调用两组数据以显示在JTextArea(jtaDisplay
)中。第一个表(emp_db
)将获取员工编号,姓名和姓氏。第二个表(sec_clearance
)将获得员工安全许可级别。
该方法放置在构造函数中,因此它将在帧启动时执行,但每当我运行该帧时,它都不会显示数据。没有出现错误消息,堆栈跟踪不显示任何错误消息。
我在方法内的不同位置放置了一个JOptionPane,以查看问题的确切位置,并发现while(rs.next())
语句未执行,因为JOptionPane显示在while
语句之外但不在其中。
以下是我目前的代码:
try
{
String user = txtEmpTitle.getText();
String encuser = encrypt(user); //encrypting employee number with AES to read in database
String getEmpNum = "Select * from emp_db where emp_num = '" + encuser + "'";
String getSecLevel = "select secLevel from sec_clearance where emp_num = '" + encuser + "'";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/employee_database","root","pass123");
Statement stmt=conn.createStatement();
Statement stmt2=conn.createStatement();
ResultSet rs = stmt.executeQuery(getEmpNum);
ResultSet rs2 = stmt2.executeQuery(getSecLevel);
while(rs.next() && rs2.next())
{
String empNum = rs.getString("emp_num");
String empName = rs.getString("fname");
String empSname = rs.getString("sname");
String empSecLevel = rs2.getString("secLevel");
//decrypting data in database
String decNum = EmpEditDB.decrypt(empNum);
String decName = EmpEditDB.decrypt(empName);
String decSname = EmpEditDB.decrypt(empSname);
String decSecLevel = EmpEditDB.decrypt(empSecLevel);
jtaDisplay.setText("Employee number: " + decNum +
"\nEmployee name: " + decName + " " + decSname +
"\nSecurity clearance: " + decSecLevel);
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e);
}
如何获取while语句中的代码以在JTextArea中执行和显示数据?
答案 0 :(得分:0)