I have a database table in oracle which has a column of type DATE. It looks like the table below
TABLE1
ID PRODUCT_NAME ITEM_CNT ENTERED_DATE
1 prod1 500 2012-07-01
2 prod2 1000 2012-06-30
in my java code, I want to get the total item_cnt for a certain date range. here is the code sample
String sql = "select sum(item_cnt) from table1 where entered_date between ? and ?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null;
try{
conn = getConnection(url, user, passwd);
pstmt = conn.prepareStatement(sql);
pstmt.setDate(1, java.sql.Date.valueOf(from_date)); //from_date is a string of "yyyy-mm-dd"
pstmt.setDate(2, java.sql.Date.valueOf(to_date)); //to_date is a string of "yyyy-mm-dd"
rset = pstmt.executeQuery();
....
}catch(SQLException e){
//do something
} finally{
//clean up
}
This code was running fine for a while until three days ago, I start getting the following exception at line pstmt.executeQuery();
java.sql.SQLRecoverableException: No more data to read from socket
at oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:1157)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:290)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:884)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1167)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1289)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3584)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3628)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1493)
I tried to search for answers but couldn't find anything that really explains it. then I changed my sql query to
"select sum(item_cnt) from table1 where entered_date between to_date(?, 'yyyy-mm-dd') and to_date(?, 'yyyy-mm-dd')";
and instead of setting date, I changed the prepared statement to the following
pstmt.setString(1, from_date);
pstmt.setString(2, to_date);
Then the exception is gone.
Another confusion is, when I populate my table, I am still using the following
pstmt.setDate(1, java.sql.Date.valueOf(date)); //date is a string of format "yyyy-mm-dd"
and it is still working. only the select statement was giving me exceptions.
Now everything is working but I really want to know why. Anyone knows?
I did upgrade my java to 1.7.0_03-b05 recently. and I am using ojdbc6.jar. The oracle is 11g. Could this be the driver's problem? is ojdbc7 out?
I was facing this exception while working over JDBC with IBM WAS 7.0, I had performed a JCA lifecycle management operation on data source. Which is like controlling the runtime status of the data source. Purge removes the contents of connection pool for the data source. However, in WAS this purging the pool will not affect the ongoing transactions. Check on your side.
Another thing which I performed was; the disk space was full on directory where Oracle was installed, I added extra space over that.
As a best practice, I stay away from java.sql.Date class and use to_date() and to_char() functions while dealing with Dates in Java with Oracle.
Related
Pretty much as the title says, I am trying to put a simple Java time value into my SQL for Oracle but I get the below error when the code runs the query.
Exception:java.sql.SQLSyntaxErrorException: ORA-00920: invalid
relational operator
Long theTime = new java.util.Date().getTime();
StringBuffer sql = new StringBuffer("SELECT...");
...
sql.append(" WHERE from_date <= "+new java.sql.Timestamp(theTime));
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql.toString());
How can I resolve this exception?
Additional information
I should add that for reasons unknown, when using this against Oracle 19c (does't happen on 12c) we are having an issue with PreparedStatement.
This line 'hangs' for 3 hours, before running and returning results, hence why I'm suing the example above.
StringBuffer sql = new StringBuffer("SELECT...");
...
sql.append(" WHERE from_date <= ?");
PreparedStatement ps = conn.getConnection().prepareStatement(sql.toString()); // Hangs here for 3 hours!
Thanks in advance
K.
I have a java servlet application and I'm using a prepared query to update a record in a SQL Server Database table.
Lets say I want to execute UPDATE MyTable SET name = 'test' WHERE id = '10'. (Yes, id is a varchar)
I used the following code to make this happen:
PreparedStatement pstmt = con.prepareStatement("UPDATE MyTable SET name = ? WHERE id = ?");
pstmt.setString(1, getName() );
pstmt.setString(2, getID() );
pstmt.executeUpdate();
I found out that while I was running a JMeter script to simulate 2 users, this statement causes a deadlock in my database.
I wanted to check what my values were in the SQL Profiler so I used the following code, so I could check the values.
String query = String.format("UPDATE MyTable SET name = '%s' WHERE id = '%s' ", getName(), getID() );
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.executeUpdate();
Suddenly my deadlock was gone! It's a shame the last approach is vulnerable to SQL injection.
Is there somebody who can tell me what is going on and/or how to fix it?
Ok I finally found the problem and solution to my problem.
It seemed that the combination of the jTDS JDBC driver with MSSQL was the 'problem'.
This article explained my situation exactly. And with the help of this FAQ I was able to set the datasource to the right configuration.
From what I understand:
If you have statement that uses a String-like index (Like in my situation), the table performs an index SCAN instead of an index SEEK. This causes the whole table to be locked and vulnerable to deadlocks.
I hope this will help other people too.
When I execute a query using PreparedStatement.executeQuery() from a Java application, I get a resultSet object with wrong values for one of the fields. When I use run the same query in SQL developer, then I see the values correctly.
For e.g. In my query output,I am able to see the values as below:
ID_TABLE VALUE_ID
-------------------------
1234 111111
1234 222222
5678 333333
5678 444444
But, in java, when I am iterating the result set in while loop, I have printed logs with rs.getString("ID_TABLE") and rs.getString("VALUE_ID") on a single line as
System.out.println(" Table Id= "+rs.getString("ID_TABLE")+" -Value Id= "+rs.getString("VALUE_ID"));
Here, I am getting in logs as Table Id=5678 -Value Id=222222 which is incorrect. I am always getting the same output in sql developer and it is running in a single thread only. Nothing is running in parallel.
I am using Oracle 12c (version-12.1) and jdk 1.7.0_45.
Do you think it's a Java side problem or Oracle side data fetching problem?
Please let me know if you need additional details.
Please find below my method code snippet:
Connection conn = null;
ResultSet res = null;
String strQuery = "SELECT ID_TABLE,VALUE_ID FROM AppTableDetails";
PreparedStatement pStmt = null;
conn = getConnection();
pStmt = conn.prepareStatement(strQuery);
res = pStmt.executeQuery();
while (res.next())
{
System.out.println("Table Id= "+res.getString("ID_TABLE")+" -Value Id= "+res.getString("VALUE_ID"));
}
One more thing, this error is occurring randomly and not every time when I run the process.
I wish to get user info just like provided by
SELECT SYS_CONTEXT ('USERENV', 'SESSION_USER') FROM DUAL;
and
SELECT SYS_CONTEXT ('USERENV', 'OS_USER') FROM DUAL;
inside a JAVA UDF for Oracle 11g without making a JDBC connection and running these queries to query from DUAL.
I tried System.getProperty("user.name") to read the current OS_user through jvm but I think we are not allowed to fetch information outside the database environment.
More generically, problem statement is to fetch information about the user who has logged into database and using that java UDF (where we need to determine these information) ?
I have found solution to above problem by using the "jdbc:default:connection" which is an internal connection maintained by database itself which is always available. Notice I did not do conn.close(); in the end because this is a shared stream which once closed is closed for all database clients.
public static String doSQL() throws SQLException {
String result = new String();
String q1 = "SELECT SYS_CONTEXT('USERENV','SESSION_USER') FROM DUAL";
Connection conn =
DriverManager.getConnection("jdbc:default:connection");
PreparedStatement ps = conn.prepareStatement(
q1
);
ResultSet rs = ps.executeQuery();
while (rs.next())
result = rs.getString(1);
return "my udf says"+result;
}
I am using a JDBC connection to fetch data from an Access database.
The database design is not my control. In the database there are columns that have "?" included in their names, for example: Open?, Paid?, and lots more.
When I try to fetch data with a PreparedStatement it gives me an error. The query is:
SELECT Open? FROM tblJobList WHERE WeekEnding=?
I also tried to use brackets like [Open?], but the result is the same.
The error I receive is "Too few parameters ..." as I am pushing only one parameter into the PreparedStatement.
I can not use normal statement because of WeekEnding=? as this value is a Timestamp and I could not manage to work it with Statement. Only prepared statement works here.
Can anyone tell me how to use these kind of column names in a PreparedStatement?
use the " character
"SELECT \"Open?\" FROM tblJobList WHERE WeekEnding=?"
tested this against oracle and appears to work with mssqlserver
How to select a column in SQL Server with a special character in the column name?
Just to update this for current technologies:
While the JDBC-ODBC Bridge and Access ODBC were unable to handle a PreparedStatement with a column name containing a question mark, the UCanAccess JDBC driver handles it just fine, as can be confirmed with the following code:
String connectionUrl = "jdbc:ucanaccess://C:/Users/Public/UCanAccessTest.accdb";
Connection conn = DriverManager.getConnection(connectionUrl);
String sql = "SELECT ID, [Open?] FROM tblJobList WHERE WeekEnding=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setDate(1, java.sql.Date.valueOf("2016-01-01"));
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.printf("%d: %s%n", rs.getInt("ID"), rs.getBoolean("Open?"));
}
conn.close();
For more information on UCanAccess, see
Manipulating an Access database from Java without ODBC
I am not sure but you can try // to escape the special meaning of ? and to use it as a normal character. Like:
"SELECT Open//? FROM tblJobList WHERE WeekEnding=?"
You can get something similar to your problem here:
Round bracket in string with JDBC prepared statement
Escaping quotes in MSSQL is done by a double quote, so a '' or a "" will produce one escaped ' and ", respectively.