View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Helper class to connect to a mysql database.
27   *
28   * @author Frederico Campos
29   */
30  public class Mysql {
31      /*
32       * Helper class to connect to a Msysql database
33       *
34       * Author: Frederico Campos
35       */
36  
37      /**
38       * Db hostname.
39       */
40      private String hostname;
41  
42      /**
43       * Db port number.
44       */
45      private String port;
46  
47      /**
48       * Db username.
49       */
50      private String username;
51  
52      /**
53       * Db password.
54       */
55      private String password;
56  
57      /**
58       * Db name.
59       */
60      private String database;
61  
62      /**
63       * Constructs and initializes.
64       *
65       * @param cl
66       *            The command line
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              // find password from my.cfg or my.ini
87          }
88          this.username = "";
89          if (cl.hasOption("user")) {
90              this.username = cl.getOptionValue("user");
91          }
92      }
93  
94      /**
95       * Contructs the object with the given paramters.
96       *
97       * @param serverHostname
98       *            The MySQL hostname
99       * @param serverPort
100      *            The MySQL port
101      * @param dbUsername
102      *            The database username
103      * @param dbPassword
104      *            The database password
105      * @param databaseName
106      *            The database name
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      * Get database connection.
120      *
121      * @return The connection
122      * @throws SQLException
123      *             -
124      * @throws InstantiationException
125      *             -
126      * @throws IllegalAccessException
127      *             -
128      * @throws ClassNotFoundException
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      * Closes the connection.
155      *
156      * @param conn
157      *            The connection
158      */
159     public final void closeConnection(final Connection conn) {
160         try {
161             if (conn != null) {
162                 conn.close();
163             }
164         } catch (SQLException e) {
165             // TODO Auto-generated catch block
166             e.printStackTrace();
167         }
168     }
169 
170     /**
171      * Returns the hostname.
172      *
173      * @return the hostname
174      */
175     public final String getHostname() {
176         return hostname;
177     }
178 
179     /**
180      * Sets the hostname.
181      *
182      * @param host
183      *            the hostname to set
184      */
185     public final void setHostname(final String host) {
186         this.hostname = host;
187     }
188 
189     /**
190      * @return the port
191      */
192     public final String getPort() {
193         return port;
194     }
195 
196     /**
197      * @param serverPort
198      *            the port to set
199      */
200     public final void setPort(final String serverPort) {
201         this.port = serverPort;
202     }
203 
204     /**
205      * @return the username
206      */
207     public final String getUsername() {
208         return username;
209     }
210 
211     /**
212      * @param dbUsername
213      *            the username to set
214      */
215     public final void setUsername(final String dbUsername) {
216         this.username = dbUsername;
217     }
218 
219     /**
220      * @return the password
221      */
222     public final String getPassword() {
223         return password;
224     }
225 
226     /**
227      * @param dbPassword
228      *            the password to set
229      */
230     public final void setPassword(final String dbPassword) {
231         this.password = dbPassword;
232     }
233 
234     /**
235      * @return the database
236      */
237     public final String getDatabase() {
238         return database;
239     }
240 
241     /**
242      * @param dbName
243      *            the database to set
244      */
245     public final void setDatabase(final String dbName) {
246         this.database = dbName;
247     }
248 }