目前我有一个解决这个问题的方法是使用一个文本字段和一个组合框,但是这非常不整洁,希望删除文本字段,因为这是将数据输入到 mysql 数据库并检索它,所以我需要能够将结果添加到组合框中,因为它在文本字段中
private void jTextField15KeyReleased(java.awt.event.KeyEvent evt) {
String ThePub = jTextField15.getText();
int publengh = ThePub.length();
if (publengh > 2) {
jTextField15.setVisible(false);
fillpub(ThePub);
}
public void fillpub(String pub) {
Connection con;
ResultSet rs;
PreparedStatement pst;
String thedata;
try {
String cs = "jdbc:mysql://localhost:3306/booksalvation4";
String user = "root";
String password = "letmein";
pub = "'" + pub + "%'";
con = DriverManager.getConnection(cs, user, password);
String query = "select * from publisher where name like" + pub;
pst = con.prepareStatement(query);
rs = pst.executeQuery();
while (rs.next()) {
thedata = rs.getString(2);
jComboBox11.addItem(thedata);
}
} catch (SQLException ex) {
Logger.getLogger(mainJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}