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 java.util.List; import org.gcube.common.core.faults.GCUBEFault; /** * * @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 GCUBEFault{ 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 GCUBEFault(e);} } /** * * @param table * @param values * @throws RemoteException */ public static synchronized void InsertInto(String table, List> values) throws GCUBEFault { if (values==null) throw new GCUBEFault(); 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 GCUBEFault(e);} StringBuffer insertQuery=new StringBuffer(); for (List row: values) { insertQuery.append("INSERT INTO ").append(table.toUpperCase()).append(" VALUES("); try{ for(String value: row){ insertQuery.append("'").append(value.replaceAll("'", " ")).append("'"); } insertQuery.deleteCharAt(insertQuery.length()-1).append(");"); //logger.debug(insertQuery); st.executeUpdate(insertQuery.toString()); // 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 GCUBEFault(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(); } }