2008-04-22 14:11:53 +02:00
|
|
|
package org.gcube.vremanagement.vremodeler.db;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.rmi.RemoteException;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.Statement;
|
2008-10-21 11:13:34 +02:00
|
|
|
import java.util.List;
|
|
|
|
import org.gcube.common.core.faults.GCUBEFault;
|
2008-10-30 19:54:34 +01:00
|
|
|
import org.gcube.common.core.utils.logging.GCUBELog;
|
2008-04-22 14:11:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author lucio
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class DBInterface {
|
|
|
|
|
2008-04-22 15:04:13 +02:00
|
|
|
|
2008-04-22 14:11:53 +02:00
|
|
|
private static Connection conn;
|
|
|
|
|
2008-10-30 19:54:34 +01:00
|
|
|
private static String dbFile = System.getenv("GLOBUS_LOCATION") + File.separator + "etc" + File.separator + "org.gcube.vremanagement.vremodeler" + File.separator + "hsqldb" + File.separator + "vdldb";
|
2008-04-22 14:11:53 +02:00
|
|
|
|
2008-10-30 19:54:34 +01:00
|
|
|
private static GCUBELog logger = new GCUBELog(DBInterface.class.getName());
|
2008-04-22 14:11:53 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* @throws SQLException
|
|
|
|
*/
|
|
|
|
public static Connection connect() throws SQLException{
|
|
|
|
|
|
|
|
if(conn==null)
|
|
|
|
{
|
|
|
|
// Load the HSQL Database Engine JDBC driver
|
|
|
|
// hsqldb.jar should be in the class path or made part of the current jar
|
|
|
|
try{
|
|
|
|
Class.forName("org.hsqldb.jdbcDriver");
|
|
|
|
}catch(ClassNotFoundException e){throw new SQLException(e.getMessage());}
|
|
|
|
// connect to the database. This will load the db files and start the
|
|
|
|
// database if it is not alread running.
|
|
|
|
// db_file_name_prefix is used to open or create files that hold the state
|
|
|
|
// of the db.
|
|
|
|
// It can contain directory names relative to the
|
|
|
|
// current working directory
|
|
|
|
conn = DriverManager.getConnection("jdbc:hsqldb:file:"
|
|
|
|
+ dbFile, // filenames
|
|
|
|
"sa", // username
|
|
|
|
""); // password
|
|
|
|
}
|
|
|
|
return conn;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param query
|
|
|
|
*/
|
2008-10-21 11:13:34 +02:00
|
|
|
public static synchronized void ExecuteUpdate(String query) throws GCUBEFault{
|
2008-04-22 14:11:53 +02:00
|
|
|
Statement st = null;
|
|
|
|
|
|
|
|
try{
|
|
|
|
st = conn.createStatement(); // statement objects can be reused with
|
|
|
|
st.executeUpdate(query);
|
|
|
|
st.close();
|
2008-04-22 19:00:29 +02:00
|
|
|
}catch(SQLException e){//logger.error("error executing Query", e);
|
2008-10-21 11:13:34 +02:00
|
|
|
throw new GCUBEFault(e);}
|
2008-04-22 19:00:29 +02:00
|
|
|
|
2008-04-22 14:11:53 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 11:13:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-04-22 14:11:53 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param table
|
|
|
|
* @param values
|
|
|
|
* @throws RemoteException
|
|
|
|
*/
|
2008-10-21 11:13:34 +02:00
|
|
|
public static synchronized void InsertInto(String table, List<List<String>> values) throws GCUBEFault {
|
2008-04-22 14:11:53 +02:00
|
|
|
|
2008-10-21 11:13:34 +02:00
|
|
|
if (values==null) throw new GCUBEFault();
|
2008-04-22 14:11:53 +02:00
|
|
|
Statement st = null;
|
2008-10-30 19:54:34 +01:00
|
|
|
|
2008-04-22 14:11:53 +02:00
|
|
|
try{
|
|
|
|
st = conn.createStatement(); // statement objects can be reused with
|
2008-04-22 19:00:29 +02:00
|
|
|
}catch(SQLException e){//logger.error("error creating SQL statement");
|
2008-10-21 11:13:34 +02:00
|
|
|
throw new GCUBEFault(e);}
|
2008-04-22 19:00:29 +02:00
|
|
|
|
2008-10-30 19:54:34 +01:00
|
|
|
StringBuffer insertQuery;
|
2008-10-21 11:13:34 +02:00
|
|
|
|
|
|
|
for (List<String> row: values)
|
2008-04-22 14:11:53 +02:00
|
|
|
{
|
2008-10-30 19:54:34 +01:00
|
|
|
|
|
|
|
insertQuery=new StringBuffer();
|
2008-10-21 11:13:34 +02:00
|
|
|
insertQuery.append("INSERT INTO ").append(table.toUpperCase()).append(" VALUES(");
|
2008-04-22 14:11:53 +02:00
|
|
|
try{
|
2008-10-21 11:13:34 +02:00
|
|
|
|
|
|
|
for(String value: row){
|
2008-10-30 19:54:34 +01:00
|
|
|
insertQuery.append("'").append(value.replaceAll("'", " ")).append("',");
|
2008-10-21 11:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
insertQuery.deleteCharAt(insertQuery.length()-1).append(");");
|
2008-04-22 19:00:29 +02:00
|
|
|
//logger.debug(insertQuery);
|
2008-10-21 11:13:34 +02:00
|
|
|
|
|
|
|
st.executeUpdate(insertQuery.toString()); // run the query
|
2008-10-30 19:54:34 +01:00
|
|
|
}catch(SQLException e){logger.error("HSQLDB ERROR: Problem inserting data "+e.getMessage()+" -- "+ insertQuery);
|
|
|
|
System.out.println("HSQLDB ERROR: Problem inserting data "+e.getMessage()+" -- "+ insertQuery);
|
|
|
|
e.printStackTrace();
|
2008-04-22 19:00:29 +02:00
|
|
|
}
|
|
|
|
|
2008-12-02 11:23:05 +01:00
|
|
|
catch(Exception e){
|
|
|
|
logger.error("VDLModelService error: Problem inserting data "+e.getMessage());
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2008-04-22 14:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
st.close();
|
2008-10-30 19:54:34 +01:00
|
|
|
}catch(SQLException e){logger.error("error closing SQL statement");
|
2008-10-21 11:13:34 +02:00
|
|
|
throw new GCUBEFault(e);}
|
2008-04-22 14:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param query
|
|
|
|
* @return
|
|
|
|
* @throws SQLException
|
|
|
|
*/
|
|
|
|
public static synchronized ResultSet queryDB(String query) throws SQLException {
|
|
|
|
|
|
|
|
Statement st = null;
|
|
|
|
ResultSet rs = null;
|
|
|
|
|
|
|
|
st = conn.createStatement(); // statement objects can be reused with
|
|
|
|
|
|
|
|
|
|
|
|
rs = st.executeQuery(query); // run the query
|
|
|
|
|
|
|
|
st.close();
|
|
|
|
return rs;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-10-21 11:13:34 +02:00
|
|
|
|
|
|
|
|
2008-04-22 14:11:53 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param table
|
|
|
|
* @throws SQLException
|
|
|
|
*/
|
|
|
|
public static void deleteAll(String table) throws SQLException{
|
|
|
|
String query="DELETE FROM "+table.toUpperCase();
|
|
|
|
Statement st=conn.createStatement();
|
|
|
|
st.executeUpdate(query);
|
|
|
|
st.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|