DB2 and ResultSetMetaData - Unable to get column name - java

I am developping an application on WAS 8.0.0.5 that iteracts with a DB2 database.
I am getting the column name using java.sql.ResultSetMetaData call getColumnName() class. On my development WAS everything works great.
ResultSetMetaData rsmd = rs.getMetaData();
String columnName = rsmd.getColumnName(i + 1);
When I try and install on a WAS 8.0.0.6 instead of getting the column name, I get the column index!!!
The driver set for the connection string is com.ibm.db2.jcc.DB2Driver
As I side note, I've confirmed and WAS 8.0.0.5 uses DB2 driver 3.62 (works) and 8.0.0.6 uses 4.12 (doesn't work).
What is wrong?

The behaviour of getColumnName() and getColumnLabel() has changed in the IBM Data Server Driver for JDBC version 4. I believe it now conforms to the JDBC specification. You can use the connection property useJDBC4ColumnNameAndLabelSemantics to modify this behaviour, as explained here: http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.apdv.java.doc/src/tpc/imjcc_c0052593.html.

Thanks for the response.
Unfortunately it wasn't the solution. The behavior was that the column index was being returned, instead of the query's label or column name itself.
The problem was that the db2jcc.jar version, configured on the WAS JDBC resources, was too old (version 3.59) I replaced it for 4.12 and now it works.

Related

ou was not a ResultSet - MS SQL Driver issue with 6.x version and Hibernate

I'm using Hibernate 5.0.10, Java 7 and Microsoft JDBC Driver 4.1 for SQL Server version 4.1.8112.100.
After changing to Microsoft SQL Server JDBC driver 6.4, I'm getting this error:
Current CallableStatement ou was not a ResultSet, but getResultList was called
It fails on the last line on getResultSet
public List<DocListResultExt> getDocuments(DocList doc) {
StoredProcedureQuery query = entityManagerFactory
.createEntityManager()
.createNamedStoredProcedureQuery("getDocList");
query.setParameter(....);
List<Sp_get_doc_list> spList = (List<Sp_get_doc_list>)query.getResultList();
Any idea why it suddenly seems not compatible with the current code?
Thanks #Mark Rotteveel seem adding SET NOCOUNT ONresolved the issue. Kind of doesn't explain why the driver would handle this differently, but that's ok.

Are all JDBC Plugins Compatible?

This is just a theoretical question, but I am building a program that gets data from Facebook using the CDATA JDBC Plugin, I wanted to know if all JDBC Plugins have the same syntax. For example, if I just change the JAR file for the driver to a Twitter one, and change the names of the tables and columns I am accessing, would it still work?
By a plugin I mean a driver, also, to put it more clearly, if I was developing a MySQL app and switched from the stock Connector/J Driver to the CData driver, would I need to change the code?
Until the underlying schema where you store remains same, the use of JDBC driver will yield the same result.
Note: Twitter/FB... both has to support the JDBC Model...
However, if you have changes in Drivers, you can consider using ApacheMetamodel Link for reference
JDBC is a standard that has been established and vetted over the years. As long as the drivers you're working with are written to that standard (which as a CData employee, I can say that ours are) you can expect your code referencing a JDBC driver to be essentially identical, regardless of the manufacturer of the driver or the data source you're connecting to.
//optional, register the driver with the DriverManager
Class.forName(myDriverName).newInstance();
//obtain a Connection instance from the DriverManager
Connection conn = null;
try {
conn = DriverManager.getConnection(myJDBCurl);
//execute a select query
Statement stmt = conn.createStatement();
Result rs = stmt.executeQuery("SELECT foo FROM bar");
} catch (SQLException ex) {
//handle any errors
}
As you can see, the code to utilize the JDBC driver can be generalized with variables to use any driver or to use different connections under a single driver (if, for instance, you wanted to connect to different Facebook accounts).
JDBC is an interesting standard. It was intentionally designed to load the driver at run-time, so no vendor classes are used during compilation.
It also has some JDBC own mechanisms for schema data (DatabaseMetaData), and for such things as doing an INSERT with an autoincrement key, and retrieving that key (getGeneratedKeys).
However the SQL is far from standardized by vendor, despite standardisation efforts. For example just getting the first 10 rows.
Unfortunately the visionaries of JDBC seem no longer to exist.
But it a sound basis for professional usage.

After Oracle upgraded from 10g to 11g , "Oracle-ORA-01722 invalid number" appeared

A java web Project
1.OS:Linux
2.Tomcat upgraded from 1.5 to 1.7
3.Java upgraded from 1.5 to 1.7
4.Oracle upgraded from 10g to 11g
There is a talbe named TBL in oracle and a column named COL defined by NUMBER(38,15).
Before upgrading, there was no problem to put a number(for example:0.123456789012345) into the column or retrieve it by "SELECT TO_CHAR(COL) FROM TBL".
But now,when I use "SELECT TO_CHAR(COL) FROM TBL" , OR "SELECT TO_NUMBER(COL) FROM TBL", the error appears("Oracle-ORA-01722 invalid number").
I confirm the value by “SELECT COL FROM TBL” via Oracle SQL Developer,and it looks like very noraml,just a number like 0.123456789012345.But if I use SQL Plus to confirm the value ,it is blank! This is very strange!
Moreover,when I use the value plus one ,no error encountered but the result is not correct (0.123456789012345 + 1 = 1.123456789012305).
Not every value in that column has the same problem but all value looks like a normal number when I select it via Oracle SQL Developer without TO_CHAR() or TO_NUMBER().
Anyone who has the simliar expierence will help a lot.thank you.

Database Lock while updating table in oracle

My aplication is performing a very simple update on a table :
UPDATE TABLE SET COLUMN = 'XYZ' WHERE PK = 123
The problem is, when Hibernate tries to update the table like this, the table get locked with
ORA event : SQL*Net more data from client.
I tried to replicate the error on my local database but i couldn't.
Does Anybody know what is happening?
Database's version where the error is happening: Oracle Database 10g Release 10.2.0.3.0 - 64bit Production
My local database's version: Oracle Database 10g Release 10.2.0.5.0 - 64bit Production
PS: The column being updated is a CLOB type and the OJDBC driver version is 1.4
Do you know what exactly is being sent to oracle db? I've seen something similar when Hibernate was sending very long sql command and it failed on JDBC driver.
I would suggest starting from getting latest JDBC driver version.
Prepared statement should be used. We should not directly assign the values.
For example:
String updateSQL = UPDATE TABLE SET COLUMN = ? WHERE PK = ? .
PreparedStatement pstmt = null;
pstmt=dbresourceAgent.getPreparedStatement(updateSQL);
pstmt.setString(1,"XYZ");
pstmt.setString(2,"123");
pstmt.executeUpdate();

Connecting to Oracle with JDBC causes queries to return zero rows.

So I have been playing around with querying databases using the standard
Statement s = connection.createStatement();
ResultSet rs = s.executQuery(queryString);
ResultSetMetadata rsmd = rs.getMetaData();
while(rs.next)){
String code = "";
String value = "";
for(int i = 1; i <= rsmd.getColumnCount(); i++){
Object obj = rs.getObject(i);
if(i == 1){
code = obj.toString():
}
else{
label = obj.toString();
}
}
//Store code and labels in map
}
...go on to close statement and move on.
I am trying to select two columns from a table in each instance.
For the most part this works well. When working with MySql & Microsoft Sql databases I get a result set full of data in the table. However when I try to do this with an Oracle database I get an empty result set.
I have tested my query string in the SQL Developer application and it works fine, returns my data. But the result set doesnt contain anything. The resultSet metadata says that it has two columns though. Is there anything I need to do when interacting with an Oracle Database that is different from the other two? Thanks.
If your query works when you run it against the Oracle database, and you know the code works since you've run it against MySQL, then some other things to try are:
1.) Make sure your JDBC connection URL is correct. Are you sure you are connecting to the database that you intend to? (i.e. - the one that would return the rows you expect?)
2.) Take into account credentials. Make sure you are using the same credentials through JDBC that you are when connecting to Oracle directly.
3.) Make sure both connections are being made from the same machine and with the same environment. Oracle drivers rely on environment variables to find a file (I believe it is called tnsnames.ora, or something like that) that contains the alias & connection info. Getting different versions of that file could point you to different Oracle instances.
4.) Try manually specifying your schema name in the query. So instead of select * from my_table use select * from my_schema.my_table. Sometimes Oracle clients will configure their sessions to have default schemas set up in their preferences.
5.) If your are attempting to select data that you've inserted with your Oracle client, make sure you've committed the transaction in your Oracle client so that the data is visible to other sessions.
One last debugging tool to use is to try connecting via the Squirrel DB client. Squirrel is a 100% pure java SQL client that connects to any DB using JDBC. It would be a good test to make sure your JDBC Driver, Connection URL, etc. are all valid.
The database table has records but the JDBC client can't retrieve the records. Means the JDBC client doesn't have the select privileges. Please run the below query on command line:
grant all on emp to hr;

Categories

Resources