简体   繁体   English

使用准备好的语句获取数据

[英]Fetch data using prepared statement

This is my query 这是我的查询

select id,name,address from emp_details where gender = ?

Input : Female , Male 输入:女,男

In this usually I need to fetch only Female data or only Male data But some times I need to fetch both Female and Male data using the same query.. 在这种情况下,通常只需要提取女性数据或仅获取男性数据,但是有时我需要使用相同的查询来获取女性和男性数据。

Is it possible? 可能吗? If so, how? 如果是这样,怎么办?

just invert the query: 只需反转查询:

select id,name,address from emp_details where gender <> ?

and specify "male" for getting females, "female" for getting males, and "whatever" for getting both. 并指定“ male”代表女性,“ female”代表男性,“ whatever”代表两者。

Using the same query, no, it's not possible. 使用相同的查询,不可以。 Use a query without any where clause to do that: 使用不带where子句的查询来执行此操作:

select id,name,address from emp_details

Use the in-clause 使用条款

where gender in ?

and as parameter a array of string 和作为参数的字符串数组

new String[]{"m","f"}
SELECT id , name , address
FROM emp_details 
WHERE gender IN (?,?);

Male of Female only => set for both ? 男只女=>两者都设置? the same value 相同的值

PreparedStatement IN clause alternatives? PreparedStatement IN子句替代项?

    String gender="Female ";

    String sql = "select id,name,address from emp_details";

    if(gender!=null && !"".equals(gender))

     {      
        sql+=" where gender = ?";

     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM