1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it.jnrpe.plugin.mysql;
17
18 import it.jnrpe.ICommandLine;
19
20 import java.sql.Connection;
21 import java.sql.Driver;
22 import java.sql.DriverManager;
23 import java.sql.SQLException;
24
25
26
27
28
29
30 public class Mysql {
31
32
33
34
35
36
37
38
39
40 private String hostname;
41
42
43
44
45 private String port;
46
47
48
49
50 private String username;
51
52
53
54
55 private String password;
56
57
58
59
60 private String database;
61
62
63
64
65
66
67
68 public Mysql(final ICommandLine cl) {
69 this.database = "mysql";
70 if (cl.hasOption("database")) {
71 this.database = cl.getOptionValue("database");
72 }
73 this.hostname = "localhost";
74 if (cl.hasOption("hostname")
75 && !"".equals(cl.getOptionValue("hostname"))) {
76 this.hostname = cl.getOptionValue("hostname");
77 }
78 this.port = "3306";
79 if (cl.hasOption("port") && !"".equals(cl.getOptionValue("port"))) {
80 this.port = cl.getOptionValue("port");
81 }
82 this.password = "";
83 if (cl.hasOption("password")) {
84 this.password = cl.getOptionValue("password");
85 } else {
86
87 }
88 this.username = "";
89 if (cl.hasOption("user")) {
90 this.username = cl.getOptionValue("user");
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public Mysql(final String serverHostname, final String serverPort,
109 final String dbUsername, final String dbPassword,
110 final String databaseName) {
111 this.hostname = serverHostname;
112 this.port = serverPort;
113 this.username = dbUsername;
114 this.password = dbPassword;
115 this.database = databaseName;
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public final Connection getConnection() throws SQLException,
132 InstantiationException, IllegalAccessException,
133 ClassNotFoundException {
134 String url =
135 "jdbc:mysql://"
136 + this.hostname
137 + ":"
138 + this.port
139 + "/"
140 + this.database
141 + "?user="
142 + this.username
143 + "&password="
144 + this.password
145 + "&autoReconnect=true"
146 + "&failOverReadOnly=false&maxReconnects=3";
147 DriverManager.registerDriver((Driver) Class.forName(
148 "com.mysql.jdbc.Driver").newInstance());
149 Connection conn = DriverManager.getConnection(url);
150 return conn;
151 }
152
153
154
155
156
157
158
159 public final void closeConnection(final Connection conn) {
160 try {
161 if (conn != null) {
162 conn.close();
163 }
164 } catch (SQLException e) {
165
166 e.printStackTrace();
167 }
168 }
169
170
171
172
173
174
175 public final String getHostname() {
176 return hostname;
177 }
178
179
180
181
182
183
184
185 public final void setHostname(final String host) {
186 this.hostname = host;
187 }
188
189
190
191
192 public final String getPort() {
193 return port;
194 }
195
196
197
198
199
200 public final void setPort(final String serverPort) {
201 this.port = serverPort;
202 }
203
204
205
206
207 public final String getUsername() {
208 return username;
209 }
210
211
212
213
214
215 public final void setUsername(final String dbUsername) {
216 this.username = dbUsername;
217 }
218
219
220
221
222 public final String getPassword() {
223 return password;
224 }
225
226
227
228
229
230 public final void setPassword(final String dbPassword) {
231 this.password = dbPassword;
232 }
233
234
235
236
237 public final String getDatabase() {
238 return database;
239 }
240
241
242
243
244
245 public final void setDatabase(final String dbName) {
246 this.database = dbName;
247 }
248 }