wood burning stoves 2.0*
The moose likes JDBC and the fly likes Can I get the string from preparedStatem.setString()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Can I get the string from preparedStatem.setString()?" Watch "Can I get the string from preparedStatem.setString()?" New topic
Author

Can I get the string from preparedStatem.setString()?

Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
I have a problem - I create my SQL queries dynamically and basing on user input options. So the user has 5 parameters (actually it's more) and he can choose to use some of them (all if he wants) or none and specify their value in the query. So I construct my query String (basic the WHERE conditions) by checking if a parameter was selected and if a value was provided. However now there is the problem of special characters like '. I could try to use replaceAll("'", "\\") but this is quite dull and I know that preparedStatement.setString() does the job better. However for me I would need than to check again if the parameter was provided and if the previous one were also (to specify the poison of ? and connect it to the right parameter). This causes a lot of combinations and does not look elegant.

So my question is - can I somehow receive the string preparedStatement.setString() produces? Or is there a similar function that would do the same job and give me the String so I can put it in the query manually.

Maybe the intro was too long but someone might have a better idea and I wanted to explain why I need it.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Check this link to see how to build dynamics query which "nullable" parameters as stored procedure:
webpage

Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
Worked like a charm! Big Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I get the string from preparedStatem.setString()?
 
Similar Threads
preparedStatement - increase column value
Multiple Columns in a JCombobox
How to query a value typed object
SQL query
issue to pass the dynamic value in query