使用来自 JtextField 的 java/jdbc 进行参数传递
Posted
技术标签:
【中文标题】使用来自 JtextField 的 java/jdbc 进行参数传递【英文标题】:Parameter Passing using java/jdbc from a JtextField 【发布时间】:2013-08-14 06:36:35 【问题描述】:我需要能够通过 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
【问题讨论】:
a) 发生什么错误?如果您有堆栈跟踪,请添加它。 b) 您的 Employee.ID 是数据库中的字符串字段吗?如果不是,您可能需要转换为整数(即不要在第一个查询中使用引号或在替代方法中使用preStat.setInteger()
)
请发布e.printStackTrace()
的输出
Nit Pick - 您应该将 idField
传递给 Search
方法。你应该使用PreparedStatement
。你应该关注Java Naming Conventions
上次我检查时,列名没有空格。 Employee ID
看起来不对
【参考方案1】:
你忘了Employee
和ID
之间的.
吗?"SELECT * FROM MYDB.Employee WHERE Employee.ID = "+idField
您的堆栈跟踪Syntax error
表示您的 sql 语法错误。 Encoutered "ID" at line 1
表示 Employee
和 ID
之间的语法分析失败。
如果您的列名称为 Employee ID
,请使用双引号或反引号对其进行转义,这取决于您的 DBMS ;)
【讨论】:
我对整个数据库很陌生。 Employee ID 列中似乎有一个空格。这不可能吗? 在检查该列之后,Employee ID 实际上确实有一个空格。这不妥吗? @Keith 什么数据库?您可能需要“引用”列名 见builds.apache.org/job/Derby-docs/lastSuccessfulBuild/artifact/… - 你需要用双引号引用名字。更好的是,如果可能的话,重命名列并删除空格。并且,使用绑定变量方法来避免 SQL 注入问题。 @Keith 查看我编辑的答案,正如 Andreas 所说,您需要用双引号或反引号引用名称(取决于您的 DBMS)以上是关于使用来自 JtextField 的 java/jdbc 进行参数传递的主要内容,如果未能解决你的问题,请参考以下文章