Java SQL问题通过带参数的CallableStatement执行查询时的问题

huangapple go评论83阅读模式
英文:

Java SQL problem executing query through CallableStatement with parameter

问题

我在一个执行接受参数的存储过程查询的CallableStatement中遇到了问题。

我有一个包含查询的字符串列表,例如:

{call query5_immatricolati(?)}

我有一个包含参数的字符串列表,例如

String cds = "L-INF";

当我运行时,没有SQL语法错误,但结果集中没有任何值。

执行的预期结果是我可以通过从结果集接收数据来创建一个对象。

以下是代码:

for (int i = 0; i < INDICATORI.getInstance().getListaIndicatori().size();) {
    for (int j = 0; j < INDICATORI.getInstance().getListaQuery().size();) {

        if (INDICATORI.getInstance().getListaIndicatori().get(i).equals("iC00a")) {
            
            for (String cds : CDS.getInstance().getCds().values()) {

                
                ArrayList<indicatore> lista = new ArrayList<indicatore>();
                String query = INDICATORI.getInstance().getListaQuery().get(j);
                
                try {
                    CallableStatement cb = DB.getInstance().getConnection().prepareCall(query);
                    cb.setString(1, cds);
                    DB.getInstance().setResultSet(cb.executeQuery());

                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                   
                    
                    
                    while (DB.getInstance().getResultSet().next()) {
                        
                        iC00a obj = new iC00a();
                        obj.setId(counter);
                        obj.setAnnoIscrizione(DB.getInstance().getResultSet().getString(1));
                        obj.setIscrittiPrimoAnno(DB.getInstance().getResultSet().getInt(2));
                
                        lista.add(obj);
                        
                        counter++;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                map.getInstance().getMap().put(INDICATORI.getInstance().getListaIndicatori().get(i) + cds, lista);
                counter = 0;
            }

        }
        
        i++;
        j++;
    }
}

我尝试手动设置cb.setString(1, cds)的值,例如cb.setString(1, "L-INF"),它可以正常工作!!!

但是我不能手动设置参数,我需要使用for循环逐个字符串并动态插入作为参数。

为什么如果我手动设置参数,就像一个字符串一样,它可以工作,而如果我使用字符串变量,就不行?

有人能告诉我我做错了什么吗?

英文:

I have a problem in a CallableStatement that execute a stored procedure query which accept a parameter.

I have a list of string that contains the query like:

{call query5_immatricolati(?)}

I have a list of string that contains the parameter like

String cds = &quot;L-INF&quot;;

I have no SQL syntax error when I run but the result set doesn't have any value.

The expected result of the execution is that i could create an object by receiving data from the result set.

Here is the code:

for (int i = 0; i &lt; INDICATORI.getInstance().getListaIndicatori().size();) {
for (int j = 0; j &lt; INDICATORI.getInstance().getListaQuery().size();) {
if (INDICATORI.getInstance().getListaIndicatori().get(i).equals(&quot;iC00a&quot;)) {
for (String cds : CDS.getInstance().getCds().values()) {
ArrayList&lt;indicatore&gt; lista = new ArrayList&lt;indicatore&gt;();
String query = INDICATORI.getInstance().getListaQuery().get(j);
try {
CallableStatement cb = DB.getInstance().getConnection().prepareCall(query);
cb.setString(1, cds);
DB.getInstance().setResultSet(cb.executeQuery());
} catch (SQLException e) {
e.printStackTrace();
}
try {
while (DB.getInstance().getResultSet().next()) {
iC00a obj = new iC00a();
obj.setId(counter);
obj.setAnnoIscrizione(DB.getInstance().getResultSet().getString(1));
obj.setIscrittiPrimoAnno(DB.getInstance().getResultSet().getInt(2));
lista.add(obj);
counter++;
}
} catch (SQLException e) {
e.printStackTrace();
}
map.getInstance().getMap().put(INDICATORI.getInstance().getListaIndicatori().get(i)+cds, lista);
counter=0;
}
}
i++;
j++;
}
}

I tried to manually set in cb.setString(1,cds) the value like cb.setString(1,"L-INF") AND IT WORKS !!!

But I can't set manually the parameter, I need to iterate with for each loop each string and dynamically insert as parameter.

Why if I set the parameter manually like a string it works instead if i give a string variable not ?

Could anyone tell me what I'm doing wrong?

答案1

得分: 0

问题出在你的变量cds上,因为它可能在前后有空格。
尝试:

cb.setString(1, cds.strip());

对我来说有效。

英文:

After a lot of attempts, I've found the solution.

The problem is in your variable cdsbecause it could have white spaces before or after.
Try:

cb.setString(1,cds.strip());

For me it worked.

huangapple
  • 本文由 发表于 2020年8月4日 20:31:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63246953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定