001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.jaggenerator;
019:
020: import javax.swing.*;
021: import java.sql.Connection;
022: import java.sql.Driver;
023: import java.sql.DriverManager;
024: import java.sql.ResultSet;
025: import java.util.ArrayList;
026:
027: /**
028: *
029: * @author hillie
030: */
031: public class GenericJdbcManager {
032:
033: private String url;
034: private String schema;
035: private String username;
036: private String password;
037: private String clazz;
038: private String dbName = "";
039: private String[] displayTypes = null;
040: private static final String SCHEMA_NAME_COLUMN = "TABLE_SCHEM";
041:
042: public GenericJdbcManager(String url, String username,
043: String password, String clazz, String[] displayTypes) {
044: this .url = url;
045: this .username = username;
046: this .password = password;
047: this .clazz = clazz;
048: this .displayTypes = displayTypes;
049:
050: int dbIndex = url.lastIndexOf("/");
051: if (dbIndex != -1) {
052: dbName = url.substring(dbIndex + 1);
053: }
054:
055: //by default use the schema that maches the username - otherwise prompt for a schema..
056: ArrayList allSchemas = new ArrayList();
057: try {
058: Connection cx = connect();
059: ResultSet schemas = cx.getMetaData().getSchemas();
060: while (schemas.next()) {
061: String s = schemas.getString(SCHEMA_NAME_COLUMN);
062: /* Do NOT ignore case here, otherwise the schema will not be selected correctly */
063: if (username.equals(s)) {
064: schema = username;
065: break;
066: }
067: allSchemas.add(s);
068: }
069:
070: // Only show if there are any schema's to select.
071: if ((schema == null) && (allSchemas.size() != 0)) {
072: schema = (String) JOptionPane
073: .showInputDialog(
074: JagGenerator.jagGenerator,
075: "There is no schema called \""
076: + username
077: + "\" in this database!\n\n"
078: + "Please choose the desired schema from this list, \n"
079: + "or press 'Cancel' to access all schemas.\n",
080: "Database schemas",
081: JOptionPane.QUESTION_MESSAGE, null,
082: allSchemas.toArray(), null);
083: }
084:
085: JagGenerator.logToConsole("Using database schema: "
086: + schema);
087:
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091: }
092:
093: public Connection connect() throws Exception {
094: DriverManager.registerDriver((Driver) Class.forName(clazz)
095: .newInstance());
096: return DriverManager.getConnection(url, username, password);
097: }
098:
099: /** Getter for property url.
100: * @return Value of property url.
101: *
102: */
103: public String getUrl() {
104: return this .url;
105: }
106:
107: /** Setter for property url.
108: * @param url New value of property url.
109: *
110: */
111: public void setUrl(String url) {
112: this .url = url;
113: }
114:
115: /** Getter for property username.
116: * @return Value of property username.
117: *
118: */
119: public String getUsername() {
120: return this .username;
121: }
122:
123: /** Setter for property username.
124: * @param username New value of property username.
125: *
126: */
127: public void setUsername(String username) {
128: this .username = username;
129: }
130:
131: /** Getter for property password.
132: * @return Value of property password.
133: *
134: */
135: public String getPassword() {
136: return this .password;
137: }
138:
139: /** Setter for property password.
140: * @param password New value of property password.
141: *
142: */
143: public void setPassword(String password) {
144: this .password = password;
145: }
146:
147: /** Getter for property Database Name.
148: * @return Value of database name.
149: *
150: *
151: */
152: public String getDBName() {
153: return this .dbName;
154: }
155:
156: /** Setter for property password.
157: * @param dbName New value of property password.
158: *
159: *
160: */
161: public void setDBName(String dbName) {
162: this .dbName = dbName;
163: }
164:
165: /**
166: * Return an array list of types that should be displayed while connecting
167: * to the database. For example: TABLE, VIEW, SYNONYM
168: */
169: public String[] getDisplayTableTypes() {
170: return this .displayTypes;
171: }
172:
173: /**
174: * Gets the currently database schema, or <code>null</code> if no schema is being used.
175: * @return Value of database name.
176: */
177: public String getSchema() {
178: return schema;
179: }
180:
181: }
|