1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose.library;
17
18 import java.sql.Connection;
19 import java.sql.DriverManager;
20 import java.sql.SQLException;
21 import java.sql.Statement;
22
23 import org.jdiagnose.DiagnosticUnit;
24
25 /***
26 * Tests a JDBC connection using driver, url, username, password and test
27 * sql settings. See the JDiagnose-Library project for other Database
28 * Diagnostics.
29 *
30 * @author jamie
31 */
32 public class BasicJdbcDiagnostic extends DiagnosticUnit {
33
34 private String driver;
35 private String url;
36 private String username;
37 private String password;
38 private String sql;
39
40 /***
41 * Full constructor.
42 *
43 * @param name the name of this diagnostic
44 * @param driver jdbc driver to use
45 * @param url jdbc url
46 * @param username jdbc username
47 * @param password jdbc password
48 * @param sql sql to use to test the connection
49 */
50 public BasicJdbcDiagnostic(String name, String driver, String url,
51 String username, String password, String sql) {
52 super(name);
53 this.driver = driver;
54 this.url = url;
55 this.username = username;
56 this.password = password;
57 this.sql = sql;
58 }
59
60 /***
61 * Full constructor.
62 *
63 * @param name the name of this diagnostic
64 * @param driver
65 * @param url
66 * @param username
67 * @param password
68 * @param sql
69 */
70 public BasicJdbcDiagnostic(String driver, String url,
71 String username, String password, String sql) {
72 super();
73 this.driver = driver;
74 this.url = url;
75 this.username = username;
76 this.password = password;
77 this.sql = sql;
78 }
79
80 /***
81 * Default Constructor. The driver, url, username, password and sql properties
82 * must be set.
83 */
84 public BasicJdbcDiagnostic() {
85
86 }
87
88 public BasicJdbcDiagnostic(String name) {
89 super(name);
90 }
91
92 /***
93 * Try to connect to the JDBC url using the driver, username and
94 * password settings.
95 *
96 * @throws SQLException
97 * @throws ClassNotFoundException
98 */
99 public void diagnoseSql() throws SQLException, ClassNotFoundException {
100 Class.forName(driver);
101 Connection connection = DriverManager.getConnection(url, username, password);
102 Statement statement = connection.createStatement();
103 if(sql != null) {
104 statement.execute(sql);
105 }
106 }
107
108 /***
109 * @return Returns the driver.
110 */
111 public String getDriver() {
112 return driver;
113 }
114
115 /***
116 * @param driver The driver to set.
117 */
118 public void setDriver(String driver) {
119 this.driver = driver;
120 }
121
122 /***
123 * @return Returns the password.
124 */
125 public String getPassword() {
126 return password;
127 }
128
129 /***
130 * @param password The password to set.
131 */
132 public void setPassword(String password) {
133 this.password = password;
134 }
135
136 /***
137 * @return Returns the sql.
138 */
139 public String getSql() {
140 return sql;
141 }
142
143 /***
144 * @param sql The sql to set.
145 */
146 public void setSql(String sql) {
147 this.sql = sql;
148 }
149
150 /***
151 * @return Returns the url.
152 */
153 public String getUrl() {
154 return url;
155 }
156
157 /***
158 * @param url The url to set.
159 */
160 public void setUrl(String url) {
161 this.url = url;
162 }
163
164 /***
165 * @return Returns the username.
166 */
167 public String getUsername() {
168 return username;
169 }
170
171 /***
172 * @param username The username to set.
173 */
174 public void setUsername(String username) {
175 this.username = username;
176 }
177 }