/** * Opens a connection to the database. * * @throws Exception if an error occurs */ public void connectToDatabase() throws Exception { if (m_Debug) { System.err.println("Connecting to " + m_DatabaseURL); } if (m_Connection == null) { try { connectUsingDriverManager(); } catch (Exception ex) { connectViaPackageLoadedDriver(); } if (m_Connection == null) { throw new SQLException("Unable to find a suitable driver for " + m_DatabaseURL); } m_Connection.setAutoCommit(m_setAutoCommit); } }
/** * Executes a SQL query. Caller must clean up manually with * <code>close()</code>. * * @param query the SQL query * @return true if the query generated results * @throws SQLException if an error occurs * @see #close() */ public boolean execute(String query) throws SQLException { if (!isConnected()) { throw new IllegalStateException("Not connected, please connect first!"); } if (!isCursorScrollable()) { m_PreparedStatement = m_Connection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); } else { m_PreparedStatement = m_Connection.prepareStatement(query, getSupportedCursorScrollType(), ResultSet.CONCUR_READ_ONLY); } return (m_PreparedStatement.execute()); }
/** * Initializes the database connection. * * @param propsFile the props file to load, ignored if null or pointing to a * directory */ public void initialize(File propsFile) { initialize(loadProperties(propsFile)); }
query += "'" + processKeyString(key[i].toString()) + "'"; } else { query += key[i].toString(); ResultSet rs = select(query); ResultSetMetaData md = rs.getMetaData(); int numAttributes = md.getColumnCount(); switch (translateDBColumnType(md.getColumnTypeName(i))) { case STRING: result[i - 1] = rs.getString(i); default: throw new Exception("Unhandled SQL result type (field " + (i + 1) + "): " + DatabaseUtils.typeName(md.getColumnType(i))); + query); close(rs); return result;
query += "'" + processKeyString(key[i].toString()) + "'"; } else if (key[i] instanceof Double) { query += safeDoubleToString((Double) key[i]); } else { query += key[i].toString(); query += "'" + element.toString() + "'"; } else if (element instanceof Double) { query += safeDoubleToString((Double) element); } else { query += element.toString(); System.err.println("Submitting result: " + query); update(query); close();
ResultSet rs = select(query); if (m_Debug) { System.err.println("...getting number of rows"); numRows = rs.getInt(1); close(rs); "INSERT INTO " + EXP_INDEX_TABLE + " VALUES ('" + expType + "', '" + expParams + "', " + numRows + " )"; if (update(query) > 0) { if (m_Debug) { System.err.println("...create returned resultset"); close(); String tableName = getResultsTableName(rp); if (tableName == null) { throw new Exception("Problem adding experiment index entry"); System.err.println(query); update(query); } catch (SQLException ex) { System.err.println(ex.getMessage());
query += "'" + processKeyString(key[i].toString()) + "'"; } else { query += key[i].toString(); ResultSet rs = select(query); if (rs.next()) { retval = true; close(rs); return retval;
/** * Returns whether the cursors only support forward movement or are scroll * sensitive (with ResultSet.CONCUR_READ_ONLY concurrency). Returns always * false if not connected * * @return true if connected and the cursor is scroll-sensitive * @see ResultSet#TYPE_SCROLL_SENSITIVE * @see ResultSet#TYPE_FORWARD_ONLY * @see ResultSet#CONCUR_READ_ONLY */ public boolean isCursorScrollSensitive() { boolean result; result = false; try { if (isConnected()) { result = m_Connection.getMetaData().supportsResultSetConcurrency( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); } } catch (Exception e) { // ignored } return result; }
/** * Checks whether any duplicate results (with respect to a key template) were * received. * * @param template the template key. * @throws Exception if duplicate results are detected */ protected void checkForDuplicateKeys(Object[] template) throws Exception { Hashtable<Object, Object> hash = new Hashtable<Object, Object>(); int numMatches = 0; for (int i = 0; i < m_Keys.size(); i++) { Object[] current = m_Keys.get(i); // Skip non-matching keys if (!matchesTemplate(template, current)) { continue; } if (hash.containsKey(current[m_KeyIndex])) { throw new Exception("Duplicate result received:" + DatabaseUtils.arrayToString(current)); } numMatches++; hash.put(current[m_KeyIndex], current[m_KeyIndex]); } if (numMatches != m_ExpectedResultsPerAverage) { throw new Exception("Expected " + m_ExpectedResultsPerAverage + " results matching key \"" + DatabaseUtils.arrayToString(template) + "\" but got " + numMatches); } }
/** * Checks whether cursors are scrollable in general, false otherwise (also if * not connected). * * @return true if scrollable and connected * @see #getSupportedCursorScrollType() */ public boolean isCursorScrollable() { return (getSupportedCursorScrollType() != -1); }
/** * Uses the specified properties to set up the database drivers. * * @param props the properties to use, ignored if null * @throws Exception if an error occurs */ public DatabaseUtils(Properties props) throws Exception { if (DRIVERS_ERRORS == null) { DRIVERS_ERRORS = new Vector<String>(); } initialize(props); }
/** * Open a connection by trying all package-loaded JDBC drivers * * @throws Exception if a problem occurs */ protected void connectViaPackageLoadedDriver() throws Exception { java.util.Properties info = new java.util.Properties(); if (m_userName.length() > 0) { info.put("user", m_userName); } if (m_password.length() > 0) { info.put("password", m_password); } for (String driverClass : DRIVERS) { Driver driver = getDriver(driverClass); if (driver != null) { Connection conn = driver.connect(m_DatabaseURL, info); if (conn != null) { m_Connection = conn; break; } } } }
query += "'" + processKeyString(key[i].toString()) + "'"; } else { query += key[i].toString(); ResultSet rs = select(query); ResultSetMetaData md = rs.getMetaData(); int numAttributes = md.getColumnCount(); switch (translateDBColumnType(md.getColumnTypeName(i))) { case STRING: result[i - 1] = rs.getString(i); default: throw new Exception("Unhandled SQL result type (field " + (i + 1) + "): " + DatabaseUtils.typeName(md.getColumnType(i))); + query); close(rs); return result;
query += "'" + processKeyString(key[i].toString()) + "'"; } else if (key[i] instanceof Double) { query += safeDoubleToString((Double) key[i]); } else { query += key[i].toString(); query += "'" + element.toString() + "'"; } else if (element instanceof Double) { query += safeDoubleToString((Double) element); } else { query += element.toString(); System.err.println("Submitting result: " + query); update(query); close();
ResultSet rs = select(query); if (m_Debug) { System.err.println("...getting number of rows"); numRows = rs.getInt(1); close(rs); "INSERT INTO " + EXP_INDEX_TABLE + " VALUES ('" + expType + "', '" + expParams + "', " + numRows + " )"; if (update(query) > 0) { if (m_Debug) { System.err.println("...create returned resultset"); close(); String tableName = getResultsTableName(rp); if (tableName == null) { throw new Exception("Problem adding experiment index entry"); System.err.println(query); update(query); } catch (SQLException ex) { System.err.println(ex.getMessage());
query += "'" + processKeyString(key[i].toString()) + "'"; } else { query += key[i].toString(); ResultSet rs = select(query); if (rs.next()) { retval = true; close(rs); return retval;
/** * Initializes the database connection. * * @param propsFile the props file to load, ignored if null or pointing to a * directory */ public void initialize(File propsFile) { initialize(loadProperties(propsFile)); }