【问题标题】:How to apply LIKE keyword's search using jtextfield's value?如何使用 jtextfield 的值应用 LIKE 关键字的搜索?
【发布时间】:2015-02-24 19:23:09
【问题描述】:

我希望它在 jtextfeild 上搜索相关名称并设置值,就像我们在 google 中一样。 (给出您在数据库中的名称建议)。这是我使用的代码,但出现语法错误。当然应该得到它。因为代码是错误的。

 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)

【问题讨论】:

  • 你能把完整的错误放上去吗?
  • @Bono 我将错误添加到问题/
  • 1- 不要在文本组件上使用关键监听器,使用 DocumentListener 或 ActionListener; 2- 使用 PreparedStatement
  • 1.行。 2.我使用MVC模式。所以我在模型包的我的 JDBC 类中插入所有 JDBC 查询..
  • @MadProgrammer 顺便问一下,修复此错误的正确查询是什么?

标签: java mysql jtextfield


【解决方案1】:

评论符号错误一侧的百分比:

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

【讨论】:

  • 您是否在查询工具中测试过该脚本,并且它是否带回了任何东西。
  • 我使用 GUI 运行此查询,但没有任何反应?
  • 尝试选择前 10 个 lot_name ......使用 like 通常并不理想,因为它通常是最便宜/性能最差的选项。
  • 不一定,但从比较来看,往往是最慢的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多