java sql语句怎么写
关于Java中SQL语句的拼接规则
实现目标语句是下面这个,注意,这里的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改为id(int类型变量),只需要在中间修改,先是删掉2,用单引' '代替
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ' ' AND 10" ;
接着在单引号中间加上双引" "
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ' " " ' AND 10" ;
接着在双引号中间加上加号+ +
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ' " + + " ' AND 10" ;
最后就是加号中间加上变量idd完事
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ' " + idd + " ' AND 10" ;
口诀:先SQL原型,替换为单引,中间加双引,中间加双加号,中间加变量。
那么java的字符串到底是怎样转化为SQL语句的呢
如图所示
编程界网,大量的免费Java入门教程,欢迎在线学习!