Skip to content
Advertisement

Java SQL problem executing query through CallableStatement with parameter

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 = "L-INF";

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 < 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++;
            }
        }

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement