2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.database.backend.mysql;
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;
33 * A backend class for MySQL connections
35 * @author Roland Haeder
37 public class MySqlDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend {
39 * An instance of a datbase connection
41 private Connection connection;
44 * Constructor with table name
46 * @param tableName Table to access
47 * @throws org.mxchange.addressbook.exceptions.UnsupportedDatabaseDriverException
49 public MySqlDatabaseBackend (final String tableName) throws UnsupportedDatabaseDriverException {
51 this.validateDriver("mysql"); //NOI18N
53 // Now that the driver is there, set the table name
54 this.setTableName(tableName);
58 public void connectToDatabase () throws SQLException {
60 this.getLogger().trace("CALLED!"); //NOI18N
62 // Generate connection string
63 String connect = String.format("jdbc:mysql://%s/%s", //NOI18N
64 this.getProperty("org.mxchange.addressbook.database.mysql.host"), //NOI18N
65 this.getProperty("org.mxchange.addressbook.database.mysql.dbname") //NOI18N
69 this.getLogger().debug(MessageFormat.format("Attempting to connect to {0} ...", connect)); //NOI18N
71 // Now get a connection instance back
72 this.connection = DriverManager.getConnection(
74 this.getProperty("org.mxchange.addressbook.database.mysql.login"), //NOI18N
75 this.getProperty("org.mxchange.addressbook.database.mysql.password") //NOI18N
79 this.getLogger().trace("EXIT!"); //NOI18N
83 public Iterator<Contact> contactIterator () throws BadTokenException {
84 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
88 public void doShutdown () {
89 // This should not happen:
90 assert(this.connection == null) : "connection is null";
93 // Close down database connection
94 this.connection.close();
95 } catch (final SQLException ex) {
96 this.abortProgramWithException(ex);
101 public long length () {
102 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
106 public void rewind () {
107 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
111 public void store (final Storeable object) throws IOException {
112 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.