Mercurial > hg > ltpda
changeset 39:11e3ed9d2115 database-connection-manager
Implement databases listing in database connection dialog
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 16:20:06 +0100 |
parents | 3aef676a1b20 |
children | 977eb37f31cb |
files | src/ConnectionManager/src/connectionmanager/DatabaseDialog.form src/ConnectionManager/src/connectionmanager/DatabaseDialog.java |
diffstat | 2 files changed, 90 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ <Component id="hostnameCombo" pref="240" max="32767" attributes="0"/> </Group> <Group type="102" alignment="1" attributes="0"> + <Component id="jLabel10" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="21" max="-2" attributes="0"/> + <Component id="databaseCombo" pref="240" max="32767" attributes="0"/> + </Group> + <Group type="102" alignment="1" attributes="0"> + <Component id="listButton" min="-2" max="-2" attributes="0"/> + <EmptySpace pref="134" max="32767" attributes="0"/> <Component id="cancelButton" min="-2" max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/> <Component id="okButton" min="-2" max="-2" attributes="0"/> </Group> - <Group type="102" alignment="1" attributes="0"> - <Component id="jLabel10" min="-2" max="-2" attributes="0"/> - <EmptySpace min="-2" pref="21" max="-2" attributes="0"/> - <Component id="databaseCombo" pref="240" max="32767" attributes="0"/> - </Group> </Group> <EmptySpace max="-2" attributes="0"/> </Group> @@ -61,10 +63,13 @@ <Component id="databaseCombo" alignment="3" min="-2" max="-2" attributes="0"/> <Component id="jLabel10" alignment="3" min="-2" max="-2" attributes="0"/> </Group> - <EmptySpace type="separate" max="-2" attributes="0"/> - <Group type="103" groupAlignment="3" attributes="0"> - <Component id="cancelButton" alignment="3" min="-2" max="-2" attributes="0"/> - <Component id="okButton" alignment="3" min="-2" max="-2" attributes="0"/> + <EmptySpace min="-2" pref="30" max="-2" attributes="0"/> + <Group type="103" groupAlignment="1" attributes="0"> + <Group type="103" alignment="1" groupAlignment="3" attributes="0"> + <Component id="okButton" alignment="3" min="-2" max="-2" attributes="0"/> + <Component id="cancelButton" alignment="3" min="-2" max="-2" attributes="0"/> + </Group> + <Component id="listButton" alignment="1" min="-2" max="-2" attributes="0"/> </Group> <EmptySpace max="-2" attributes="0"/> </Group> @@ -119,5 +124,13 @@ <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="hostnameComboActionPerformed"/> </Events> </Component> + <Component class="javax.swing.JButton" name="listButton"> + <Properties> + <Property name="text" type="java.lang.String" value="List databases"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="listButtonActionPerformed"/> + </Events> + </Component> </SubComponents> </Form>
--- 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<String> usernames = new HashSet<String>(); + 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<String> databases = new ArrayList<String>(); + 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<String> hostnames = new HashSet<String>(); 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 }