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