从数据库检索值到jradiobutton

时间:2015-05-15 17:40:03

标签: java mysql

我正在尝试实施薪资系统,而且我在更新员工方面遇到了一些问题。我已将员工的性别存储为我的数据库中的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);
}

1 个答案:

答案 0 :(得分: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);
        }

因为==用于比较哈希值和值& .equals仅用于检查值..