Printing SQL Query In PreparedStatement in oracle.jdbc.driver.OraclePreparedStatement - oracle

I need to see the query being sent to Oracle from a Java program. In the PostgreSQL JDBC driver, toString() does the job, but the same does not apply to prepared statements from Oracle JDBC implementation. Any ideas how to achieve that?

Check out Log4Jdbc. This sits between your JDBC driver and the application, logging all DB traffic that goes back and forth. It's driver-agnostic, so need for driver-specific logging code.
Extremely handy, and would be even handier if it supported DataSources, but sadly it doesn't.

I think the getOriginalSql() method returns the String being sent to Oracle.

Related

Consuming cockroachdb changefeed via JDBC

Is it possible to consume "EXPERIMENTAL CHANGEFEED FOR" (core) type queries over JDBC?
Is it possible to consume "CREATE CHANGEFEED FOR" (enterprise) type queries over JDBC?
thanks for your interest in CockroachDB changefeeds. Enterprise changefeeds should work fine with JDBC or any other SQL driver: the CREATE CHANGEFEED statement sets up the changefeed to deliver data to a Kafka or cloud storage target, and immediately returns a job ID that you can use to monitor the health of the changefeed via the SHOW JOBS statement or the web UI.
Core changefeeds work a little differently from other SQL statements: when you issue a CHANGEFEED FOR statement, CockroachDB streams results back indefinitely and never returns unless something goes wrong or the query is canceled. Currently, this streaming behavior isn't implemented in the way that the Postgres JDBC driver expects (see #4035 and the linked work-in-progress PRs), so consuming results using Postgres JDBC cursors won't work. We're working on adding support for this.

Independence though ODBC drivers

Can an application developed with oracle queries in DB layer
Be run on an SQLServer Database with the help of an ODBC driver
Maybe, if you used only ANSI SQL statements. ODBC will happily send the text of the query to the query parser on the server and as long as the server can parse it, it will run.
If, however, you have used anything that's specific to Oracle (and that's a long, long list), then it won't work so well.
All that ODBC provides you is abstraction from the connection details -- the driver, the server name, the port numbers etc.
So, how do you get true independence? Generally, you'll use a query generation library like Hibernate which knows how to translate a query language of some kind (HQL) to the specifics for that particular database (PL/SQL or Transact/SQL).
Short answer: Not reliably.
Longer answer: Not through ODBC, but using a JDBC driver for Microsoft SQL Server then perhaps if the application was developed only with ANSI standard SQL. Usually, that is not the case and some PL/SQL code will have been used. If an equivalent piece of T-SQL can be written then it is possible to port the application. But, to your question, this is largely immaterial to the database connection mechanism.
Addendum: Object Relational Mapping tools usually use dialects to generate database independent queries. Other options include using configuration to select the correct queries at run-time (if you need to support both database types).

Build query for jdbc but dont execute (or other solution to see post param-substituted query)

Im trying to degub a program using Springs's jdbcTemplate and I would really like to view the query. Is there any way to either use the debugger or a db2 watcher to see the outgoing/incoming queries?
Thanks for any help.
You can enable the JDBC trace, e.g. by adding the property traceFile=/path/to/trace.file; to your JDBC URL. More details: http://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.admin.trb.doc/doc/t0020709.html?lang=en
Keep in mind that it affects the driver performance, obviously.

Setting connection info using XQJ with Oracle 11g

I am trying to submit XQuery queries to an Oracle 11g database through their XQJ API.
When I instantiate an oracle.xquery.xqj.OXQDataSource as explained in http://www.oracle.com/technetwork/articles/oem/xquery-jdbc-325944.html, I can submit queries fine except that I haven't found how I can set up the server connection (server name, port, username, password, ...) info:
This datasource claims that it doesn't support setting any property.
It doesn't implement the data source constructor which takes a JDBC connection.
I don't see any non standard method to set such info.
When I try to access some random collection like collection("oradb:/foo") I just get an empty result set even when no server is running, suggesting that the driver doesn't even try to connect.
What have I missed and how can I set the server connection info?
Thanks,
Eric
Thanks to Charles Foster I can answer to my own question: the XQJ implementation from Oracle is an old standalone version from January 2010 that is pretty useless and doesn't interact with Oracle databases.
Despite all the Oracle statements about XQJ, I haven't been able to find any client/server XQJ implementation (except one from DataDirect of course) and the way to submit XQuery queries to Oracle databases appears to be through JDBC, embedded in PL-SQL statements.
It is possible in 12.
XQJ to run queries in Java:
http://docs.oracle.com/database/121/ADXDK/adx_j_xqj.htm#ADXDK99930
XQJ to run queries against the database:
http://docs.oracle.com/database/121/ADXDK/adx_j_xqjxdb.htm#ADXDK136

Spring framework with standard JDBC in java

I have a question and I hope you answer it please.
I want to use Spring framework with Standard jdbc like this
"SELECT usernmae FROM users WHERE username='" + usename + '"
Is it possible to do that I donot want to use prepared statement. It is just sample query.
JDBC is no more or less vulnerable to SQL injection than any other SQL connection mechanism. Vulnerability to SQL injection falls on the hands of the people who write the code. JDBC templates provide a way to manage the connection with the database not necessarily protect you from SQL injection. You are still more than capable of writing crappy code with a JDBC template that is vulnerable to SQL injection.
Edit To address the update to your question. You can do that, there is a simple query method in the JDBC template, but doing so would make you vulnerable to SQL injection. The simple answer: always use prepared statements. The only time you can safely avoid using them is if you are not including any input in the query that can from the user. However, you are far better just using a prepared statement even in those cases on the off chance that your assumption is incorrect or it changes down the road. Always, always, always use prepared statements. There is also some performance benefits to using them as the SQL engine can cache the compiled statement for later use, rather than having to recompile the SQL statement each time.

Resources