Java中的SQL语句解析

String sql = "SELECT userId,password FROM Table_1 where userId ='" + userId +"' and password = '" + pass + "'";

解析如下:

第一步:userId的值为cyy和pass的值为,.

SELECT userId,password FROM Table_1 where userId =cyy and password =,.

第二步:userId,pass为变量,用单引号把变量括起来

SELECT userId,password FROM Table_1 where userId =‘userId' and password ='pass'

第三步:写到Java代码里,就要把字符串用双引号括起来

"SELECT userId,password FROM Table_1 where userId =‘"userId"' and password ='"pass"'"

第四步:字符串要用加号连接起来

"SELECT userId,password FROM Table_1 where userId =‘"+userId+"' and password ='"+pass+"'"

"select count() from userinfo where uName= "+userName
在程序里这个只是一个查询字符串而已,记住,只是一个单纯的字符串,要和查询分析器里的有差别的,而+号只是用来连接这些字符串的.
举个例子给你:
如果userName的值为 "aaa ",则这个字符串映射成的查询语句实际就是:
select count(
) from userinfo where uName=aaa
然后你把它写入查询分析器,结果会发现在uName=aaa附近有语法错误,为什么呢?因为你的uName在数据库中定义的是一个varchar型的,而对字符型进行条件查询的时候是要加 ' '号的:
select count() from userinfo where uName= 'aaa '
因此在后台写查询字符串的时候就必须这样写:
string sql = "select count(
) from userinfo where uName= ' "+userName+ " ' "
这样映射成的查询语句就是:
select count(*) from userinfo where uName= 'aaa ' 了.
然后查询菜不会出错

Insert Into Employee ( Emp_Name, Sex, Title, Age ) Values('" + Trim(Emp_Name) + "','"+ Trim(Sex) + "','" + Trim(Title) + "'," + Trim(Str(Age)) + ")
这例句在sql中应该是Insert Into Employee ( Emp_Name, Sex, Title, Age ) Values('文本数据','文本数据','文本数据',数字类数据)
文本数据要单引,数字无需,日期型也算文本类了,Trim(Str(Age)) 这里就是数字类了

你复制过来的这句话前后应该都还有个双引号"Insert Into Employee ( Emp_Name, Sex, Title, Age ) Values('" + Trim(Emp_Name) + "','"+ Trim(Sex) + "','" + Trim(Title) + "'," + Trim(Str(Age)) + ")
"

加号就是把前面的双引号中的内容连上Trim(Emp_Name)他的内容再连上后面双引号的内容

原文地址:https://www.cnblogs.com/cyy-13/p/5753874.html