我在查询分析器中执行下面代码好使,可是在程序中运行就不好使,不知道什么问题。
--定义变量
declare @a varchar(20),
@b varchar(20),
@c varchar(20),
@d varchar(20)
--给变量初始化X
set @a= 'lvkai '
set @c= 'b '
select *
from person
Where Isnull(@a,name)=name //name为表中字段名
And Isnull(@b,address)=address //address 为表中字段名
And Isnull(@c,sex)=sex //sex为表中字段名
And Isnull(@d,zhiwei)=zhiwei //zhiwei 为表中字段名
程序中是这么写的,如果四个变量都有值,那么就能执行循环中的输出,如果不全有值,那么什么也不执行,不知道为什么,可是在查询分析器中,不全有值也好使啊。如果v1,v3如查询分析器中的值一样,就查不出结果来。必需全都有值才可以。
请高手帮忙指点一下。
//页面接收过来的复选框
String v1 = request.getParameter( "check1 ");
String v2 = request.getParameter( "check2 ");
String v3 = request.getParameter( "check3 ");
String v4 = request.getParameter( "check4 ");
//通过复选框进行动态查询
System.out.println(v1+ ", "+v2+ ", "+v3+ ", "+v4);
try {
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=stu ";
Connection conn = DriverManager.getConnection(url, "sa ", "1234 ");
Statement st=conn.createStatement();
System.out.println( "2 ");
String sql = "select * from person where isnull( ' "+v1+ " ',name)=name and isnull( ' "+v2+ " ',address)=address and isnull( ' "+v3+ " ',sex)=sex and isnull( ' "+v4+ " ',zhiwei)=zhiwei ";
ResultSet rs= st.executeQuery(sql);
System.out.println( "4 ");
List list = new ArrayList();
int i=1;
while(rs.next()){
System.out.println(rs.getString(1)+ ", "+rs.getString(2)+ ", "+rs.getString(3)+ ", "+rs.getString(4));
}
------解决方案--------------------