KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > jdbc > Test


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.jdbc;
31
32 import java.io.PrintWriter JavaDoc;
33 import java.sql.Connection JavaDoc;
34 import java.sql.DatabaseMetaData JavaDoc;
35 import java.sql.DriverManager JavaDoc;
36 import java.sql.ResultSet JavaDoc;
37 import java.sql.ResultSetMetaData JavaDoc;
38 import java.sql.Statement JavaDoc;
39
40 import org.hsqldb.util.ShutdownServer;
41
42 import com.genimen.djeneric.util.DjLogger;
43
44 /**
45  * @author Gert Rijs
46  * @version 1.0
47  */

48
49 public class Test
50 {
51   public Test()
52   {
53   }
54
55   public static void main(String JavaDoc[] args)
56   {
57     try
58     {
59       DriverManager.setLogWriter(new PrintWriter JavaDoc(System.out));
60       new DbRunner("C:/sjared/products/eclipse/workspace/Djeneric/db/gdp2");
61       // if(true!=false)
62
// return;
63
}
64     catch (Exception JavaDoc e)
65     {
66
67     }
68     try
69     {
70       //DriverManager.setLogWriter(new PrintWriter(System.out));
71
Test test1 = new Test();
72       for (int i = 0; i < 1; i++)
73         test1.run();
74       System.out.println("==============================DONE=================================");
75     }
76     catch (Exception JavaDoc ex)
77     {
78       System.err.println("exception thrown during test");
79       DjLogger.log(ex);
80     }
81     //DbRunner._runningThread.quit();
82
}
83
84   void run() throws Exception JavaDoc
85   {
86     Class.forName("com.genimen.djeneric.jdbc.DjDriver");
87     String JavaDoc NAME = "name=sa@hypersonic;";
88     String JavaDoc LOC = "location=Polymorph RDBMS;";
89     String JavaDoc DRV = "driver=org.hsqldb.jdbcDriver;";
90     String JavaDoc URL = "url=jdbc:hsqldb:hsql://localhost:9876;";
91     String JavaDoc USR = "user=sa;";
92     String JavaDoc PWD = "password=;";
93     // this should work...
94
Connection JavaDoc c = DriverManager.getConnection("jdbc:djeneric:" + NAME + LOC + DRV + URL + USR + PWD);
95
96     DatabaseMetaData JavaDoc dbm = c.getMetaData();
97
98     ResultSet JavaDoc tables = dbm.getTables(null, null, "", new String JavaDoc[]{"TABLE"});
99     ResultSetMetaData JavaDoc tablesMeta = tables.getMetaData();
100
101     while (tables.next())
102     {
103       System.out.println("new table");
104       for (int tc = 1; tc <= tablesMeta.getColumnCount(); tc++)
105       {
106         System.out.print(tables.getString(tc));
107         System.out.print(" ");
108       }
109       System.out.println("-- table data follows --");
110
111       Statement JavaDoc s = c.createStatement();
112       ResultSet JavaDoc rs = s.executeQuery("select * from " + tables.getString("TABLE_NAME"));
113       System.out.println(rs);
114       rs.close();
115       s.close();
116     }
117     tables.close();
118     c.close();
119     /*
120      * Statement s = c.createStatement(); from helptextbody where true =
121      * false"); ResultSetMetaData rmd = rs.getMetaData();
122      *
123      * System.out.println(rmd);
124      *
125      * int rownum = 0; while(rs.next()==true) { System.out.println("row " +
126      * ++rownum); for(int i=0;i <rmd.getColumnCount();i++) {
127      * System.out.println(rmd.getColumnName(i+1) + ": " + rs.getString(i+1)); } }
128      * rs.close(); s.close(); c.close(); System.out.println("isclosed: " +
129      * c.isClosed());
130      */

131   }
132 }
133
134 class DbRunner extends Thread JavaDoc
135 {
136   String JavaDoc _dbloc;
137   boolean _isRunning = false;
138   static DbRunner _runningThread = null;
139
140   public DbRunner(String JavaDoc dbloc)
141   {
142     _dbloc = dbloc;
143     setDaemon(false);
144     start();
145     try
146     {
147       Thread.sleep(3000);
148     }
149     catch (Exception JavaDoc ee)
150     {
151     }
152   }
153
154   public void run()
155   {
156     _runningThread = this;
157     _isRunning = true;
158     org.hsqldb.Server.main(new String JavaDoc[]{"-no_system_exit", "true", "-port", "9876", "-database", _dbloc});
159     _isRunning = false;
160   }
161
162   public boolean isRunning()
163   {
164     return _isRunning;
165   }
166
167   public void quit()
168   {
169     ShutdownServer.main(new String JavaDoc[]{"-user", "sa", "-password", "", "-port", "9876", "-shutdownarg", "COMPACT"});
170     while (isRunning())
171     {
172       try
173       {
174         Thread.sleep(500);
175       }
176       catch (Exception JavaDoc e)
177       {
178       }
179     }
180   }
181 }
Popular Tags