我一直在尝试使用 Hibernate 在 Java Swing 应用程序中运行 SAVE 进程。但是我每次都遇到以下异常。
java.lang.NumberFormatException: For input string: "Male"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at frames.user_info_1.saveUserDetails(user_info_1.java:258)
at frames.user_info_1.jButton1ActionPerformed(user_info_1.java:191)
at frames.user_info_1.access$100(user_info_1.java:17)
at frames.user_info_1$2.actionPerformed(user_info_1.java:147)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
这是我使用的代码(用户注册)。
public void saveUserDetails() {
try {
Transaction t = sess.beginTransaction();
String fn = firstname.getText();
String ln = lastname.getText();
String nicno = nic.getText();
String contact = contactno.getText();
String gen = (String) jComboBox1.getSelectedItem();
int gen1 = Integer.parseInt(gen);
String un = username.getText();
String pw = new String(password.getPassword());
User u = new User();
u.setFirstname(fn);
u.setLastname(ln);
u.setNic(nicno);
u.setPhone(contact);
Gender g = (Gender) sess.load(Gender.class, gen1);
u.setGender(g);
Login login = new Login();
login.setUser(u);
login.setUsername(un);
login.setPassword(pw);
String utype = "User";
int utype1 = Integer.parseInt(utype);
UserType utp = (UserType) sess.load(UserType.class, utype1);
u.setUserType(utp);
sess.save(u);
sess.save(login);
t.commit();
System.out.println("User Successfully Saved!");
} catch (NumberFormatException e) {
System.out.println("Number Format Exception");
e.printStackTrace();
}
jComboBox1 用于选择用户的性别,并使用标准搜索加载性别表中的值。
我想问题出在 jComboBox 上。非常感谢您的帮助。提前致谢。