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
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 /***
41 * DOCUMENT ME!
42 *
43 * @author Joachim Peer
44 * @version
45 */
46 public class DbPanel extends AbstractPropertyPanel implements ActionListener {
47 private JButton b_testConnection;
48
49
50 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
60
61 /***
62 * Creates new form DbPanel
63 *
64 * @param parent DOCUMENT ME!
65 */
66 public DbPanel(DevGui parent) {
67 super(parent.getProjectData());
68 initComponents();
69 doLayout();
70 }
71
72 /***
73 * DOCUMENT ME!
74 *
75 * @param projectData DOCUMENT ME!
76 */
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 /***
88 * DOCUMENT ME!
89 *
90 * @param e DOCUMENT ME!
91 */
92 public void actionPerformed(ActionEvent e) {
93 if (e.getSource() == b_testConnection) {
94 testConnection();
95 }
96 }
97
98
99 /***
100 * This method is called from within the constructor to initialize the form.
101 * WARNING: Do NOT modify this code. The content of this method is always
102 * regenerated by the FormEditor.
103 */
104 private void initComponents() {
105 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
121 jLabel1.setText("JDBC Driver Class");
122 panel_top.add(jLabel1);
123 addAFocusListener(tf_jdbcDriver, "jdbcDriver");
124 panel_top.add(tf_jdbcDriver);
125
126
127 jLabel5.setText("JDBC URL");
128 panel_top.add(jLabel5);
129 addAFocusListener(tf_jdbcURL, "jdbcURL");
130 panel_top.add(tf_jdbcURL);
131
132
133 jLabel6.setText("Username");
134 panel_top.add(jLabel6);
135 addAFocusListener(tf_username, "username");
136 panel_top.add(tf_username);
137
138
139 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
152 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 }