简体   繁体   中英

Query Not Executing Properly

I am making a Java GUI and on my GUI I have a JTable which is populated directly from database queries. What I am having issues with is adding a date range option to my GUI.

I have 2 JTextFields (startDate & endDate) which the user can type a specified range of dates (Format: DD-MMM-YY) and then the database queries and selected only those files in between the startDate and endDate.

Here is a the section of my code that is supposed to achieve the above.

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();
                }
}

I keep getting error:

ORA-00933: SQL command not properly ended

However I cannot seem to find what is wrong. The problem appears to be my query String however it appears to me that it is ended properly.

Here is the entire stacktrace:

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)

1) I suspect it is because there is no space when you are appending values. Give space after BETWEEN. Space before AND and after AND.

String query1 = "....... FILE_DATE BETWEEN " + "'" + start + "'" + " AND " + "'" + end + "';"

2) Move away from plain SQL and start using PreparedStatement .

That is the space issue, you forgot to add some space after keywords. Try this one..

 String query1 = "SELECT * FROM FILES WHERE USER_ID = 1 AND FILE_DATE 
 BETWEEN " + "'" + start + "'" + " AND " + "'" + end + "';";

You must not pass them like this since they are the loop holes and your code would be vulnerable to sql injection.

Use the PreparedStatement in the right way. Don't concatenate the parameters values in the query string, rather add it as parameter:

//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();

Use PreparedStatement instead of simple statement. You haven't provided any space after BETWEEN and before and after AND which is causing you this error.

我认为“BETWEEN”和“AND”之间的空间问题试试这个

String query1 = "SELECT * FROM FILES WHERE USER_ID = 1 AND FILE_DATE BETWEEN " + "'" + start + "' " + "AND" + " '" + end + "';";

When using JDBC, I've seen many people using a semicolon (;) at the end of the query which is absolutely wrong. Since statement and preparedStatement execute only one query at a time, it is very obvious that you won't need to end a query using semicolon explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM