vre-modeler/src/org/gcube/vremanagement/vremodeler/db/DBInterface.java

150 lines
4.5 KiB
Java
Raw Normal View History

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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gcube.common.core.utils.logging.GCUBELog;
/**
*
* @author lucio
*
*/
public class DBInterface {
private static Connection conn;
private static String dbFile = System.getenv("GLOBUS_LOCATION") + File.separator + "etc" + File.separator + "org_diligentproject_vdlgeneratorservice_model" + File.separator + "hsqldb" + File.separator + "vdldb";
/**
*
* @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
*/
public static synchronized void ExecuteUpdate(String query) throws RemoteException{
Statement st = null;
try{
st = conn.createStatement(); // statement objects can be reused with
st.executeUpdate(query);
st.close();
}catch(SQLException e){logger.error("error executing Query", e);throw new RemoteException("error creating SQL statement",e);}
}
/**
*
* @param table
* @param values
* @throws RemoteException
*/
public static synchronized void InsertInto(String table, String[][] values, boolean deleteOne) throws RemoteException {
if (values==null) return;
Statement st = null;
ResultSet rs = null;
try{
st = conn.createStatement(); // statement objects can be reused with
}catch(SQLException e){logger.error("error creating SQL statement");throw new RemoteException("error creating SQL statement",e);}
String insertQuery="";
for (String[] row: values)
{
insertQuery="INSERT INTO "+table.toUpperCase()+" VALUES(";
try{
int decr=0;
if (deleteOne) decr=-1;
for (int i=0; i<row.length+decr; i++)
if (i==(row.length+decr-1)) insertQuery+="'"+row[i].replaceAll("'"," ")+"'";
else insertQuery+="'"+row[i].replaceAll("'"," ")+"',";
insertQuery+=");";
logger.debug(insertQuery);
//System.out.println(insertQuery);
st.executeUpdate(insertQuery); // run the query
}catch(SQLException e){logger.error("HSQLDB ERROR: Problem inserting data "+e.getMessage()+" -- "+insertQuery);}
catch(Exception e){logger.error("VDLModelService error: Problem inserting data "+e.getMessage());}
}
try{
st.close();
}catch(SQLException e){logger.error("error closing SQL statement");throw new RemoteException("error closing SQL statement",e);}
}
/**
*
* @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;
}
/**
*
* @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();
}
}