Java Questions
Java Questions

Reputation: 7953

how to replace a string value in java

i replace a particular string in a statement like the following

SQL = SQL.replaceAll("CUSTOMER_NUMBER", customer);

this conversion goes as integer but i want to replace this as a string like the following

AND CIMtrek_accountlist_customer_number = '0002538'

but at present it replaces like the following

AND CIMtrek_accountlist_customer_number = 0002538

how to do this in java.

Upvotes: 0

Views: 540

Answers (2)

Deep
Deep

Reputation: 6022

Though you should be using PreparedStatement if you are running SQL, However if placeholder "CUSTOMER_NUMBER" is under your control, It is better to use String.format. See and example here

Upvotes: 1

Jon Taylor
Jon Taylor

Reputation: 7905

Just get it to output the ' as well as the customer variable

SQL = SQL.replaceAll("CUSTOMER_NUMBER", "'" + customer + "'");

However as @jlordo mentioned in a comment, you should look at using prepared statements which will allow you to inject values into a prepared sql statement.

Upvotes: 3

Related Questions