简体   繁体   English

如果edittext为空,如何排除或设置某个参数的null

[英]how to exclude or setnull on some parameter if edittext is empty

I have a stored procedure with alot of parameters for filtering i have 2 edittext Cardnumber and Carcode and both of thoose have to be filled before i can get any results 我有一个带有很多用于过滤的参数的存储过程,我有2个edittext卡号和Carcode,在我得到任何结果之前都必须填满这两个位置

so my question is how do i get the result if one of them is empty or if they both are empty 所以我的问题是,如果其中一个为空或两个都为空,我如何得到结果

 protected String doInBackground(String... params) {
                try {
                    Connection con = connectionClass.CONN();
                    if (con == null) {
                        z = "Error in connection with SQL server";
                    } else {
                        doerTicket = setingPreferences.getString("doerTicket", "");
                        CallableStatement cs = null;
                        String query = "exec [file].[usp_getParts] \n@p_ItemNumber = ?,\n@p_DoerTicket = ?,\n@p_CarItemNumber = ?;
                        cs = con.prepareCall(query);
                        cs.setString(1, Cardnumber);//edittext(@p_ItemNumber)
                        cs.setString(2, doerTicket);
                        cs.setString(3, Carcode);//edittext(@p_CarItemNumber)
                        ResultSet rs = cs.executeQuery();
                        while (rs.next()) {

                    }
                } catch (Exception ex) {
                    z = "Exceptions";
                }
                return z;

            }
        }

the parameter i currently use in the sp 我当前在sp中使用的参数

ALTER PROCEDURE [file].[usp_getParts]
    , @p_ItemNumber VARCHAR ( 3000 ) = NULL
    , @p_CarItemNumber NVARCHAR ( 20 ) = NULL
    , @p_DoerTicket VARCHAR ( 200 ) = NULL

with itemnumber 带有项目编号 带有项目编号 with carcode 与汽车代码 与汽车代码

or i can also get the result by using 或者我也可以通过使用获得结果

EXEC    @return_value = [file].[usp_getParts]
        @p_CarItemNumber  = NULL,
        @p_ItemNumber = N'*****',
        @p_DoerTicket = N'0x01000000f475bad9ea7d1f1d6b142a1b8cc95ce74b3f387cb1388a80097f35a8c1e9131ffa7f4b833553bede4a3abec49254373d06fb3294c60c6d24',
        @p_SearchSessionID = @p_SearchSessionID OUTPUT,
        @p_TotalRows = @p_TotalRows OUTPUT

I don't think you can remove any parameter from a statement; 我认为您无法从语句中删除任何参数; I suggest you to use a wildcard for setting statement parameters which are not passed to the function: 我建议您使用通配符来设置不会传递给函数的语句参数:

CallableStatement cs = null;
String query = "exec [file].[usp_getParts] \n@p_ItemNumber = ?,\n@p_DoerTicket = ?,\n@p_CarItemNumber = ?";
cs = con.prepareCall(query);
cs.setString(1, Cardnumber != null ? Cardnumber : "*");//edittext(@p_ItemNumber)
cs.setString(2, doerTicket != null ? doerTicket : "*");
cs.setString(3, Carcode != null ? Carcode : "*");//edittext(@p_CarItemNumber)
ResultSet rs = cs.executeQuery();

This way you can have a single query to extract data with any filter combination. 这样,您可以使用单个查询来使用任何过滤器组合提取数据。

If you don't want to contact the datasource if some filter is not passed, you could simply check them before preparing the statement and return an error string (or maybe better throw an application exception). 如果您不希望在未通过某些过滤器的情况下与数据源联系,则可以在准备语句之前简单地检查它们并返回错误字符串(或者最好抛出应用程序异常)。

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

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