实现目标语句是这个
注意,这里的java变量是idd
int idd; String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ‘" + idd + "‘ AND 10" ;
java中的字符串只能双引号,如果字符串中需要拼接变量,该变量用单引号括起来,然后加两个双引号再加两个加号,中间就是变量。
拼接步骤
- 写出SQL具体语句(无变量都是具体值的),比如上面的sql
//查询picinfos表中id为2到10的id和piUrl值 String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN 2 AND 10" ;
2.此时要将2改为id(int类型变量),只需要在中间修改
,先是删掉2,用单引‘ ‘代替String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ‘ ‘ AND 10" ;
3.接着在单引号中间加上双引" "
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ‘ " " ‘ AND 10" ;
4.接着在双引号中间加上加号+ +
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ‘ " + + " ‘ AND 10" ;
5.最后就是加号中间加上变量idd完事
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ‘ " + idd + " ‘ AND 10" ;
口诀:先SQL原型,替换为单引,中间加双引,中间加双加号,中间加变量。