gpt4 book ai didi

java - 从数据库检索值到 jradiobutton

转载 作者:行者123 更新时间:2023-11-29 22:26:01 27 4
gpt4 key购买 nike

我正在尝试实现工资系统,但在更新员工时遇到了一些问题。我已将员工的性别作为 varchar 存储在我的数据库中。当我输入员工 ID 并单击搜索按钮时,我想从数据库中获取该信息,并根据数据将其显示在男性/女性单选按钮中。代码中没有错误。但问题是,当我尝试这样做时,只有男性按钮出现,这是默认的。即使数据库上的数据显示“女性”,也不会选择“女性”单选按钮。有人可以帮我解决这个问题吗?下面是我的代码。

package AppPackage;

import static AppPackage.AddEmployeeGUI.convertUtilDateToSqlDate;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class UpdateEmployeeGUI extends javax.swing.JFrame {

Connection conn=null;
PreparedStatement pst=null;
ResultSet rs=null;

public UpdateEmployeeGUI() {
initComponents();
conn=MySQLConnect.ConnectDb();
}
private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql = "SELECT * FROM EmployeeInfo WHERE EmployeeID =?";
pst=conn.prepareStatement(sql);
pst.setString(1,EmployeeIDSearchField.getText());

rs = pst.executeQuery();
if(rs.next()) {
String ID = rs.getString("EmployeeID");
EmployeeIDField.setText(ID);
String FN = rs.getString("FirstName");
FirstNameField.setText(FN);
String LN = rs.getString("LastName");
LastNameF.setText(LN);
String GN = rs.getString("Gender");
if (GN =="Female"){
MaleRadio.setSelected(false);
FemaleRadio.setSelected(true);

}
else{
FemaleRadio.setSelected(false);
MaleRadio.setSelected(true);
}
String CN = rs.getString("ContactNo");
ContactNoField.setText(CN);
String EM = rs.getString("Email");
EmailField.setText(EM);
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
Date dateValue = null;
try {
dateValue = date.parse(rs.getString("DateOfJoin"));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
jDateChooser.setDate(dateValue);
String Des = rs.getString("Designation");
DesignationComboBox.addItem(Des); //getItemAt(int index);
String BS = rs.getString("BasicSalary");
BasicSalaryField.setText(BS);

}
} catch (SQLException e ) {
JOptionPane.showMessageDialog(null, e);
}
}

private void UpdateEmployeeButtonActionPerformed(java.awt.event.ActionEvent evt) {
// UPDATE EmployeeInfo SET FirstName='?', LastName='?', LastName='?', Gender='?', ContactNo='?',Email='?', DateOfJoin='?', Designation='?' WHERE EmployeeID='?';
try{

String sql1 = "UPDATE EmployeeInfo SET FirstName=?, LastName=?, Gender=?, ContactNo=?, Email=?, DateOfJoin=?, Designation=?, BasicSalary=? WHERE EmployeeID=?";
pst=conn.prepareStatement(sql1);
pst.setString(1,FirstNameField.getText());
pst.setString(2,LastNameF.getText());
pst.setString(3,GenderC);
pst.setString(4,ContactNoField.getText());
pst.setString(5,EmailField.getText());
pst.setDate(6,convertUtilDateToSqlDate(jDateChooser.getDate()));
pst.setString(7,DesignationComboBox.getSelectedItem().toString());
pst.setString(8,BasicSalaryField.getText());
pst.setString(9,EmployeeIDField.getText());

pst.execute();

String sql2 = "UPDATE employeebasicsalary SET BasicSalary=? WHERE EmployeeID=?";
pst=conn.prepareStatement(sql2);
pst.setString(1,BasicSalaryField.getText());
pst.setString(2,EmployeeIDField.getText());
pst.execute();

JOptionPane.showMessageDialog(null, "Successfully updated!");


}catch(Exception e){
JOptionPane.showMessageDialog(null, e);

}
}

private void FemaleRadioActionPerformed(java.awt.event.ActionEvent evt) {
GenderC = "Female";
}

private void MaleRadioActionPerformed(java.awt.event.ActionEvent evt) {
GenderC = "Male";
}

private void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {
EmployeeIDField.setText(null);
FirstNameField.setText(null);
LastNameF.setText(null);
MaleRadio.setSelected(true);
FemaleRadio.setSelected(false);
ContactNoField.setText(null);
EmailField.setText(null);
jDateChooser.setCalendar(null);
BasicSalaryField.setText(null);
DesignationComboBox.setSelectedIndex(0);
}

最佳答案

替换行

if (GN =="Female"){
MaleRadio.setSelected(false);
FemaleRadio.setSelected(true);

}
else{
FemaleRadio.setSelected(false);
MaleRadio.setSelected(true);
}

        if (GN.equals("Female")){
MaleRadio.setSelected(false);
FemaleRadio.setSelected(true);
} else {
FemaleRadio.setSelected(false);
MaleRadio.setSelected(true);
}

as == 用于比较哈希和值,.equals 仅用于检查值。

关于java - 从数据库检索值到 jradiobutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30265631/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com