JDBC ResultSet - Can't retrieve alias column - java

We have the follwing SQL-Query:
select Resa.ResID, Avgångdatum, Avgångtid, AvgångStad, AnkomstTid, AnkomstDatum, AnkomstStad, AntalPlatser, Kostnad,
(select sum(Bokning.AntalBiljetter) from Bokning where Bokning.ResID = Resa.ResID) as EnkelBiljetter,
(select sum(Bokning.AntalBiljetter) from Bokning where Resa.ResID in
(select PaketResa.ResID from PaketResa where PaketResa.PaketID in
(select PaketID from PaketResa where PaketResa.PaketID = Bokning.PaketID))) as PaketBiljetter
from Resa;
When we try to get the columns "Enkelbiljetter" and "PaketBiljetter" in java, with
Integer.parseInt(result.getObject("PaketBiljetter").toString()
It returns a nullpointerexception. We've tried adding the "?useOldAliasMetadataBehavior=true" to the connection-statement but the result is the same. Neither does putting the columnindex (10 and 11) in the getObject-method. We're using the 5.1.19 JDBC driver. Any suggestions?

It could be either your result or getObject(...) returning a null pointer. Since there's a getInt() method you should try this first:
result.getInt("PaketBiljetter")
and see what happens.

Related

DB2 Query issue with Hibernate when checking for values

I have this query, using Hibernate 4 and DB2, running a native query:
select * from TABLE1 t where (:param1 is null or t.NAME = :param1)
That throws exception that null is invalid in the context (since SQL becomes select * from TABLE1 t where (null is null or t.NAME = null))
So I tried:
select * from TABLE1 t where (COALESCE(:param1,'!') = '!' or t.NAME = :param1)
That throws exception when I pass value "JUSTIN" -
"THE VALUE OF INPUT VARIABLE OR PARAMETER NUMBER IS INVALID OR TOO LARGE FOR THE TARGET COLUMN OR THE TARGET VALUE"
Which appears to be cause by the fact that DB2 checks the length of ":param1" against "!".
Is there a way in DB2 to pass dynamic parameters?
I'm starting to hate DB2....
Turns out this works with Hibernate:
select * from TABLE1 t where NVL(:param1, t.NAME) = t.NAME;

How to pass dashed strings to oracle from a query written in java

I have a problem with this query when I pass it to an oracle dbms
SELECT * FROM RD_RBF WHERE REQUEST_ID = 'S2N-F01-000000000001'
because of the dashes in the string the jvm return me this exception
java.sql.SQLException: Fail to convert to internal representation
How can I pass this query to oracle correctly? Thanks a lot
P.S. I'm not shure of the code because I'm using Talend software that generates automatically the code of components but I can post part of the code above
String dbquery_tOracleInput_1 = "SELECT * FROM RD_RBF WHERE REQUEST_ID = 'S2N-F01-000000000001'";
java.sql.ResultSet rs_tOracleInput_1 = null;
try{
rs_tOracleInput_1 = stmt_tOracleInput_1.executeQuery(dbquery_tOracleInput_1);
java.sql.ResultSetMetaData rsmd_tOracleInput_1 = rs_tOracleInput_1.getMetaData();
int colQtyInRs_tOracleInput_1 = rsmd_tOracleInput_1.getColumnCount();
Use toraclerow component.
Query as follows in component:
"SELECT * FROM RD_RBF WHERE REQUEST_ID = ?"
Go to advance setting, select use prepared statement and add 1 paarmeter index.
also select the propagate Query's record set.
tOracleRow_1 -----> tParseRecordSet----->

How to execute multiple depent SQLite Queries (Run SQLite Script) from Java?

I have some data in an SQLite Database. The following queries in SQLiteBrowser give me exactly the ResultSet I want:
With TempTable0 AS (SELECT Source, Date, Value AS Close FROM FinData WHERE Type = 'Close'),
TempTable1 AS (SELECT Source, Date, Value AS Colume FROM FinData WHERE Type = ' Colume')
SELECT DISTINCT TempTable0.Date, Close, Colume FROM TempTable0
JOIN TempTable1 ON TempTable1.Date = TempTable0.Date
Now, I tried to get this working as a single query via a simple executeQuery-Method from Java and read the data in a Matrixlike-DataStructure:
String sql = "With TempTable0 AS (SELECT Source, Date, Value AS Close FROM FinData WHERE Type = 'Close'), TempTable1 AS (SELECT Source, Date, Value AS Colume FROM FinData WHERE Type = ' Colume') SELECT DISTINCT TempTable0.Date, Close, Colume FROM TempTable0 JOIN TempTable1 ON TempTable1.Date = TempTable0.Date";
Connection Conn = DriverManager.getConnection(SQLCommunication.urlDB);
Statement stmt = Conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next() ) {
String res = rs.getString("Close");
DataMatrix.get("Close").add(res);
res = rs.getString("Colume");
DataMatrix.get("Colume").add(res);
}
However, the Resultset returns null.
I suspect this is because, it cannot work with two dependent SQLite-Queries, however, I have no idea, how to solve this.
I am no expert on SQLite and Java interaction and I am really running out of ideas, right now. Do you have any sugestions? (Even maybe a tip on nesting these two statements in one so it can be executed in one shot?)
Thanks so much!
Wiwi

PreparedStatement.executeUpdate() is not working for Oracle

I am using PreparedStatement for executing the update query.
The following is the query:
String callersUpdateQuery = "update W67U999S a set pcrdattim= ? where exists (select b.CRDATTIM, b.RECORDCD, b.CRNODE, b.UNITCD, b.WRKTYPE from W03U999S b where a.PCRDATTIM = ? and a.CCRDATTIM = b.CRDATTIM and a.CRECORDCD = b.RECORDCD and a.CCRNODE = b.CRNODE and a.PRECORDCD = 'F' and a.PCRNODE = '01' and b.WRKTYPE = 'CALLER' and b.UNITCD=? and a.crecordcd='T')";
The below is the java code that should update the records:
preparedStatement = dbConnection.prepareStatement(callersUpdateQuery);
preparedStatement.setString(1,newFolderCrdattim);
preparedStatement.setString(2,crdattim);
preparedStatement.setString(3,businessAreaName.trim());
int j = preparedStatement.executeUpdate();
But preparedStatement.executeUpdate() is not updating the required rows and returning the updated rows count as zero. Weirdly, the same sql query when I execute at the database end, the records are getting updated.
My database is Oracle and the schema of the table that should be updated is below:
Name Null Type
----------- -------- ----------
PCRDATTIM NOT NULL CHAR(26)
PRECORDCD NOT NULL CHAR(1)
PCRNODE NOT NULL CHAR(2)
RECORDTYPE NOT NULL NUMBER(3)
CCRDATTIM NOT NULL CHAR(26)
CRECORDCD NOT NULL CHAR(1)
CCRNODE NOT NULL CHAR(2)
CRDATTIM NOT NULL CHAR(26)
LINKRULE_ID NOT NULL NUMBER(14)
Can anyone guess what's wrong with the code or query?
First, did you check for existence of tuples on the select you're using as condition in where clause?
If there are rows being returned. The issue may be related to the transaction in which you're executing your update statement. Double check for your transaction mode and if it is really being committed.
As a query optimization suggestion I'd change the statement to:
String callersUpdateQuery =
"update W67U999S a
set pcrdattim= ?
where
a.PCRDATTIM = ?
and a.PRECORDCD = 'F'
and a.PCRNODE = '01'
and a.CRECORDCD ='T'
and exists (
select
b.CRDATTIM,
b.RECORDCD,
b.CRNODE,
b.WRKTYPE
from W03U999S b
where
b.CCRDATTIM = a.CRDATTIM
and b.CRECORDCD = a.RECORDCD
and b.CCRNODE = a.CRNODE
and b.WRKTYPE = 'CALLER'
and b.UNITCD=?
)";
That way you will be first reducing the tuples from a then use it to narrow the b tuples only to those that match.
Oracle CHAR type is the culprit here. The columns that I want to update are of type CHAR. That's causing the issue. This link helped me in figuring out the solution: Oracle JDBC and Oracle CHAR data type

How can a jdbcTemplate execute an update statement and return variable

I am newbie to work on spring, and now I got a query but not sure how to use jdbcTemplate to work on it, the query is as follow:
UPDATE table set Seq = #seq := Seq + 1 WHERE id= ?; SELECT #seq as total;
I try to use jdbcTemplate.executeQuery but it will shows up error:
Can not issue data manipulation statements with executeQuery().
I try to use jdbcTemplate.executeUpdate, but seems it only return affected row
Is there anyone know how I can achieve this query and return the updated value?
You should be able to execute the UPDATE statement first using update and then execute the SELECT statement using queryForObject. The variable is persisted across the session bound to the same connection.
jdbcTemplate.update("UPDATE table set Seq = #seq := Seq + 1 WHERE id= ?", yourId);
int total = jdbcTemplate.queryForObject("SELECT #seq as total", Integer.class);

Categories

Resources