Link to home
Start Free TrialLog in
Avatar of hgbdelphi
hgbdelphi

asked on

how can i do like PreparedStatement.setString(1,'a'),PreparedStatement.setString(2,'b')?

hi,experts,i want do this like PreparedStatement.setString(1,'a'),PreparedStatement.setString(2,'b')

 String s="select * from tab1 where a=? and b=? and c=?";
 MyStringClass.setString(1,'aaa');
 MyStringClass.setString(3,'ccc');
 MyStringClass.setString(2,'bbb');
 
and after setString();
 s="select * from tab1 where a='aaa' and b='bbb' and c='ccc';

now i use String.replaceFirst("\\?",s);
but if i not sequence replace, it can not get right resutl,how can i do it?

thanks!
Avatar of aozarov
aozarov

How do you store your strings inside MyStringClass? (I will asume they are stored in a Map named map).

StringTokenizer stTokens = new StringTokenizer(s, "?", true);
int index = 1;
StringBuffer result = new StringBuffer();
while (stTokens.hasMoreTokens())
{
     String token = stTokens.nextToken();
     if ("?".equals(token))
    {
        result.append(map.get(new Integer(index)));
        index++;
    }
    else
   {
       result.append(token);
   }
}

System.out.println(result.toString());
}
Avatar of hgbdelphi

ASKER

hi,aozarov
 thanks for your help,this is my want


String s="select * from tab1 where a=? and b=? and c=?";
 MyStringClass.setString(s,1,'aaa');
 MyStringClass.setString(s,3,'ccc');
 MyStringClass.setString(s,2,'bbb');
 
and after setString();
 s="select * from tab1 where a='aaa' and b='bbb' and c='ccc';

now i use String.replaceFirst("\\?",s);
but if i not sequence replace, it can not get right resutl,how can i do it?
now i don't know how do i write MyStringClass.setString()method,now i use
String.replaceFirst("\\?",s);

but if i not sequence replace, it can not get right resutl,how can i do it?
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
replace: System.out.println(myStringClass.getTranslatedString());
with: System.out.println(myStringClass.getTranslatedQuery());

The suggested code is instead of passing s to each method and having setString as a static method on MyStringClass.
hi,aozarov
  thanks for your help!
 it can work fine. best Regard!
NP :-)
Avatar of CEHJ
There's already a class that does this:

                  String s="select * from tab1 where a={0} and b={1} and c={2}";
                  
                   java.text.MessageFormat mf = new java.text.MessageFormat(s);
                   Object[] args = new Object[3];
                   args[0] = "'aaa'";
                   args[2] = "'ccc'";
                   args[1] = "'bbb'";
                   System.out.println(mf.format(args));
                  
>> There's already a class that does this:
Yes, but that requires a different syntax (instead of unordered "?" to have ordered "{..}")
>>Yes, but that requires a different syntax (instead of unordered "?" to have ordered "{..}")

I don't understand your point ...
>> I don't understand your point ...
My point is that "select * from tab1 where a={0} and b={1} and c={2}"
doesn't use the syntax used by PreparedStatement
"select * from tab1 where a=? and b=? and c=?"
which might be part of the requirements.