Accessing a protected variable of PreparedStatement

G

Gello Ramello

Hi, the environment is Java 1.5, Tomcat 5.5, Postgres 8.1, Eclipse
3.1.1 and Windows XP Pro.
I want to create a function that reads PreparedStatement attributes.
The ps is passed as generic object function parameter and then casted
to PreparedStatement. I can access to PreparedMetaData and other
stuffs, but not to the _stmt properties and methods (like QueryString,
query parameters etc). In fact _stmt is a protected property of
PreparedStatement. My question is: why, as seen in debug mode, Eclipse
can access it and all its methods and properties?
How can I access to _stmt?

Thank you.
 
D

Daniel Pitts

Hi, the environment is Java 1.5, Tomcat 5.5, Postgres 8.1, Eclipse
3.1.1 and Windows XP Pro.
I want to create a function that reads PreparedStatement attributes.
The ps is passed as generic object function parameter and then casted
to PreparedStatement. I can access to PreparedMetaData and other
stuffs, but not to the _stmt properties and methods (like QueryString,
query parameters etc). In fact _stmt is a protected property of
PreparedStatement. My question is: why, as seen in debug mode, Eclipse
can access it and all its methods and properties?
How can I access to _stmt?

Thank you.
First, Debuggers have special access.
Second, you can use reflection to access protected properties.
Third, Don't use reflection to access protected properties...

Generally, even if the PreparedStatement has a string property which
holds the "statement", it is actually just a place-holder reference
for a statement that is already compiled on your SQL server. Changing
the stmt at best would have no effect, and at worse would cause
undefined behavior. Its a bad idea to violate access constraints,
even if you are able.
 
G

Gello Ramello

Daniel Pitts ha usato la sua tastiera per scrivere :
Generally, even if the PreparedStatement has a string property which
holds the "statement", it is actually just a place-holder reference
for a statement that is already compiled on your SQL server. Changing
the stmt at best would have no effect, and at worse would cause
undefined behavior. Its a bad idea to violate access constraints,
even if you are able.

Thank you for your answer, indeed I want to read only the "statement",
not to change it or its properties. The goal is to read the sql string
and the parameters passed to the statement itself. Do you know any way
to do the job? Just to abuse of your courtesy ;-)
 
R

Robert Klemme

Daniel Pitts ha usato la sua tastiera per scrivere :

Thank you for your answer, indeed I want to read only the "statement",
not to change it or its properties. The goal is to read the sql string
and the parameters passed to the statement itself. Do you know any way
to do the job? Just to abuse of your courtesy ;-)

Since PreparedStatement is an interface you can as well write wrapper
classes and thusly intercept method calls.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top