]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java
A lot refacturings ...
[addressbook-swing.git] / Addressbook / src / org / mxchange / addressbook / database / backend / mysql / MySqlDatabaseBackend.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.database.backend.mysql;
18
19 import java.io.IOException;
20 import java.sql.Connection;
21 import java.sql.DriverManager;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import java.util.Iterator;
25 import org.mxchange.addressbook.contact.Contact;
26 import org.mxchange.addressbook.database.backend.BaseDatabaseBackend;
27 import org.mxchange.addressbook.database.backend.DatabaseBackend;
28 import org.mxchange.addressbook.database.storage.Storeable;
29 import org.mxchange.addressbook.exceptions.BadTokenException;
30 import org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException;
31
32 /**
33  * A backend class for MySQL connections
34  *
35  * @author Roland Haeder
36  */
37 public class MySqlDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend {
38         /**
39          * An instance of a datbase connection
40          */
41         private static Connection connection;
42
43         /**
44          * Constructor with table name
45          * 
46          * @param tableName Table to access
47          * @throws org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException
48          */
49         public MySqlDatabaseBackend (final String tableName) throws UnsupportedDatabaseDriverException {
50                 // Validate driver
51                 this.validateDriver("mysql"); //NOI18N
52
53                 // Now that the driver is there, set the table name
54                 this.setTableName(tableName);
55         }
56
57         @Override
58         public void connectToDatabase () throws SQLException {
59                 // Trace message
60                 this.getLogger().trace("CALLED!"); //NOI18N
61
62                 // Is the connection already there?
63                 if (MySqlDatabaseBackend.connection instanceof Connection) {
64                         // Already connected
65                         this.getLogger().debug("Connection is already established."); //NOI18N
66
67                         // No need to connect
68                         return;
69                 }
70
71                 // Generate connection string
72                 String connect = String.format("jdbc:mysql://%s/%s", //NOI18N
73                                 this.getProperty("database.mysql.host"), //NOI18N
74                                 this.getProperty("database.mysql.dbname") //NOI18N
75                 );
76
77                 // Debug message
78                 this.getLogger().debug(MessageFormat.format("Attempting to connect to {0} ...", connect)); //NOI18N
79
80                 // Now get a connection instance back
81                 MySqlDatabaseBackend.connection = DriverManager.getConnection(
82                                 connect,
83                                 this.getProperty("database.mysql.login"), //NOI18N
84                                 this.getProperty("database.mysql.password") //NOI18N
85                 );
86
87                 // Trace message
88                 this.getLogger().trace("EXIT!"); //NOI18N
89         }
90
91         @Override
92         @Deprecated
93         public Iterator<Contact> contactIterator () throws BadTokenException {
94                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
95         }
96
97         @Override
98         public void doShutdown () {
99                 // This should not happen:
100                 assert(MySqlDatabaseBackend.connection == null) : "connection is null"; //NOI18N
101
102                 try {
103                         // Close down database connection
104                         MySqlDatabaseBackend.connection.close();
105                 } catch (final SQLException ex) {
106                         this.abortProgramWithException(ex);
107                 }
108         }
109
110         @Override
111         public int getTotalCount () {
112                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
113         }
114
115         /**
116          * Checks whether at least one row is found with given boolean value.
117          *
118          * @param columnName Column to check for boolean value
119          * @param bool Boolean value to check
120          * @return Whether boolean value is found and returns at least one row
121          */
122         @Override
123         public boolean isRowFound (final String columnName, final boolean bool) {
124                 // Is at least one entry found?
125                 if (this.getTotalCount() == 0) {
126                         // No entry found at all
127                         return false;
128                 }
129                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
130         }
131
132         @Override
133         public long length () {
134                 throw new UnsupportedOperationException("Not implemented for this backend."); //NOI18N
135         }
136
137         @Override
138         public void rewind () {
139                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
140         }
141
142         @Override
143         public void store (final Storeable object) throws IOException {
144                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
145         }
146 }