[WIP ]reorganized dir structure
This commit is contained in:
@ -0,0 +1,94 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2015 MINRES Technologies GmbH and others.
|
||||
* 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
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.sqlite.db;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
|
||||
/**
|
||||
* An abstract class that handles insert/select-operations into/from a database
|
||||
*
|
||||
* @author Tino for http://www.java-blog.com
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class AbstractDatabaseHandler<T> {
|
||||
|
||||
/**
|
||||
* The type of the objects that should be created and filled with values
|
||||
* from the database or inserted into the database
|
||||
*/
|
||||
protected Class<T> type;
|
||||
|
||||
/**
|
||||
* Contains the settings to create a connection to the database like
|
||||
* host/port/database/user/password
|
||||
*/
|
||||
protected IDatabase databaseConnectionFactory;
|
||||
|
||||
/** The SQL-select-query */
|
||||
protected final String query;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param type
|
||||
* The type of the objects that should be created and filled with
|
||||
* values from the database or inserted into the database
|
||||
* @param databaseConnecter
|
||||
* Contains the settings to create a connection to the database
|
||||
* like host/port/database/user/password
|
||||
*/
|
||||
protected AbstractDatabaseHandler(Class<T> type,
|
||||
IDatabase databaseConnectionFactory, String criterion) {
|
||||
|
||||
this.databaseConnectionFactory = databaseConnectionFactory;
|
||||
this.type = type;
|
||||
this.query = createQuery(criterion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the SQL-String to insert into / select from the database
|
||||
*
|
||||
* @return the SQL-String
|
||||
*/
|
||||
protected abstract String createQuery(String criterion);
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a comma-separated-String with the names of the variables in this
|
||||
* class
|
||||
*
|
||||
* @param usePlaceHolders
|
||||
* true, if PreparedStatement-placeholders ('?') should be used
|
||||
* instead of the names of the variables
|
||||
* @return
|
||||
*/
|
||||
protected String getColumns(boolean usePlaceHolders) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
boolean first = true;
|
||||
/* Iterate the column-names */
|
||||
for (Field f : type.getDeclaredFields()) {
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
sb.append(", ");
|
||||
|
||||
if (usePlaceHolders)
|
||||
sb.append("?");
|
||||
else
|
||||
sb.append(f.getName());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2015 MINRES Technologies GmbH and others.
|
||||
* 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
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.sqlite.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a connection to a database.
|
||||
*
|
||||
* @author Tino
|
||||
* @created 03.12.2008
|
||||
*
|
||||
*/
|
||||
public interface IDatabase {
|
||||
|
||||
/**
|
||||
* Establishes a new connection to the database
|
||||
*
|
||||
* @return A new connection to the database
|
||||
* @throws SQLException
|
||||
*/
|
||||
public Connection createConnection() throws SQLException;
|
||||
|
||||
/**
|
||||
* Returns the connection url
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getConnectionUrl();
|
||||
|
||||
/**
|
||||
* releases the result set
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public void close(ResultSet resultSet, Statement statement, Connection connection);
|
||||
|
||||
/**
|
||||
* releases the preparedStatement
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public void close(PreparedStatement preparedStatement, Connection connection);
|
||||
|
||||
public void setData(String name, Object value);
|
||||
|
||||
public Object getData(String name);
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2015 MINRES Technologies GmbH and others.
|
||||
* 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
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.sqlite.db;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class SQLiteDatabase implements IDatabase {
|
||||
|
||||
protected String dbFileName;
|
||||
|
||||
protected HashMap<String, Object> props;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public SQLiteDatabase(String dbFileName) {
|
||||
super();
|
||||
this.dbFileName = dbFileName;
|
||||
props = new HashMap<String, Object>();
|
||||
}
|
||||
|
||||
@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) {
|
||||
try {
|
||||
if(resultSet!=null) resultSet.close();
|
||||
if(statement!=null) statement.close();
|
||||
if(connection!=null) connection.close();
|
||||
} catch (SQLException e) {}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(PreparedStatement preparedStatement, Connection connection) {
|
||||
try {
|
||||
preparedStatement.close();
|
||||
connection.close();
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setData(String name, Object value){
|
||||
props.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getData(String name){
|
||||
return props.get(name);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2015 MINRES Technologies GmbH and others.
|
||||
* 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
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.sqlite.db;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class that inserts a list of <T>s into the corresponding database-table.
|
||||
*
|
||||
* @author Tino for http://www.java-blog.com
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class SQLiteDatabaseInsertHandler<T> extends AbstractDatabaseHandler<T> {
|
||||
|
||||
public SQLiteDatabaseInsertHandler(Class<T> type,
|
||||
IDatabase databaseConnecter) {
|
||||
super(type, databaseConnecter, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createQuery(String criterion) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("INSERT INTO ");
|
||||
sb.append(type.getSimpleName());
|
||||
sb.append("(");
|
||||
sb.append(super.getColumns(false));
|
||||
sb.append(")");
|
||||
sb.append(" VALUES (");
|
||||
sb.append(super.getColumns(true));
|
||||
sb.append(")");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a list of <T>s into the corresponding database-table
|
||||
*
|
||||
* @param list
|
||||
* List of <T>s that should be inserted into the corresponding
|
||||
* database-table
|
||||
*
|
||||
* @throws SQLException
|
||||
* @throws SecurityException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public void insertObjects(List<T> list) throws SQLException,
|
||||
SecurityException, IllegalArgumentException,
|
||||
InstantiationException, IllegalAccessException,
|
||||
IntrospectionException, InvocationTargetException {
|
||||
|
||||
Connection connection = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
|
||||
try {
|
||||
connection = databaseConnectionFactory.createConnection();
|
||||
preparedStatement = connection.prepareStatement(query);
|
||||
|
||||
for (T instance : list) {
|
||||
int i = 0;
|
||||
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
|
||||
field.getName(), type);
|
||||
|
||||
Method method = propertyDescriptor
|
||||
.getReadMethod();
|
||||
|
||||
Object value = method.invoke(instance);
|
||||
|
||||
preparedStatement.setObject(++i, value);
|
||||
}
|
||||
|
||||
preparedStatement.addBatch();
|
||||
}
|
||||
preparedStatement.executeBatch();
|
||||
|
||||
} finally {
|
||||
databaseConnectionFactory.close(preparedStatement, connection);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2015 MINRES Technologies GmbH and others.
|
||||
* 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
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.sqlite.db;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class that creates a list of <T>s filled with values from the corresponding
|
||||
* database-table.
|
||||
*
|
||||
* @author Tino for http://www.java-blog.com
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class SQLiteDatabaseSelectHandler<T> extends AbstractDatabaseHandler<T> {
|
||||
|
||||
public SQLiteDatabaseSelectHandler(Class<T> type,
|
||||
IDatabase databaseConnectionFactory) {
|
||||
super(type, databaseConnectionFactory, null);
|
||||
}
|
||||
|
||||
public SQLiteDatabaseSelectHandler(Class<T> type,
|
||||
IDatabase databaseConnectionFactory, String criteria) {
|
||||
super(type, databaseConnectionFactory, criteria);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String createQuery(String criterion) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("SELECT ");
|
||||
sb.append(super.getColumns(false));
|
||||
sb.append(" FROM ");
|
||||
|
||||
/* We assume the table-name exactly matches the simpleName of T */
|
||||
sb.append(type.getSimpleName());
|
||||
if(criterion!=null){
|
||||
sb.append(" WHERE ( ");
|
||||
sb.append(criterion);
|
||||
sb.append(" )");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a list of <T>s filled with values from the corresponding
|
||||
* database-table
|
||||
*
|
||||
* @return List of <T>s filled with values from the corresponding
|
||||
* database-table
|
||||
*
|
||||
* @throws SQLException
|
||||
* @throws SecurityException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
public synchronized List<T> selectObjects() throws SQLException,
|
||||
SecurityException, IllegalArgumentException,
|
||||
InstantiationException, IllegalAccessException,
|
||||
IntrospectionException, InvocationTargetException {
|
||||
|
||||
Connection connection = null;
|
||||
Statement statement = null;
|
||||
ResultSet resultSet = null;
|
||||
|
||||
try {
|
||||
connection = databaseConnectionFactory.createConnection();
|
||||
statement = connection.createStatement();
|
||||
resultSet = statement.executeQuery(query);
|
||||
|
||||
return createObjects(resultSet);
|
||||
|
||||
} finally {
|
||||
databaseConnectionFactory.close(resultSet, statement, connection);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a list of <T>s filled with values from the provided ResultSet
|
||||
*
|
||||
* @param resultSet
|
||||
* ResultSet that contains the result of the
|
||||
* database-select-query
|
||||
*
|
||||
* @return List of <T>s filled with values from the provided ResultSet
|
||||
*
|
||||
* @throws SecurityException
|
||||
* @throws IllegalArgumentException
|
||||
* @throws SQLException
|
||||
* @throws InstantiationException
|
||||
* @throws IllegalAccessException
|
||||
* @throws IntrospectionException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
private List<T> createObjects(ResultSet resultSet)
|
||||
throws SecurityException, IllegalArgumentException,
|
||||
SQLException, InstantiationException,
|
||||
IllegalAccessException, IntrospectionException,
|
||||
InvocationTargetException {
|
||||
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
while (resultSet.next()) {
|
||||
|
||||
T instance = type.newInstance();
|
||||
|
||||
for (Field field : type.getDeclaredFields()) {
|
||||
|
||||
/* We assume the table-column-names exactly match the variable-names of T */
|
||||
Object value = resultSet.getObject(field.getName());
|
||||
if(value!=null){
|
||||
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type);
|
||||
propertyDescriptor.getWriteMethod().invoke(instance, value);
|
||||
}
|
||||
}
|
||||
|
||||
list.add(instance);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user