当前位置:Gxlcms > 数据库问题 > [转帖]关于Java中SQL语句的拼接规则

[转帖]关于Java中SQL语句的拼接规则

时间:2021-07-01 10:21:17 帮助过:12人阅读

"+e.getName()+" 注意 一共有三组标点符号 (除去 方法函数后面的括号)

 

实现目标语句是这个
注意,这里的java变量是idd

int idd;
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN  ‘" + idd + "‘ AND 10" ;

java中的字符串只能双引号,如果字符串中需要拼接变量,该变量用单引号括起来,然后加两个双引号再加两个加号,中间就是变量。

拼接步骤

  1. 写出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原型,替换为单引,中间加双引,中间加双加号,中间加变量。

[转帖]关于Java中SQL语句的拼接规则

标签:name   符号   引号   gpo   col   select   函数   cin   sql   

人气教程排行