gpt4 book ai didi

java - 使用 java/jdbc 从 JtextField 进行参数传递

转载 作者:行者123 更新时间:2023-12-01 23:39:05 26 4
gpt4 key购买 nike

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

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

最佳答案

您是否忘记了EmployeeID之间的.
“SELECT * FROM MYDB.Employee WHERE Employee.ID =”+idField

你的stacktrace 语法错误意味着你的sql语法是错误的。 第 1 行遇到的“ID” 表示 EmployeeID 之间的语法分析失败。

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

关于java - 使用 java/jdbc 从 JtextField 进行参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18224788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com