2015-02-24 55 views
0

所有我想要它搜索jtextfeild上的相關名稱和設置值,就像我們在谷歌中得到的一樣。 (提供數據庫中的名稱建議)。這是我使用的代碼,但它得到語法錯誤。當然,它應該得到它。因爲代碼是錯誤的。
如何使用jtextfield的值應用LIKE關鍵字的搜索?

private void txtLnameKeyReleased(java.awt.event.KeyEvent evt) 
    {          
    try { 
     ResultSet rs = JDBC.getData("select lot_name from lot where lot_type='" + company.getSelectedItem().toString() + "' and lot_name like '"+txtLname.getText()+"'% "); 
     if (rs.first()) { 
     txtLname.setText(rs.getString("lot_name")); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 


我真的不知道如何設置與%文本字段的gettext的()方法。請幫幫我。謝謝。


添加錯誤

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%' at line 1 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:525) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
at com.mysql.jdbc.Util.getInstance(Util.java:386) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2619) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2569) 
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1521) 
at Modle.JDBC.getData(JDBC.java:46) 
at lottery.LottryRegistration.txtLnameKeyReleased(LottryRegistration.java:736) 
at lottery.LottryRegistration.access$1800(LottryRegistration.java:22) 
at lottery.LottryRegistration$16.keyReleased(LottryRegistration.java:298) 
at java.awt.Component.processKeyEvent(Component.java:6466) 
at javax.swing.JComponent.processKeyEvent(JComponent.java:2829) 
at java.awt.Component.processEvent(Component.java:6282) 
at java.awt.Container.processEvent(Container.java:2229) 
at java.awt.Component.dispatchEventImpl(Component.java:4861) 
at java.awt.Container.dispatchEventImpl(Container.java:2287) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895) 
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762) 
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027) 
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899) 
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727) 
at java.awt.Component.dispatchEventImpl(Component.java:4731) 
at java.awt.Container.dispatchEventImpl(Container.java:2287) 
at java.awt.Window.dispatchEventImpl(Window.java:2719) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) 
at java.awt.EventQueue.access$200(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:688) 
at java.awt.EventQueue$3.run(EventQueue.java:686) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
at java.awt.EventQueue$4.run(EventQueue.java:702) 
at java.awt.EventQueue$4.run(EventQueue.java:700) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 
+0

難道你把完整的錯誤? – Bono 2015-02-24 20:03:24

+0

@Bono我將錯誤添加到了問題/ – 2015-02-24 20:19:16

+0

1-不要在文本組件上使用關鍵字偵聽器,請使用DocumentListener或ActionListener; 2-使用PreparedStatement – MadProgrammer 2015-02-24 20:37:09

回答

0

百分比在註釋符號的錯誤的一邊:

ResultSet rs = JDBC.getData("select lot_name from lot where lot_type='" 
    + company.getSelectedItem().toString() 
    + "' and lot_name like '" 
    + txtLname.getText() 
    + "%' "); 
+0

它運行時沒有任何錯誤..但沒有放置在jTextfield .. – 2015-02-24 19:35:54

+0

你測試了腳本,並且它在查詢工具中帶回任何東西。 – 2015-02-24 19:38:15

+0

我使用GUI運行這個查詢,什麼都沒有發生? – 2015-02-24 19:48:02