• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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!
0
hgbdelphi
Asked:
hgbdelphi
  • 6
  • 3
  • 2
1 Solution
 
aozarovCommented:
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());
}
0
 
hgbdelphiAuthor Commented:
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?
0
 
hgbdelphiAuthor Commented:
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
aozarovCommented:
Hi hgbdelphi,
I think a better aproach would be similar to this:

public class MyStrngClass
{
       private Map map = new HashMap();
      private String query;

      public MyStringClass(String query)
     {
          this.query = query;
     }

    public void setString(int index, String value)
    {
           map.put(new Integer(index), value);
   }

   public String getTranslatedQuery()
  {
        StringTokenizer stTokens = new StringTokenizer(query, "?", 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);
           }
      }

      return result.toString();
  }
}
 
Then you can call it like this:
MyStrngClass myStringClass = new MyStringClass("select * from tab1 where a=? and b=? and c=?");
myStringClass .setString(1, "aaa");
myStringClass .setString(3, "ccc");
myStringClass .setString(2, "bbb");

// now you can get the translated string
System.out.println(myStringClass.getTranslatedString());
0
 
aozarovCommented:
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.
0
 
hgbdelphiAuthor Commented:
hi,aozarov
  thanks for your help!
 it can work fine. best Regard!
0
 
aozarovCommented:
NP :-)
0
 
CEHJCommented:
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));
                  
0
 
aozarovCommented:
>> There's already a class that does this:
Yes, but that requires a different syntax (instead of unordered "?" to have ordered "{..}")
0
 
CEHJCommented:
>>Yes, but that requires a different syntax (instead of unordered "?" to have ordered "{..}")

I don't understand your point ...
0
 
aozarovCommented:
>> 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.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now