KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ResultSetBuilderDemo


1 /*--
2
3  Copyright 2000 Brett McLaughlin & Jason Hunter. All rights reserved.
4  
5  Redistribution and use in source and binary forms, with or without modifica-
6  tion, are permitted provided that the following conditions are met:
7  
8  1. Redistributions of source code must retain the above copyright notice,
9     this list of conditions, and the following disclaimer.
10  
11  2. Redistributions in binary form must reproduce the above copyright notice,
12     this list of conditions, the disclaimer that follows these conditions,
13     and/or other materials provided with the distribution.
14  
15  3. The names "JDOM" and "Java Document Object Model" must not be used to
16     endorse or promote products derived from this software without prior
17     written permission. For written permission, please contact
18     license@jdom.org.
19  
20  4. Products derived from this software may not be called "JDOM", nor may
21     "JDOM" appear in their name, without prior written permission from the
22     JDOM Project Management (pm@jdom.org).
23  
24  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
25  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
26  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  JDOM PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  
35  This software consists of voluntary contributions made by many individuals
36  on behalf of the Java Document Object Model Project and was originally
37  created by Brett McLaughlin <brett@jdom.org> and
38  Jason Hunter <jhunter@jdom.org>. For more information on the JDOM
39  Project, please see <http://www.jdom.org/>.
40  
41  */

42
43 import java.io.*;
44 import java.sql.*;
45 import java.util.*;
46
47 import org.jdom.*;
48 import org.jdom.output.*;
49
50 import org.jdom.contrib.input.ResultSetBuilder;
51
52 public class ResultSetBuilderDemo {
53     
54   // SQL tables copied from the Servlets.com ISP listing application
55

56   static final String JavaDoc PREP =
57     "create table rsbd ( " +
58     "id int PRIMARY KEY, " +
59     "name varchar(255) NOT NULL, " +
60     "home_url varchar(255) NULL, " +
61     "contact_email varchar(255) NULL, " +
62     "contact_phone varchar(255) NULL, " +
63     "location varchar(255) NULL, " +
64     "comments long varchar NULL, " +
65     "free_hosting boolean NULL, " +
66     "state_flag tinyint NOT NULL, " + // submitted, rejected, live, dead
67
"submitter_email varchar(255) NULL, " + // not displayed
68
"created_time timestamp NOT NULL, " +
69     "modified_time timestamp NOT NULL " +
70     ")";
71
72   static final String JavaDoc FILL =
73     "insert into rsbd (id, name, home_url, contact_email, " +
74     "contact_phone, comments, free_hosting, state_flag, created_time, " +
75     "modified_time) values (2, 'sphere', null, 'info@sphere', " +
76     "'1234', 'cool', true, 10, " +
77     "{ts '1999-02-09 20:11:11.123455'}, " +
78     "{ts '1999-03-21 22:11:11.123455'})";
79
80   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
81     // Tested against Cloudscape database that comes with the J2EE ref impl
82
Class.forName("COM.cloudscape.core.JDBCDriver");
83     Connection con =
84       DriverManager.getConnection("jdbc:cloudscape:rsbd;create=true");
85
86     // Create and fill commands, needed only on the first run
87
Statement prep = con.createStatement();
88     prep.executeUpdate(PREP);
89
90     Statement fill = con.createStatement();
91     fill.executeUpdate(FILL);
92
93     Namespace ns = Namespace.getNamespace("xhtml", "http://w3.org/etc");
94
95     Statement stmt = con.createStatement();
96     ResultSet rs = stmt.executeQuery(
97       "select id, name, home_url || contact_phone from rsbd");
98     ResultSetBuilder builder = new ResultSetBuilder(rs);
99     builder.setAsElement(3, "num3");
100     //builder.setNamespace(ns);
101
//builder.setAsElement("id", "newid");
102
//builder.setAsElement("home_url", "newhome_url");
103
//builder.setAsElement(4, "some4");
104
//builder.setAsAttribute(4, "some4");
105
//builder.setAsAttribute("state_flag");
106
builder.setAsAttribute("created_time", "ctime");
107     Document doc = builder.build();
108     XMLOutputter outputter = new XMLOutputter();
109     outputter.output(doc, System.out);
110   }
111 }
112
Popular Tags