aspose file tools*
The moose likes JDBC and the fly likes Callable Statement - Fetch IN parameter Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Callable Statement - Fetch IN parameter Values" Watch "Callable Statement - Fetch IN parameter Values" New topic
Author

Callable Statement - Fetch IN parameter Values

Vimlesh Mishra
Greenhorn

Joined: Jul 03, 2014
Posts: 2
I am trying to write an utility method which will take CallableStatement object and log SQL. I am able to get all parameter metadata information from getParameterMetadata method but main challenge is how to fetch IN parameter values which we set explicitly.

example : MySP(?,?)

StringBuilder callStmt = new StringBuilder("{CALL MYSP ?, ?)}");
CallableStatement callableStatement = conn.prepareCall(callStmt.toString());
callableStatement.setString(1, "Test");
callableStatement.registerOutParameter(2, Types.VARCHAR);

//Call to my utility method
myMethod(callableStatement)
Now how can i get value "Test" set as first IN type parameter from CallableStatement object?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Welcome to the Ranch!

This probably isn't possible directly. The JDBC drivers generally don't let you read back parameters that were put in.

There is a possibility to wrap the JDBC classes with your own, which would take care of the logging. You can either do it on your own (as described here), assuming your requirements are fairly simple, or use an existing library, such as Log4JDBC (there might be others).
Vimlesh Mishra
Greenhorn

Joined: Jul 03, 2014
Posts: 2
Thanks martin for quick response, but both solution need code change for all existing codebase. I were looking for some direct approach.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

The approach you strive for is not feasible, as far as I know. I've been trying to achieve the same, and ended up implementing my own wrappers around the JDBC objects (the first approach from my previous post).

I haven't studied it in detail, but the Log4JDBC approach seems pretty lightweight. All you need is to add a jar (or two) to the project, load a different JDBC driver and modify the connection URL. I'm afraid it won't get much simpler than that. If you're deploying your application to a JEE application server, you might need some more tweaking to let it use the Log4JDBC driver, but it should be just the configuration, not code changes.

Other than that, if you need this just in one place of your code, a possibility would be to modify just that part of code to log the parameter values when it is passing them to the CallableStatement instance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Callable Statement - Fetch IN parameter Values