我正在制作一个Java GUI,在我的GUI上,我有一个直接从数据库查询填充的JTable。我遇到的问题是在我的GUI中添加日期范围选项。
我有2个JTextFields(startDate& endDate),用户可以键入指定的日期范围(格式:DD-MMM-YY),然后数据库查询并仅选择startDate和endDate之间的那些文件。 / p>
这是我的代码部分,应该实现上述目标。
public void actionPerformed(ActionEvent arg0) {
String start = startDate.getText();
String end = endDate.getText();
try {
String query1 = "SELECT * FROM FILES WHERE USER_ID = 1 AND FILE_DATE BETWEEN" + "'" + start + "'" + "AND" + "'" + end + "';";
pat = conn.prepareStatement(query1);
rs = pat.executeQuery();
tableData.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}
}
我一直收到错误:
ORA-00933:SQL命令未正确结束
然而我似乎无法找到错误。问题似乎是我的查询字符串,但在我看来它已正确结束。
这是整个堆栈跟踪:
java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:886)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1175)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1296)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3657)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1495)
at ManagementGUI$6.actionPerformed(ManagementGUI.java:481)
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$200(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.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)
答案 0 :(得分:0)
1)我怀疑这是因为你附加值时没有空间。在BETWEEN之后给予空间。 AND之前和之后的空间。
String query1 = "....... FILE_DATE BETWEEN " + "'" + start + "'" + " AND " + "'" + end + "';"
2)离开普通SQL并开始使用PreparedStatement。
答案 1 :(得分:0)
这是空间问题,你忘了在关键字之后添加一些空格。试试这个..
String query1 = "SELECT * FROM FILES WHERE USER_ID = 1 AND FILE_DATE
BETWEEN " + "'" + start + "'" + " AND " + "'" + end + "';";
你不能这样传递它们,因为它们是循环漏洞,你的代码很容易被sql注入。
答案 2 :(得分:0)
以正确的方式使用PreparedStatement
。不要在查询字符串中连接参数值,而是将其添加为参数:
//use ? to refer to parameters in the PreparedStatement
String query1 = "SELECT * FROM FILES WHERE USER_ID = 1 AND FILE_DATE BETWEEN ? AND ?";
pat = conn.prepareStatement(query1);
//create a SimpleDateFormat instance that will parse each String
//and convert them into java.util.Date instances
//use the proper format to parse your dates
SimpleDateFormat sdf = new SimpleDateFormat("...");
//add the arguments to the PreparedStatement
pat.setTimestamp(1, new java.sql.Timestamp(sdf.parse(start).getTime()));
pat.setTimestamp(2, new java.sql.Timestamp(sdf.parse(end).getTime()));
rs = pat.executeQuery();
答案 3 :(得分:0)
使用PreparedStatement而不是简单语句。 在BETWEEN之后以及之前和之后,您还没有提供任何空间,这会导致您出现此错误。
答案 4 :(得分:0)
我认为“BETWEEN”和“AND”之间的空间问题试试这个
String query1 = "SELECT * FROM FILES WHERE USER_ID = 1 AND FILE_DATE BETWEEN " + "'" + start + "' " + "AND" + " '" + end + "';";
答案 5 :(得分:0)
使用JDBC时,我看到很多人在查询末尾使用分号(;),这是绝对错误的。由于statement和prepareStatement一次只能执行一个查询,因此很明显,您不需要显式结束使用分号结束查询。