我已将组合框
链接到MySQL 数据库,以便它按姓名显示员工列表。
问题是,当我点击某个员工时,我想在单独的文本字段中显示他的注册号。
我尝试使用此代码,但不幸的是它不起作用:
public void popcombo(){
try {
String sql ="Select registration,surname,name from employee";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
String surname = rs.getString("surname");
String registration = rs.getString("registration");
String name =rs.getString("name");
StringBuilder st = new StringBuilder();
st.append(surname).append(" ").append(name);
AutoCompleteDecorator.decorate(empcombo);
empcombo.addItem(st);
}
} catch(Exception ty)
{
JOptionPane.showMessageDialog(null,ty);
}
}
private void empcomboItemStateChanged(java.awt.event.ItemEvent evt) {
String p= empcombo.getSelectedItem().toString();
try{
String sql ="Select surname,name,registration from employee where registration='"+p+"'";
rs=ps.executeQuery();
while(rs.next()){
jTextField1.setText(rs.getString("registration"));
}
}catch(Exception tz)
{
JOptionPane.showMessageDialog(null,tz);
}
}
最佳答案
我使用了这段代码:
private void empcomboItemStateChanged(java.awt.event.ItemEvent evt) {
try{
{
String selectedItem = empcombo.getSelectedItem().toString();
String sql ="Select registration from employee where surname='"+selectedItem.split(" ")[0]+"' and name='"+selectedItem.split(" ")[1]+"'";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()){
jTextField1.setText(rs.getString("registration"));
}
}
}catch(Exception gh){
JOptionPane.showMessageDialog(null, gh);
}
}
关于java - 组合框链接到 MySQL 和 Jtextfield,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31634229/