該程序返回0,需要停止/暫停程序,按下按鈕,然後返回ID。如何在按下按鈕後製作程序返回ID?
static int ID=0;
static String log="";
static String pass="";
static SessionFactory factory;
public static int enterStudent(JPanel panel){
panel.setLayout(null);
JLabel jLabel=new JLabel("Login");
panel.add(jLabel);
jLabel.setBounds(10,10,100,30);
JLabel jLabel1=new JLabel("Password");
panel.add(jLabel1);
jLabel1.setBounds(110,10,100,30);
final JTextArea textArea=new JTextArea();
textArea.setBounds(10,50,100,50);
panel.add(textArea);
final JTextArea textArea2=new JTextArea();
textArea2.setBounds(110,50,100,50);
panel.add(textArea2);
JButton enterButton=new JButton("Enter");
enterButton.setBounds(10,100,200,50);
panel.add(enterButton);
try {
factory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
的ActionListener
enterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e2) {
log=textArea.getText();
pass=textArea2.getText();
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
List students = session.createQuery("FROM Student").list();
for (Iterator iterator =
students.iterator(); iterator.hasNext();){
Student student = (Student) iterator.next();
if((student.getLogin().equals(log))&&(student.getPassword().equals(pass))){
ID=student.getId();//this should be returned
JOptionPane.showMessageDialog(null,"return="+ID);
break;
}
}
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
});
return ID; //returns 0
}
這是登錄功能,檢查登錄並傳遞DATABASE。需要返回學生的ID,但是程序返回0
謝謝你的想法「顯示ID」。 – Ererdar