View Javadoc

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;
22  import java.sql.Driver;
23  import java.sql.DriverManager;
24  import java.sql.ResultSet;
25  import java.util.ArrayList;
26  
27  /***
28   *
29   * @author  hillie
30   */
31  public class GenericJdbcManager {
32  
33     private String url;
34     private String schema;
35     private String username;
36     private String password;
37     private String clazz;
38     private String dbName = "";
39     private String[] displayTypes = null;
40     private static final String SCHEMA_NAME_COLUMN = "TABLE_SCHEM";
41  
42  
43     public GenericJdbcManager(String url, String username, String password, String clazz, String[] 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 allSchemas = new ArrayList();
57        try {
58           Connection cx = connect();
59           ResultSet schemas = cx.getMetaData().getSchemas();
60           while (schemas.next()) {
61              String 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) 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 e) {
87           e.printStackTrace();
88        }
89     }
90  
91     public Connection connect() throws Exception {
92        DriverManager.registerDriver((Driver) 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 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 url) {
111       this.url = url;
112    }
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 
124    /*** Setter for property username.
125     * @param username New value of property username.
126     *
127     */
128    public void setUsername(String username) {
129       this.username = username;
130    }
131 
132 
133    /*** Getter for property password.
134     * @return Value of property password.
135     *
136     */
137    public String 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 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 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 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[] 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 getSchema() {
183       return schema;
184    }
185 
186 }