View Javadoc

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;
19  import java.sql.PreparedStatement;
20  import java.sql.SQLException;
21  import java.util.Vector;
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 values = null;
32    private StringBuffer sql = null;
33  
34    /***
35     * default constructor
36     */
37    public DynamicQuery()
38    {
39      this.values = new Vector();
40      this.sql = new StringBuffer();
41    } 
42    
43    public DynamicQuery(String sql)
44    {
45      this.values = new Vector();
46      this.sql = new StringBuffer(sql);
47    }
48    
49    public void append(String sql)
50    {
51      this.sql.append(sql);
52    }
53    
54    public void addValue(Object obj)
55    {
56      this.values.addElement(obj);
57    }
58    
59    public PreparedStatement buildPreparedStatement(Connection connection)
60      throws SQLException
61    {
62      PreparedStatement 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 toString()
70    { 
71      StringBuffer buffer = new StringBuffer(sql.toString());
72      buffer.append("\n\n");
73      
74      for (int i=0; i<values.size(); i++)
75      {
76        Object 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  }