简体   繁体   English

如何在Java中替换字符串值

[英]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. 如何在Java中做到这一点。

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. 但是,正如@jlordo在评论中提到的那样,您应该查看使用准备好的语句,该语句将允许您将值注入到准备好的sql语句中。

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. 尽管如果您正在运行SQL,则应该使用PreparedStatement ,但是,如果占位符"CUSTOMER_NUMBER"在您的控制之下,则最好使用String.format。 See and example here 在这里查看和示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM