Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* $Id$
*
* Copyright (C) Seungkeun Lee, 2006
* Copyright (C) INRIA, 2007-2009
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package fr.inrialpes.exmo.align.service;
import java.lang.ClassNotFoundException;
import java.lang.IllegalAccessException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
public class DBServiceImpl implements DBService{
int id = 0;
Connection conn = null;
static String IPAddress = "localhost";
static String port = "3306";
static String user = "adminAServ";
Chan Leduc
committed
static String database = "AServDB";
//To be used in reconnect()
Chan Leduc
committed
String driverPrefix = "jdbc:mysql";
//String driverPrefix = "jdbc:postgresql";
CacheImpl cache = null;
public DBServiceImpl() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Chan Leduc
committed
Class.forName("com.mysql.jdbc.Driver").newInstance();
//Class.forName("org.postgresql.Driver").newInstance();
}
public DBServiceImpl( String driver, String prefix, String DBPort ) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class.forName(driver).newInstance();
Chan Leduc
committed
driverPrefix = prefix;
port = DBPort;
}
public void init() {
}
public void connect( String password ) throws SQLException {
connect( IPAddress, port, user, password, database );
}
public void connect( String user, String password ) throws SQLException {
connect( IPAddress, port, user, password, database );
}
public void connect( String port, String user, String password ) throws SQLException {
connect( IPAddress, port, user, password, database );
}
public void connect(String IPAddress, String port, String user, String password ) throws SQLException {
connect( IPAddress, port, user, password, database );
}
Chan Leduc
committed
public void connect(String IPAddress, String port, String user, String password, String database ) throws SQLException {
dbpass = password;
conn = DriverManager.getConnection(driverPrefix+"://"+IPAddress+":"+port+"/"+database, user, password);
}
Chan Leduc
committed
//with "dbpass" given by "connect"
public Connection reconnect() throws SQLException {
conn = DriverManager.getConnection(driverPrefix+"://"+IPAddress+":"+port+"/"+database, user, dbpass);
return conn;
Chan Leduc
committed
}
Chan Leduc
committed
public Connection getConnection() throws SQLException {
if (conn==null || conn.isClosed())
return reconnect();
return conn;
}
public void close() {
try {
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
Chan Leduc
committed
}