# HG changeset patch # User Daniele Nicolodi # Date 1323098406 -3600 # Node ID 11e3ed9d2115d6f3cecb5a752cafa3749834bf3c # Parent 3aef676a1b202178933a9c7c0b0da8bf177ae4eb Implement databases listing in database connection dialog diff -r 3aef676a1b20 -r 11e3ed9d2115 src/ConnectionManager/src/connectionmanager/DatabaseDialog.form --- a/src/ConnectionManager/src/connectionmanager/DatabaseDialog.form Mon Dec 05 16:20:06 2011 +0100 +++ b/src/ConnectionManager/src/connectionmanager/DatabaseDialog.form Mon Dec 05 16:20:06 2011 +0100 @@ -34,15 +34,17 @@ + + + + + + + - - - - - @@ -61,10 +63,13 @@ - - - - + + + + + + + @@ -119,5 +124,13 @@ + + + + + + + + diff -r 3aef676a1b20 -r 11e3ed9d2115 src/ConnectionManager/src/connectionmanager/DatabaseDialog.java --- a/src/ConnectionManager/src/connectionmanager/DatabaseDialog.java Mon Dec 05 16:20:06 2011 +0100 +++ b/src/ConnectionManager/src/connectionmanager/DatabaseDialog.java Mon Dec 05 16:20:06 2011 +0100 @@ -1,6 +1,7 @@ package connectionmanager; import java.util.HashSet; +import java.util.ArrayList; import javax.swing.DefaultComboBoxModel; import javax.swing.table.TableModel; @@ -34,6 +35,7 @@ okButton = new javax.swing.JButton(); cancelButton = new javax.swing.JButton(); hostnameCombo = new javax.swing.JComboBox(); + listButton = new javax.swing.JButton(); setTitle("Database connection"); setLocationByPlatform(true); @@ -69,6 +71,13 @@ } }); + listButton.setText("List databases"); + listButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + listButtonActionPerformed(evt); + } + }); + org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( @@ -81,13 +90,15 @@ .add(18, 18, 18) .add(hostnameCombo, 0, 240, Short.MAX_VALUE)) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() + .add(jLabel10) + .add(21, 21, 21) + .add(databaseCombo, 0, 240, Short.MAX_VALUE)) + .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() + .add(listButton) + .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 134, Short.MAX_VALUE) .add(cancelButton) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) - .add(okButton)) - .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() - .add(jLabel10) - .add(21, 21, 21) - .add(databaseCombo, 0, 240, Short.MAX_VALUE))) + .add(okButton))) .addContainerGap()) ); layout.setVerticalGroup( @@ -101,10 +112,12 @@ .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(databaseCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(jLabel10)) - .add(18, 18, 18) - .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) - .add(cancelButton) - .add(okButton)) + .add(30, 30, 30) + .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) + .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) + .add(okButton) + .add(cancelButton)) + .add(listButton)) .addContainerGap()) ); @@ -127,6 +140,51 @@ initDatabaseCombo(); }//GEN-LAST:event_hostnameComboActionPerformed +private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listButtonActionPerformed + // selected hostname + hostname = (String) hostnameCombo.getSelectedItem(); + // users for given hostname + HashSet usernames = new HashSet(); + int rows = model.getRowCount(); + for (int i = 0; i < rows; i++) { + if (model.getValueAt(i, 0).equals(hostname)) { + String username = (String) model.getValueAt(i, 2); + if (username != null) { + usernames.add(username); + } + } + } + // credentials dialog + String[] users = new String[usernames.size()]; + usernames.toArray(users); + java.util.Arrays.sort(users); + CredentialsDialog dialog = new CredentialsDialog(this, hostname, users); + dialog.setVisible(true); + + try { + // connect to database + Class.forName("com.mysql.jdbc.Driver"); + String url = String.format("jdbc:mysql://%s/", hostname); + java.sql.Connection conn = java.sql.DriverManager.getConnection(url, dialog.username, dialog.password); + // list databases + java.sql.Statement stmt = conn.createStatement(); + java.sql.ResultSet rs = stmt.executeQuery("SHOW DATABASES"); + ArrayList databases = new ArrayList(); + while (rs.next()) { + String db = rs.getString(1); + if (! db.equals("information_schema")) { + databases.add(db); + } + } + // add to combo box + databaseCombo.setModel(new DefaultComboBoxModel(databases.toArray())); + // show list + databaseCombo.setPopupVisible(true); + // give focus to list + databaseCombo.getEditor().getEditorComponent().requestFocus(); + } catch (Exception ex) { } +}//GEN-LAST:event_listButtonActionPerformed + private void initHostnameCombo() { HashSet hostnames = new HashSet(); int rows = model.getRowCount(); @@ -159,6 +217,7 @@ private javax.swing.JComboBox hostnameCombo; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel9; + private javax.swing.JButton listButton; private javax.swing.JButton okButton; // End of variables declaration//GEN-END:variables }