1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
63 if (username.equals(s)) {
64 schema = username;
65 break;
66 }
67 allSchemas.add(s);
68 }
69
70
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 }