MessageFormat/String.format用法

本文详细介绍了Java中MessageFormat和String.format在SQL语句拼接时的使用方法和注意事项,特别关注了如何正确处理特殊字符如单引号和双引号,以及在实际开发中避免SQL注入攻击的重要性。

MessageFormat用法

String result=MessageFormat.format("select * from t_table where id={0} and name={1}","1","abc");

//result结果

select * from t_table where id=1 and name=abc

但是MessageFormat支持“”但不支持‘符号比如

String result=MessageFormat.format("select * from t_table where id={0} and name=’{1}‘","1","abc");

所以如果我们需要拼接sql语句可以使用String.format()方法


String.format用法


String result=String.format("String result=MessageFormat.format("select * from t_table where id='%s' and name='%s');","1","abc");

//result结果

select * from t_table where id=’1‘ and name=’abc‘
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值