KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jaggenerator > GenericJdbcManager


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jaggenerator;
19
20 import javax.swing.*;
21 import java.sql.Connection JavaDoc;
22 import java.sql.Driver JavaDoc;
23 import java.sql.DriverManager JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 /**
28  *
29  * @author hillie
30  */

31 public class GenericJdbcManager {
32
33    private String JavaDoc url;
34    private String JavaDoc schema;
35    private String JavaDoc username;
36    private String JavaDoc password;
37    private String JavaDoc clazz;
38    private String JavaDoc dbName = "";
39    private String JavaDoc[] displayTypes = null;
40    private static final String JavaDoc SCHEMA_NAME_COLUMN = "TABLE_SCHEM";
41
42
43    public GenericJdbcManager(String JavaDoc url, String JavaDoc username, String JavaDoc password, String JavaDoc clazz, String JavaDoc[] displayTypes) {
44       this.url = url;
45       this.username = username;
46       this.password = password;
47       this.clazz = clazz;
48       this.displayTypes = displayTypes;
49
50       int dbIndex = url.lastIndexOf("/");
51       if (dbIndex != -1) {
52          dbName = url.substring(dbIndex + 1);
53       }
54
55       //by default use the schema that maches the username - otherwise prompt for a schema..
56
ArrayList JavaDoc allSchemas = new ArrayList JavaDoc();
57       try {
58          Connection JavaDoc cx = connect();
59          ResultSet JavaDoc schemas = cx.getMetaData().getSchemas();
60          while (schemas.next()) {
61             String JavaDoc s = schemas.getString(SCHEMA_NAME_COLUMN);
62             /* Do NOT ignore case here, otherwise the schema will not be selected correctly */
63             if (username.equals(s)) {
64                schema = username;
65                break;
66             }
67             allSchemas.add(s);
68          }
69
70           // Only show if there are any schema's to select.
71
if ((schema == null) && (allSchemas.size() != 0)) {
72             schema = (String JavaDoc) JOptionPane.showInputDialog(
73                   JagGenerator.jagGenerator,
74                   "There is no schema called \"" + username + "\" in this database!\n\n" +
75                   "Please choose the desired schema from this list, \n" +
76                   "or press 'Cancel' to access all schemas.\n",
77                   "Database schemas",
78                   JOptionPane.QUESTION_MESSAGE,
79                   null,
80                   allSchemas.toArray(),
81                   null);
82          }
83
84          JagGenerator.logToConsole("Using database schema: " + schema);
85
86       } catch (Exception JavaDoc e) {
87          e.printStackTrace();
88       }
89    }
90
91    public Connection JavaDoc connect() throws Exception JavaDoc {
92       DriverManager.registerDriver((Driver JavaDoc) Class.forName(clazz).newInstance());
93       return DriverManager.getConnection(url, username, password);
94    }
95
96
97    /** Getter for property url.
98     * @return Value of property url.
99     *
100     */

101    public String JavaDoc getUrl() {
102       return this.url;
103    }
104
105
106    /** Setter for property url.
107     * @param url New value of property url.
108     *
109     */

110    public void setUrl(String JavaDoc url) {
111       this.url = url;
112    }
113
114
115    /** Getter for property username.
116     * @return Value of property username.
117     *
118     */

119    public String JavaDoc getUsername() {
120       return this.username;
121    }
122
123
124    /** Setter for property username.
125     * @param username New value of property username.
126     *
127     */

128    public void setUsername(String JavaDoc username) {
129       this.username = username;
130    }
131
132
133    /** Getter for property password.
134     * @return Value of property password.
135     *
136     */

137    public String JavaDoc getPassword() {
138       return this.password;
139    }
140
141
142    /** Setter for property password.
143     * @param password New value of property password.
144     *
145     */

146    public void setPassword(String JavaDoc password) {
147       this.password = password;
148    }
149
150
151    /** Getter for property Database Name.
152     * @return Value of database name.
153     *
154     *
155     */

156    public String JavaDoc getDBName() {
157       return this.dbName;
158    }
159
160
161    /** Setter for property password.
162     * @param dbName New value of property password.
163     *
164     *
165     */

166    public void setDBName(String JavaDoc dbName) {
167       this.dbName = dbName;
168    }
169
170    /**
171     * Return an array list of types that should be displayed while connecting
172     * to the database. For example: TABLE, VIEW, SYNONYM
173     */

174    public String JavaDoc[] getDisplayTableTypes() {
175       return this.displayTypes;
176    }
177
178    /**
179     * Gets the currently database schema, or <code>null</code> if no schema is being used.
180     * @return Value of database name.
181     */

182    public String JavaDoc getSchema() {
183       return schema;
184    }
185
186 }
187
Popular Tags