0

我需要能够通过 3 个键值搜索 derby 数据库。我设置了一个 GUI,并且能够使用数据库中的数据填充它。我已将其设置为可以向任一方向滚动。我遇到的问题是,当我创建一种方法来对其中一个关键字段进行特定搜索时,我的查询失败了。以下是我试图用来促进搜索的方法。我尝试使用语句并将变量连接到字符串语句,并且尝试使用准备好的语句并绑定变量。这两个我都不能上班。我已经在代码中标记了发生错误的位置。

public static void Search(){
    String idField = InitGUI.getidField().getText();
    String sql = ("SELECT * FROM MYDB.Employee WHERE Employee ID = '"+idField +"'");

    try {

//      prepStat = dbCon.prepareStatement(sql);
//      prepStat.setString(1, idField);
//      rs = prepStat.executeQuery();
    rs = stmt.executeQuery(sql);     <== This is where the error occurs.
    SqlStatements.SearchResult(rs.getRow());
    } catch (SQLException e) {

        e.printStackTrace();
    }
}

以下是堆栈跟踪。

Connected to database
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "ID" at line 1, column 44.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown   Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeQuery(Unknown Source)
at Scheduler.DBConnector.Search(DBConnector.java:77)
at Scheduler.myActionListener.actionPerformed(myActionListener.java:20)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.sql.SQLException: Syntax error: Encountered "ID" at line 1, column 44.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 46 more
Caused by: ERROR 42X01: Syntax error: Encountered "ID" at line 1, column 44.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
... 40 more
4

1 回答 1

3

你忘记了and.之间的关系吗?EmployeeID
"SELECT * FROM MYDB.Employee WHERE Employee.ID = "+idField

您的堆栈跟踪Syntax error意味着您的 sql 语法错误。这Encoutered "ID" at line 1意味着语法分析在Employee和之间失败ID

如果您的列名Employee ID,用双引号或反引号对其进行转义,取决于您的 DBMS ;)

于 2013-08-14T06:47:20.117 回答