既然是拼接sql语句长度不一定,用StringBuffer修饰,而不用String
String name=request.getParameter("name")
String address=request.getParemeter("address")
StringBuffer sql = new StringBuffer()
sql.append("select * from Tuser as t where 1=1")
if(name!=null||!"".equals(name)){
sql.append(" and t.name like'%"+name+"%'")
}
if(address!=null||!"".equals(address)){
sql.append(" and t.address like'%"+address+"%'")
}
以上就是拼接sql语句,也就是说select * from Tuser as t where 1=1一定会执行,而后面两个会根据用户数据参数选择执行
我可是手写的啊,没有复制,明白了吗?不明了再问我,明白了就给分吧,嘿嘿
1、Oracle创建表,含两列,name和job;create table test_job(name varchar2(30), job varchar2(30))
2、插入样例数据;
insert into test_job(name, job) values('ste','teacher')
insert into test_job(name, job) values('jack','driver')
insert into test_job(name, job) values('mick','cooker')
insert into test_job(name, job) values('jacy','driver')
insert into test_job(name, job) values('lucy','boxer')
insert into test_job(name, job) values('john','iter')
3、执行查询语句,数据已插入;select * from TEST_JOB t
4、编辑语句,拼接所需sql;select name||'''s job is '|| job from TEST_JOB t;
两表的数据连接sql语句,假如有a表和b表:select * from a,b 这样就可以查询两个表的所有数据!也可以添加条件 select * from a,b where (条件)!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)