双引号应该是为了构造字符串而添加的,而单引号则是查询条件值。 例如select * from user where name='张三' 在java中会这样出现 String sql = "select * from user where name='" + "张三" + "'"
就是拼字符串 String sql="select * from table where a='1'" +" and b='2'"+"and c='3' ";“”里面再套引号,就用单引号,用+一般是为了防止sql一行过长,就用“+”来换行,其实就是一个字符串相加的运算,加起来还是一个String型。细心点不难理解
数据库中使用的单引号 字符串使用的是双引号。双引号需要转义,\" 例如select * from usres where username='"+zhangsan+"'" ;
楼主不喜欢这样就用查询预处理 prepaestatement 看起来舒服点
这个看看数就行,“”里的是SQL语句,+连接两个对象
String sql = "select * from user where name='" + "张三" + "'";我解释一下 "select * from user where name='"是一个字符串 "张三"和"'"也是字符串 +的意思是把这几个字符串拼接 select * from user where name='zhangsan'; 用+一般是为了防止sql一行过长,就用“+”来换行,其实就是一个字符串相加的运算,加起来还是一个String型。细心点不难理解 sql="insert into "+ "yboltp.tb_bzry_zz a (a.lsh,a.zhh,a.zcjgdm,a.yxksr,a.yxdqr,a.zcsj,a.djh,a.zt) "+ "values('"+lsh+"','"+vo.getZhh()+"','"+vo.getZcjgdm()+"',"+yxksr+","+ yxdqr+", "+"to_date('"+zcsj+"','yyyy-mm-dd hh24:mi:ss')"+",'"+vo.getDjh()+"','00')";这是公司的一段代码 插入的数据都是变量传进来的
主要是 拼接问题 为了可以再sql语句中用到变量! 最简单的例子 String name="(随便了)"; String sql="select * from table where name='"+name(这里就是变量了)+"'"; 你问的应该就是这个吧!
int age=30; String name="张三"; String table="user"; String sql="select * from " + table + " where name=' " +name+ " ' and age="+age+" ";细心点就行了,没什么难度。
例如select * from user where name='张三'
在java中会这样出现
String sql = "select * from user where name='" + "张三" + "'"
String sql="select * from table where a='1'"
+" and b='2'"+"and c='3' ";“”里面再套引号,就用单引号,用+一般是为了防止sql一行过长,就用“+”来换行,其实就是一个字符串相加的运算,加起来还是一个String型。细心点不难理解
prepaestatement 看起来舒服点
"张三" +
"'";我解释一下 "select * from user where name='"是一个字符串
"张三"和"'"也是字符串 +的意思是把这几个字符串拼接 select * from user where name='zhangsan';
用+一般是为了防止sql一行过长,就用“+”来换行,其实就是一个字符串相加的运算,加起来还是一个String型。细心点不难理解
sql="insert into "+
"yboltp.tb_bzry_zz a (a.lsh,a.zhh,a.zcjgdm,a.yxksr,a.yxdqr,a.zcsj,a.djh,a.zt) "+
"values('"+lsh+"','"+vo.getZhh()+"','"+vo.getZcjgdm()+"',"+yxksr+","+
yxdqr+", "+"to_date('"+zcsj+"','yyyy-mm-dd hh24:mi:ss')"+",'"+vo.getDjh()+"','00')";这是公司的一段代码 插入的数据都是变量传进来的
最简单的例子
String name="(随便了)";
String sql="select * from table where name='"+name(这里就是变量了)+"'";
你问的应该就是这个吧!
String name="张三";
String table="user";
String sql="select * from " + table + " where name=' " +name+ " ' and age="+age+" ";细心点就行了,没什么难度。