2015-02-24 72 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