KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > util > jdbc > DynamicQuery


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.util.jdbc;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 /**
24  * @author Steve Viens (steve@viens.net)
25  */

26 public class DynamicQuery
27 {
28   /**
29    * Vector of all SQL values
30    */

31   private Vector JavaDoc values = null;
32   private StringBuffer JavaDoc sql = null;
33
34   /**
35    * default constructor
36    */

37   public DynamicQuery()
38   {
39     this.values = new Vector JavaDoc();
40     this.sql = new StringBuffer JavaDoc();
41   }
42   
43   public DynamicQuery(String JavaDoc sql)
44   {
45     this.values = new Vector JavaDoc();
46     this.sql = new StringBuffer JavaDoc(sql);
47   }
48   
49   public void append(String JavaDoc sql)
50   {
51     this.sql.append(sql);
52   }
53   
54   public void addValue(Object JavaDoc obj)
55   {
56     this.values.addElement(obj);
57   }
58   
59   public PreparedStatement JavaDoc buildPreparedStatement(Connection JavaDoc connection)
60     throws SQLException JavaDoc
61   {
62     PreparedStatement JavaDoc stmt = connection.prepareStatement(sql.toString());
63     for (int i=0; i<values.size(); i++)
64       stmt.setObject(i+1,values.elementAt(i));
65     
66     return stmt;
67   }
68   
69   public String JavaDoc toString()
70   {
71     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(sql.toString());
72     buffer.append("\n\n");
73     
74     for (int i=0; i<values.size(); i++)
75     {
76       Object JavaDoc obj = values.elementAt(i);
77       
78       buffer.append(i+1);
79       buffer.append("\t");
80       buffer.append(obj.getClass().getName());
81       buffer.append("\t");
82       buffer.append(obj.toString());
83       buffer.append("\n");
84     }
85     
86     return buffer.toString();
87   }
88 }
89
Popular Tags