1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
package net.mtu.eggplant.util.sql; |
29 | |
|
30 | |
import java.awt.GridBagConstraints; |
31 | |
import java.awt.GridBagLayout; |
32 | |
import java.sql.Connection; |
33 | |
import java.sql.DriverManager; |
34 | |
import java.sql.SQLException; |
35 | |
import java.util.HashMap; |
36 | |
import java.util.Map; |
37 | |
|
38 | |
import javax.swing.JComboBox; |
39 | |
import javax.swing.JDialog; |
40 | |
import javax.swing.JLabel; |
41 | |
import javax.swing.JPasswordField; |
42 | |
import javax.swing.JTextField; |
43 | |
|
44 | |
import org.slf4j.Logger; |
45 | |
import org.slf4j.LoggerFactory; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public class SQLConnectionDialog extends JDialog { |
55 | |
|
56 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(SQLConnectionDialog.class); |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public SQLConnectionDialog(final java.awt.Frame parent, |
65 | |
final Map<String, String> drivers) { |
66 | 0 | super(parent, "Open JDBC Connection", true); |
67 | 0 | _drivers = new HashMap<String, String>(drivers); |
68 | |
|
69 | 0 | getContentPane().setLayout(new GridBagLayout()); |
70 | |
GridBagConstraints gbc; |
71 | 0 | setSize(300, 150); |
72 | |
|
73 | 0 | JLabel driverLabel = new JLabel("Driver: "); |
74 | 0 | gbc = new GridBagConstraints(); |
75 | 0 | gbc.fill = GridBagConstraints.HORIZONTAL; |
76 | 0 | gbc.weightx = 1.0; |
77 | 0 | gbc.weighty = 0.0; |
78 | 0 | getContentPane().add(driverLabel, gbc); |
79 | |
|
80 | 0 | _driverCombo = new JComboBox<>(); |
81 | |
|
82 | |
|
83 | 0 | for (final String name : _drivers.keySet()) { |
84 | 0 | _driverCombo.addItem(name); |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | 0 | _driverCombo.setSelectedIndex(0); |
92 | |
|
93 | |
|
94 | |
|
95 | 0 | gbc = new GridBagConstraints(); |
96 | 0 | gbc.fill = GridBagConstraints.NONE; |
97 | 0 | gbc.weightx = 0.0; |
98 | 0 | gbc.weighty = 0.0; |
99 | 0 | gbc.gridwidth = GridBagConstraints.REMAINDER; |
100 | 0 | getContentPane().add(_driverCombo, gbc); |
101 | |
|
102 | 0 | JLabel dataSourceLabel = new JLabel("Data Source:"); |
103 | 0 | gbc = new GridBagConstraints(); |
104 | 0 | gbc.fill = GridBagConstraints.HORIZONTAL; |
105 | 0 | gbc.weightx = 1.0; |
106 | 0 | gbc.weighty = 0.0; |
107 | 0 | getContentPane().add(dataSourceLabel, gbc); |
108 | |
|
109 | 0 | _dataSourceText = new JTextField(20); |
110 | 0 | gbc = new GridBagConstraints(); |
111 | 0 | gbc.fill = GridBagConstraints.NONE; |
112 | 0 | gbc.weightx = 0.0; |
113 | 0 | gbc.weighty = 0.0; |
114 | 0 | gbc.gridwidth = GridBagConstraints.REMAINDER; |
115 | 0 | getContentPane().add(_dataSourceText, gbc); |
116 | |
|
117 | 0 | JLabel userLabel = new JLabel("Username:"); |
118 | 0 | gbc = new GridBagConstraints(); |
119 | 0 | gbc.fill = GridBagConstraints.HORIZONTAL; |
120 | 0 | gbc.weightx = 1.0; |
121 | 0 | gbc.weighty = 0.0; |
122 | 0 | getContentPane().add(userLabel, gbc); |
123 | |
|
124 | 0 | _userText = new JTextField(20); |
125 | 0 | gbc = new GridBagConstraints(); |
126 | 0 | gbc.fill = GridBagConstraints.NONE; |
127 | 0 | gbc.weightx = 0.0; |
128 | 0 | gbc.weighty = 0.0; |
129 | 0 | gbc.gridwidth = GridBagConstraints.REMAINDER; |
130 | 0 | getContentPane().add(_userText, gbc); |
131 | |
|
132 | 0 | JLabel passLabel = new JLabel("Password:"); |
133 | 0 | gbc = new GridBagConstraints(); |
134 | 0 | gbc.fill = GridBagConstraints.HORIZONTAL; |
135 | 0 | gbc.weightx = 1.0; |
136 | 0 | gbc.weighty = 0.0; |
137 | 0 | getContentPane().add(passLabel, gbc); |
138 | |
|
139 | 0 | _passText = new JPasswordField(20); |
140 | 0 | gbc = new GridBagConstraints(); |
141 | 0 | gbc.fill = GridBagConstraints.NONE; |
142 | 0 | gbc.weightx = 0.0; |
143 | 0 | gbc.weighty = 0.0; |
144 | 0 | gbc.gridwidth = GridBagConstraints.REMAINDER; |
145 | 0 | getContentPane().add(_passText, gbc); |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | 0 | } |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public void depopulate() { |
167 | |
|
168 | |
|
169 | 0 | String dataSource = _dataSourceText.getText(); |
170 | 0 | String url = "jdbc:odbc:" |
171 | |
+ dataSource; |
172 | 0 | String pass = new String(_passText.getPassword()); |
173 | 0 | String user = _userText.getText(); |
174 | 0 | String connectString = null; |
175 | 0 | final String driverName = (String) _driverCombo.getSelectedItem(); |
176 | |
|
177 | |
try { |
178 | 0 | Class.forName(_drivers.get(driverName)).newInstance(); |
179 | 0 | } catch (final ClassNotFoundException e) { |
180 | 0 | LOGGER.error("Couldn't find driver: " |
181 | 0 | + _drivers.get(driverName)); |
182 | 0 | _connection = null; |
183 | 0 | return; |
184 | 0 | } catch (final InstantiationException e) { |
185 | 0 | LOGGER.error("Couldn't find driver: " |
186 | 0 | + _drivers.get(driverName)); |
187 | 0 | _connection = null; |
188 | 0 | return; |
189 | 0 | } catch (final IllegalAccessException e) { |
190 | 0 | LOGGER.error("Couldn't find driver: " |
191 | 0 | + _drivers.get(driverName)); |
192 | 0 | _connection = null; |
193 | 0 | return; |
194 | 0 | } |
195 | 0 | connectString = url; |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
try { |
217 | 0 | _connection = DriverManager.getConnection(connectString, user, pass); |
218 | 0 | } catch (final SQLException sqle) { |
219 | 0 | LOGGER.debug("caught sql error opening connection: " |
220 | |
+ sqle); |
221 | 0 | _connection = null; |
222 | 0 | } |
223 | 0 | } |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
public Connection getConnection() { |
231 | 0 | return _connection; |
232 | |
} |
233 | |
|
234 | |
private final Map<String, String> _drivers; |
235 | |
|
236 | 0 | private Connection _connection = null; |
237 | |
|
238 | |
private JTextField _userText; |
239 | |
|
240 | |
private JComboBox<String> _driverCombo; |
241 | |
|
242 | |
private JPasswordField _passText; |
243 | |
|
244 | |
private JTextField _dataSourceText; |
245 | |
} |