1 package org.apache.ojb.tools.mapping.reversedb2.gui; 2 3 4 import java.util.Vector ; 5 import org.apache.ojb.tools.mapping.reversedb2.Main; 6 import javax.swing.MutableComboBoxModel ; 7 21 22 23 24 25 26 38 public class JDlgDBConnection extends javax.swing.JDialog 39 { 40 41 private Vector vJDBCDrivers = new Vector (); 42 private Vector vJDBCUrls = new Vector (); 43 44 private java.sql.Connection theConnection = null; 45 46 private boolean isDisposed = false; 47 51 public JDlgDBConnection(java.awt.Frame parent, boolean isModal) 52 { 53 super(parent, isModal); 54 initComponents(); 55 56 synchronized (Main.getProperties()) 57 { 58 int i = 0; 60 String s = null; 61 do 62 { 63 s = Main.getProperties().getProperty(Main.PROPERTY_JDBCDRIVER + i); 64 if (s != null) vJDBCDrivers.add(s); 65 i++; 66 } while (s != null); 67 68 69 i = 0; 71 s = null; 72 do 73 { 74 s = Main.getProperties().getProperty(Main.PROPERTY_JDBCURL + i); 75 if (s != null) vJDBCUrls.add(s); 76 i++; 77 } while (s != null); 78 79 80 s = Main.getProperties().getProperty("JDBCUser" + i); 82 83 84 85 this.cmbJDBCDriver.setModel(new javax.swing.DefaultComboBoxModel (vJDBCDrivers)); 86 this.cmbJDBCURL.setModel(new javax.swing.DefaultComboBoxModel (vJDBCUrls)); 87 88 89 this.cmbJDBCDriver.setEditable(true); 90 this.cmbJDBCURL.setEditable(true); 91 92 93 this.tfUsername.setText(Main.getProperties().getProperty(Main.PROPERTY_JDBCUSER, "")); 94 this.tfPassword.setText(""); 95 96 97 this.lblResult.setText(""); 98 pack(); 99 } 100 if (this.cmbJDBCDriver.getSelectedItem() != null && 101 this.cmbJDBCURL.getSelectedItem() != null) 102 { 103 this.pbTest.setEnabled(true); 104 this.pbOpen.setEnabled(true); 105 } 106 else 107 { 108 this.pbTest.setEnabled(false); 109 this.pbOpen.setEnabled(false); 110 } 111 } 112 113 117 public java.sql.Connection showAndReturnConnection() 118 { 119 show(); 120 while (!this.isDisposed) 121 { 122 try 123 { 124 synchronized (this) 125 { 126 wait(); 127 } 128 } 129 catch (Throwable t) 130 { 131 } 132 } 133 return theConnection; 134 } 135 136 141 public void dispose() 142 { 143 super.dispose(); 144 this.isDisposed = true; 145 synchronized(this) 146 { 147 this.notifyAll(); 148 } 149 } 150 151 156 private void initComponents() { 158 java.awt.GridBagConstraints gridBagConstraints; 159 160 lblJDBCDriver = new javax.swing.JLabel (); 161 cmbJDBCDriver = new javax.swing.JComboBox (); 162 lblJDBCURL = new javax.swing.JLabel (); 163 cmbJDBCURL = new javax.swing.JComboBox (); 164 lblUsername = new javax.swing.JLabel (); 165 tfUsername = new javax.swing.JTextField (); 166 lblPassword = new javax.swing.JLabel (); 167 tfPassword = new javax.swing.JPasswordField (); 168 lblResult = new javax.swing.JTextField (); 169 jPanel1 = new javax.swing.JPanel (); 170 pbCancel = new javax.swing.JButton (); 171 pbTest = new javax.swing.JButton (); 172 pbOpen = new javax.swing.JButton (); 173 174 getContentPane().setLayout(new java.awt.GridBagLayout ()); 175 176 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 177 addWindowListener(new java.awt.event.WindowAdapter () 178 { 179 public void windowClosing(java.awt.event.WindowEvent evt) 180 { 181 closeDialog(evt); 182 } 183 }); 184 185 lblJDBCDriver.setText("JDBC Driver Class:"); 186 gridBagConstraints = new java.awt.GridBagConstraints (); 187 gridBagConstraints.gridx = 0; 188 gridBagConstraints.gridy = 0; 189 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 190 getContentPane().add(lblJDBCDriver, gridBagConstraints); 191 192 cmbJDBCDriver.setFont(new java.awt.Font ("Dialog", 0, 12)); 193 cmbJDBCDriver.setMinimumSize(new java.awt.Dimension (31, 20)); 194 cmbJDBCDriver.setPreferredSize(new java.awt.Dimension (31, 20)); 195 cmbJDBCDriver.addItemListener(new java.awt.event.ItemListener () 196 { 197 public void itemStateChanged(java.awt.event.ItemEvent evt) 198 { 199 cmbJDBCDriverItemStateChanged(evt); 200 } 201 }); 202 203 gridBagConstraints = new java.awt.GridBagConstraints (); 204 gridBagConstraints.gridx = 1; 205 gridBagConstraints.gridy = 0; 206 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 207 getContentPane().add(cmbJDBCDriver, gridBagConstraints); 208 209 lblJDBCURL.setText("JDBC URL:"); 210 gridBagConstraints = new java.awt.GridBagConstraints (); 211 gridBagConstraints.gridx = 0; 212 gridBagConstraints.gridy = 1; 213 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 214 getContentPane().add(lblJDBCURL, gridBagConstraints); 215 216 cmbJDBCURL.setFont(new java.awt.Font ("Dialog", 0, 12)); 217 cmbJDBCURL.setMinimumSize(new java.awt.Dimension (31, 20)); 218 cmbJDBCURL.setPreferredSize(new java.awt.Dimension (31, 20)); 219 cmbJDBCURL.addItemListener(new java.awt.event.ItemListener () 220 { 221 public void itemStateChanged(java.awt.event.ItemEvent evt) 222 { 223 cmbJDBCURLItemStateChanged(evt); 224 } 225 }); 226 227 gridBagConstraints = new java.awt.GridBagConstraints (); 228 gridBagConstraints.gridx = 1; 229 gridBagConstraints.gridy = 1; 230 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 231 getContentPane().add(cmbJDBCURL, gridBagConstraints); 232 233 lblUsername.setText("Username:"); 234 gridBagConstraints = new java.awt.GridBagConstraints (); 235 gridBagConstraints.gridx = 0; 236 gridBagConstraints.gridy = 2; 237 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 238 getContentPane().add(lblUsername, gridBagConstraints); 239 240 tfUsername.setColumns(30); 241 tfUsername.setText("jTextField3"); 242 gridBagConstraints = new java.awt.GridBagConstraints (); 243 gridBagConstraints.gridx = 1; 244 gridBagConstraints.gridy = 2; 245 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 246 gridBagConstraints.weightx = 1.0; 247 getContentPane().add(tfUsername, gridBagConstraints); 248 249 lblPassword.setText("Password:"); 250 gridBagConstraints = new java.awt.GridBagConstraints (); 251 gridBagConstraints.gridx = 0; 252 gridBagConstraints.gridy = 3; 253 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 254 getContentPane().add(lblPassword, gridBagConstraints); 255 256 tfPassword.setColumns(30); 257 gridBagConstraints = new java.awt.GridBagConstraints (); 258 gridBagConstraints.gridx = 1; 259 gridBagConstraints.gridy = 3; 260 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 261 gridBagConstraints.weightx = 1.0; 262 getContentPane().add(tfPassword, gridBagConstraints); 263 264 lblResult.setBackground((java.awt.Color ) javax.swing.UIManager.getDefaults().get("Label.background")); 265 lblResult.setEditable(false); 266 lblResult.setText("jTextField1"); 267 lblResult.setBorder(null); 268 gridBagConstraints = new java.awt.GridBagConstraints (); 269 gridBagConstraints.gridx = 0; 270 gridBagConstraints.gridy = 4; 271 gridBagConstraints.gridwidth = 2; 272 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 273 getContentPane().add(lblResult, gridBagConstraints); 274 275 pbCancel.setMnemonic('c'); 276 pbCancel.setText("Cancel"); 277 pbCancel.addActionListener(new java.awt.event.ActionListener () 278 { 279 public void actionPerformed(java.awt.event.ActionEvent evt) 280 { 281 pbCancelActionPerformed(evt); 282 } 283 }); 284 285 jPanel1.add(pbCancel); 286 287 pbTest.setMnemonic('t'); 288 pbTest.setText("Test"); 289 pbTest.setEnabled(false); 290 pbTest.addActionListener(new java.awt.event.ActionListener () 291 { 292 public void actionPerformed(java.awt.event.ActionEvent evt) 293 { 294 pbTestActionPerformed(evt); 295 } 296 }); 297 298 jPanel1.add(pbTest); 299 300 pbOpen.setMnemonic('o'); 301 pbOpen.setText("Open"); 302 pbOpen.setEnabled(false); 303 pbOpen.addActionListener(new java.awt.event.ActionListener () 304 { 305 public void actionPerformed(java.awt.event.ActionEvent evt) 306 { 307 pbOpenActionPerformed(evt); 308 } 309 }); 310 311 jPanel1.add(pbOpen); 312 313 gridBagConstraints = new java.awt.GridBagConstraints (); 314 gridBagConstraints.gridx = 1; 315 gridBagConstraints.gridy = 5; 316 gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; 317 getContentPane().add(jPanel1, gridBagConstraints); 318 319 pack(); 320 } 322 private void pbOpenActionPerformed (java.awt.event.ActionEvent evt) { pbTestActionPerformed(evt); 326 if (theConnection != null) 327 { 328 synchronized(Main.getProperties()) 329 { 330 int i = 0; 331 while (Main.getProperties().getProperty(Main.PROPERTY_JDBCDRIVER + i) != null) 332 { 333 Main.getProperties().remove(Main.PROPERTY_JDBCDRIVER + i); 334 i++; 335 } 336 while (Main.getProperties().getProperty(Main.PROPERTY_JDBCURL + i) != null) 337 { 338 Main.getProperties().remove(Main.PROPERTY_JDBCURL + i); 339 i++; 340 } 341 for (i = 0; i < cmbJDBCDriver.getModel().getSize(); i++) 342 Main.getProperties().setProperty(Main.PROPERTY_JDBCDRIVER + i, cmbJDBCDriver.getModel().getElementAt(i).toString()); 343 for (i = 0; i < cmbJDBCURL.getModel().getSize(); i++) 344 Main.getProperties().setProperty(Main.PROPERTY_JDBCURL + i, cmbJDBCURL.getModel().getElementAt(i).toString()); 345 Main.getProperties().setProperty(Main.PROPERTY_JDBCUSER, tfUsername.getText()); 346 Main.getProperties().storeProperties(""); 347 } 348 dispose(); 349 } 350 } 352 353 private void cmbJDBCURLItemStateChanged(java.awt.event.ItemEvent evt) { if (this.cmbJDBCDriver.getSelectedItem() != null && 356 this.cmbJDBCURL.getSelectedItem() != null) 357 { 358 this.pbTest.setEnabled(true); 359 this.pbOpen.setEnabled(true); 360 } 361 else 362 { 363 this.pbTest.setEnabled(false); 364 this.pbOpen.setEnabled(false); 365 } 366 } 368 369 private void cmbJDBCDriverItemStateChanged(java.awt.event.ItemEvent evt) { if (this.cmbJDBCDriver.getSelectedItem() != null && 373 this.cmbJDBCURL.getSelectedItem() != null) 374 { 375 this.pbTest.setEnabled(true); 376 this.pbOpen.setEnabled(true); 377 } 378 else 379 { 380 this.pbTest.setEnabled(false); 381 this.pbOpen.setEnabled(false); 382 } 383 } 385 private void pbCancelActionPerformed (java.awt.event.ActionEvent evt) { theConnection = null; 389 dispose(); 390 } 392 private void pbTestActionPerformed (java.awt.event.ActionEvent evt) { theConnection = null; 396 try 397 { 398 theConnection = this.connectToDB(cmbJDBCDriver.getSelectedItem().toString(), 399 cmbJDBCURL.getSelectedItem().toString(), 400 tfUsername.getText(), 401 new String (tfPassword.getPassword())); 402 this.lblResult.setText("Connection successful"); 403 this.lblResult.setForeground(java.awt.Color.green); 404 if (this.cmbJDBCDriver.getModel() instanceof MutableComboBoxModel ) 405 { 406 Object selectedItem = cmbJDBCDriver.getSelectedItem(); 407 ((MutableComboBoxModel )this.cmbJDBCDriver.getModel()).removeElement(selectedItem); 408 ((MutableComboBoxModel )this.cmbJDBCDriver.getModel()).insertElementAt(selectedItem,0); 409 cmbJDBCDriver.setSelectedItem(selectedItem); 410 } 411 if (this.cmbJDBCURL.getModel() instanceof MutableComboBoxModel ) 412 { 413 Object selectedItem = cmbJDBCURL.getSelectedItem(); 414 ((MutableComboBoxModel )this.cmbJDBCURL.getModel()).removeElement(selectedItem); 415 ((MutableComboBoxModel )this.cmbJDBCURL.getModel()).insertElementAt(selectedItem, 0); 416 cmbJDBCURL.setSelectedItem(selectedItem); 417 } 418 } 419 catch (java.sql.SQLException sqlEx) 420 { 421 StringBuffer strBufMessages = new StringBuffer (); 422 java.sql.SQLException currentSqlEx = sqlEx; 423 do 424 { 425 strBufMessages.append("\n" + sqlEx.getErrorCode() + ":" + sqlEx.getMessage()); 426 currentSqlEx = currentSqlEx.getNextException(); 427 } while (currentSqlEx != null); 428 javax.swing.JOptionPane.showMessageDialog(this, 429 "Error connecting to database:" + strBufMessages.toString(), 430 "Error connecting to database", 431 javax.swing.JOptionPane.ERROR_MESSAGE); 432 this.lblResult.setText("Connection failed: JDBC Error"); 433 this.lblResult.setForeground(java.awt.Color.red); 434 } 435 catch (java.lang.ClassNotFoundException clsNFEx) 436 { 437 javax.swing.JOptionPane.showMessageDialog(this, 438 "Could not load driver because the specified class could not be found", 439 "Error connecting to database", 440 javax.swing.JOptionPane.ERROR_MESSAGE); 441 this.lblResult.setText("Connection failed: Driver class not found"); 442 this.lblResult.setForeground(java.awt.Color.red); 443 } 444 } 446 447 private void closeDialog(java.awt.event.WindowEvent evt) { setVisible(false); 449 dispose(); 450 } 452 private java.sql.Connection connectToDB(String strJDBCDriver, String strJDBCURL, 453 String strUsername, String strPassword) 454 throws java.sql.SQLException , java.lang.ClassNotFoundException 455 { 456 Class.forName(strJDBCDriver); 457 java.sql.Connection conn = 458 java.sql.DriverManager.getConnection(strJDBCURL, 459 strUsername, strPassword); 460 return conn; 461 } 462 463 466 public static void main(String args[]) 467 { 468 System.out.println(new JDlgDBConnection(new javax.swing.JFrame (), true).showAndReturnConnection()); 469 System.out.println(new JDlgDBConnection(new javax.swing.JFrame (), false).showAndReturnConnection()); 470 } 471 472 473 private javax.swing.JLabel lblJDBCURL; 475 private javax.swing.JComboBox cmbJDBCURL; 476 private javax.swing.JComboBox cmbJDBCDriver; 477 private javax.swing.JLabel lblJDBCDriver; 478 private javax.swing.JPanel jPanel1; 479 private javax.swing.JTextField lblResult; 480 private javax.swing.JLabel lblPassword; 481 private javax.swing.JPasswordField tfPassword; 482 private javax.swing.JLabel lblUsername; 483 private javax.swing.JTextField tfUsername; 484 private javax.swing.JButton pbTest; 485 private javax.swing.JButton pbOpen; 486 private javax.swing.JButton pbCancel; 487 489 } 490 491 492 551 | Popular Tags |