我在Swing應用程序中實現了autocompleteDecorator。我的代碼是這樣的。AutoComplete修飾器
public inventory_purchase() {
initComponents();
AutoCompleteDecorator.decorate(this.combo);
}
public void autocomplete(){
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn= DriverManager.getConnection("jdbc:derby://localhost:1527/C:/jpublisher/pub", "APP", "app");
Statement stmt = conn.createStatement();
String query="SELECT * FROM INVENTORY";
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
combo.addItem(rs.getString("CATEGORY"));
}
}
catch (ClassNotFoundException | SQLException ex) {
JOptionPane.showMessageDialog(null, "Data cannot be loaded. Error!!");
}
}
這個自動完成裝飾工程,只有當我稱之爲
formWindowOpened(java.awt.event.WindowEvent evt){autocomplete();}
此功能如何使用這個自動完成一鍵收聽?像:
private void comboKeyReleased(java.awt.event.KeyEvent evt) {
autocomplete();
}
是否有任何其他簡單的過程來使用從數據庫的自動完成?
你不應該在每次需要發佈語句時都實例化數據庫連接。你應該在你的應用程序啓動時執行此操作,並將它保存在單鍵版本 – amphibient