1 23 24 29 package org.dbforms.devgui; 30 31 import java.awt.*; 32 import java.awt.event.*; 33 34 import java.sql.*; 35 36 import javax.swing.*; 37 38 39 40 46 public class DbPanel extends PropertyPanel implements ActionListener { 47 private JButton b_testConnection; 48 49 private JLabel jLabel1; 51 private JLabel jLabel5; 52 private JLabel jLabel6; 53 private JLabel jLabel7; 54 private JTextField tf_jdbcDriver; 55 private JTextField tf_jdbcURL; 56 private JTextField tf_password; 57 private JTextField tf_username; 58 59 61 66 public DbPanel(DevGui parent) { 67 super(parent.getProjectData()); 68 initComponents(); 69 doLayout(); 70 } 71 72 77 public void setNewProjectData(ProjectData projectData) { 78 this.projectData = projectData; 79 80 tf_jdbcDriver.setText(projectData.getProperty("jdbcDriver")); 81 tf_jdbcURL.setText(projectData.getProperty("jdbcURL")); 82 tf_username.setText(projectData.getProperty("username")); 83 tf_password.setText(projectData.getProperty("password")); 84 } 85 86 87 92 public void actionPerformed(ActionEvent e) { 93 if (e.getSource() == b_testConnection) { 94 testConnection(); 95 } 96 } 97 98 99 104 private void initComponents() { jLabel1 = new javax.swing.JLabel (); 106 tf_jdbcDriver = new javax.swing.JTextField (); 107 jLabel5 = new javax.swing.JLabel (); 108 tf_jdbcURL = new javax.swing.JTextField (); 109 jLabel6 = new javax.swing.JLabel (); 110 tf_username = new javax.swing.JTextField (); 111 jLabel7 = new javax.swing.JLabel (); 112 tf_password = new javax.swing.JTextField (); 113 114 setLayout(new BorderLayout()); 115 116 JPanel panel_top = new JPanel(); 117 panel_top.setLayout(new GridLayout(5, 2)); 118 add(BorderLayout.NORTH, panel_top); 119 120 jLabel1.setText("JDBC Driver Class"); 122 panel_top.add(jLabel1); 123 addAFocusListener(tf_jdbcDriver, "jdbcDriver"); 124 panel_top.add(tf_jdbcDriver); 125 126 jLabel5.setText("JDBC URL"); 128 panel_top.add(jLabel5); 129 addAFocusListener(tf_jdbcURL, "jdbcURL"); 130 panel_top.add(tf_jdbcURL); 131 132 jLabel6.setText("Username"); 134 panel_top.add(jLabel6); 135 addAFocusListener(tf_username, "username"); 136 panel_top.add(tf_username); 137 138 jLabel7.setText("Password"); 140 panel_top.add(jLabel7); 141 addAFocusListener(tf_password, "password"); 142 panel_top.add(tf_password); 143 144 b_testConnection = new JButton("Test connection"); 145 b_testConnection.addActionListener(this); 146 b_testConnection.setToolTipText("Check if the configuration provided in this dialog is correct."); 147 panel_top.add(b_testConnection); 148 } 149 150 151 private void testConnection() { 153 String jdbcDriver = projectData.getProperty("jdbcDriver"); 154 String jdbcURL = projectData.getProperty("jdbcURL"); 155 String username = projectData.getProperty("username"); 156 String password = projectData.getProperty("password"); 157 158 Connection con = null; 159 160 try { 161 con = XMLConfigGenerator.createConnection(jdbcDriver, jdbcURL, 162 username, password); 163 164 if (con == null) { 165 JOptionPane.showMessageDialog(this, "No connection", "db failure", 166 JOptionPane.ERROR_MESSAGE); 167 } else { 168 JOptionPane.showMessageDialog(this, 169 "Database connection successfully installed.", 170 "Success", JOptionPane.PLAIN_MESSAGE); 171 } 172 } catch (Exception e) { 173 if (con == null) { 174 JOptionPane.showMessageDialog(this, "Error:" + e.toString(), 175 "db failure", 176 JOptionPane.ERROR_MESSAGE); 177 } 178 179 e.printStackTrace(); 180 } finally { 181 try { 182 if (con != null) { 183 con.close(); 184 } 185 } catch (SQLException sqle) { 186 ; 187 } 188 } 189 } 190 } 191 | Popular Tags |