2015-01-21 21:58:35 +01:00
|
|
|
/*******************************************************************************
|
2015-10-22 00:25:12 +02:00
|
|
|
* Copyright (c) 2015 MINRES Technologies GmbH and others.
|
2015-01-21 21:58:35 +01:00
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* MINRES Technologies GmbH - initial API and implementation
|
|
|
|
*******************************************************************************/
|
2015-01-06 17:14:16 +01:00
|
|
|
package com.minres.scviewer.database.sqlite.db;
|
2015-01-03 16:34:32 +01:00
|
|
|
|
2015-01-24 19:11:10 +01:00
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLClassLoader;
|
2015-01-03 16:34:32 +01:00
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.Statement;
|
2015-01-10 00:23:46 +01:00
|
|
|
import java.util.HashMap;
|
2015-01-03 16:34:32 +01:00
|
|
|
|
|
|
|
public class SQLiteDatabase implements IDatabase {
|
|
|
|
|
|
|
|
protected String dbFileName;
|
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
protected HashMap<String, Object> props;
|
|
|
|
|
2015-01-24 19:11:10 +01:00
|
|
|
static {
|
|
|
|
try {
|
|
|
|
URL dbUrl = SQLiteDatabase.class.getResource("/sqlite-jdbc-3.8.7.jar");
|
|
|
|
ClassLoader loader = URLClassLoader.newInstance(
|
|
|
|
new URL[] { dbUrl },
|
|
|
|
SQLiteDatabase.class.getClassLoader()
|
|
|
|
);
|
|
|
|
Class.forName("org.sqlite.JDBC", true, loader);
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
public SQLiteDatabase(String dbFileName) {
|
|
|
|
super();
|
|
|
|
this.dbFileName = dbFileName;
|
2015-01-10 00:23:46 +01:00
|
|
|
props = new HashMap<String, Object>();
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Connection createConnection() throws SQLException {
|
|
|
|
// create a database connection and return it
|
|
|
|
return DriverManager.getConnection(getConnectionUrl() );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getConnectionUrl() {
|
|
|
|
// now we set up a set of fairly basic string variables to use in the body of the code proper
|
|
|
|
String sJdbc = "jdbc:sqlite";
|
|
|
|
String sDbUrl = sJdbc + ":" + dbFileName;
|
|
|
|
// which will produce a legitimate Url for SqlLite JDBC :
|
|
|
|
// jdbc:sqlite:hello.db
|
|
|
|
return sDbUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close(ResultSet resultSet, Statement statement, Connection connection) {
|
2015-01-09 11:58:14 +01:00
|
|
|
try {
|
2015-01-20 18:50:15 +01:00
|
|
|
if(resultSet!=null) resultSet.close();
|
|
|
|
if(statement!=null) statement.close();
|
|
|
|
if(connection!=null) connection.close();
|
2015-01-09 11:58:14 +01:00
|
|
|
} catch (SQLException e) {}
|
2015-01-03 16:34:32 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close(PreparedStatement preparedStatement, Connection connection) {
|
2015-01-09 11:58:14 +01:00
|
|
|
try {
|
|
|
|
preparedStatement.close();
|
|
|
|
connection.close();
|
|
|
|
} catch (SQLException e) {}
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
@Override
|
|
|
|
public void setData(String name, Object value){
|
|
|
|
props.put(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object getData(String name){
|
|
|
|
return props.get(name);
|
|
|
|
}
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|