public DatabaseExporter(ConnectionHelper connectionHelper, SqlExceptionHelper sqlExceptionHelper) throws SQLException { this.connectionHelper = connectionHelper; this.sqlExceptionHelper = sqlExceptionHelper; connectionHelper.prepare(false); // CUSTOM: changed auto-commit flag to false connection = connectionHelper.getConnection(); statement = connection.createStatement(); }
public void connect(String fileName, String tableName) throws DAOException { try { c = ConnectionHelper.getConnection(fileName); Statement s = c.createStatement(); s.execute( "CREATE TABLE IF NOT EXISTS " + tableName + " (" + " id INTEGER, " + " numProcessed INTEGER, " + " totalToProcess INTEGER, " + " startUp INTEGER, " + " runTime INTEGER, " + " shutDown INTEGER " + ")"); s.execute("DELETE FROM " + tableName); } catch (SQLException e) { e.printStackTrace(); throw new DAOException(e); } }
private void execute( boolean script, boolean export, Writer fileOutput, Statement statement, final String sql) throws IOException, SQLException { final SqlExceptionHelper sqlExceptionHelper = new SqlExceptionHelper(); String formatted = formatter.format(sql); if (delimiter != null) formatted += delimiter; if (script) System.out.println(formatted); LOG.debug(formatted); if (outputFile != null) { fileOutput.write(formatted + "\n"); } if (export) { statement.executeUpdate(sql); try { SQLWarning warnings = statement.getWarnings(); if (warnings != null) { sqlExceptionHelper.logAndClearWarnings(connectionHelper.getConnection()); } } catch (SQLException sqle) { LOG.unableToLogSqlWarnings(sqle); } } }
@Override public void release() throws Exception { try { statement.close(); } finally { connectionHelper.release(); } }
/** * To check whether an active connection exists to a given broker. * * @param brokerURI The URI of the broker to check for the connection. * @return true is the client is connected to the given broker; false otherwise. * @throws ClientException If the given URI is malformatted. * @see NodeAddress , {@link #connect(String)} */ public boolean connectionIsActive(String brokerURI) throws ClientException { try { NodeAddress brokerAddr = ConnectionHelper.getAddress(brokerURI); return brokerStates.containsKey(brokerAddr); } catch (CommunicationException e) { throw new ClientException(e.getMessage(), e); } }
/** * Provided a broker URI, this method returns the associated broker state. The broker state * contains the details about the broker connection as well as, depending on the configuration, * the adv/sub messages sent to the specified broker. * * @param brokerURI * @return * @throws ClientException If the given broker URI is mal-formatted */ public BrokerState getBrokerState(String brokerURI) throws ClientException { try { NodeAddress brokerAddress = ConnectionHelper.getAddress(brokerURI); return brokerStates.get(brokerAddress); } catch (CommunicationException e) { throw new ClientException("Could not get broker status: " + e.getMessage(), e); } }
/** * DOC tguiu Comment method "doGetTableNames". * * @param connection * @return */ private static List<String> doGetTableNames(Connection connection) { List<String> result = new ArrayList<String>(15); for (MetadataTable table : ConnectionHelper.getTables(connection)) { if (table == null) { continue; } result.add(table.getLabel()); } return result; }
/** * return the first connection available that refers to this table by moving up to the last * Package instance that hold this table and finding the connection linked to it * * @param metadataTable * @return */ public static Connection getFirstConnection(MetadataTable metadataTable) { assert metadataTable != null; Connection connection = null; Namespace namespace = metadataTable.getNamespace(); // for the file connection if (namespace != null && namespace instanceof RecordFile) { return ConnectionHelper.getConnection((RecordFile) namespace); } // for the mdm connection if (namespace != null && namespace instanceof TdXmlSchema) { return ConnectionHelper.getConnection((Package) namespace); } if (namespace != null) { Package thePackage = SwitchHelpers.PACKAGE_SWITCH.doSwitch(namespace); if (thePackage != null) { connection = getFirstPackageConnection(thePackage); } } // else class is not linked to any package return connection; }
public InventoryRecord readItem() throws Exception { Connection connection = null; PreparedStatement statement = null; ResultSet rs = null; try { connection = ConnectionHelper.getConnection(dataSource); statement = connection.prepareStatement(ConnectionHelper.SELECT_INVENTORY); statement.setInt(1, 1); rs = statement.executeQuery(); int quantity = -1; while (rs.next()) { quantity = rs.getInt("quantity"); } // If we run out of items we are done so stop processing orders if (quantity < 1) { return null; } // decrement the quantity and update the table InventoryRecord ir = new InventoryRecord(1, --quantity); decrementInventory(connection, ir); readerIndex++; this.inventoryCheckpoint.setInventoryCount(readerIndex); return new InventoryRecord(1, 1); // Every order only orders 1 item } catch (SQLException e) { throw e; } finally { ConnectionHelper.cleanupConnection(connection, rs, statement); } }
public static String search(String name, String year, String season) { Connection c = ConnectionHelper.connect(); String result = ""; try { if (!name.equals("")) name = "name='" + name + "'"; else name = "name LIKE '%'"; if (!year.equals("")) year = "year='" + year + "'"; else year = "year LIKE '%'"; if (!season.equals("")) season = "season='" + season + "'"; else season = "season LIKE '%'"; String sql_query = "SELECT * " + "FROM archives " + "WHERE " + name + " AND " + year + " AND " + season; PreparedStatement stmt = c.prepareStatement(sql_query); ResultSet rs = stmt.executeQuery(); while (rs.next()) { result = result + rs.getString("filename") + ";"; } } catch (Exception e) { result = "database error"; } ConnectionHelper.close(c); return result; }
/** * DOC tguiu Comment method "findByLabel". * * @deprecated it would be better to use find with some unique identifier * @param connection * @param label * @return */ @Deprecated public static MetadataTable findByLabel(Connection connection, String label) { if (connection == null) { throw new IllegalArgumentException("null connection"); // $NON-NLS-1$ } if (label == null || "".equals(label)) { throw new IllegalArgumentException("null/empty label"); // $NON-NLS-1$ } Set<MetadataTable> tables = ConnectionHelper.getTables(connection); for (MetadataTable table : tables) { if (label.equals(table.getLabel())) { return table; } } return null; }
@Override public void init() throws DBException { Properties props = getProperties(); String servers = props.getProperty("voltdb.servers", "localhost"); String user = props.getProperty("voltdb.user", ""); String password = props.getProperty("voltdb.password", ""); String strLimit = props.getProperty("voltdb.ratelimit"); int ratelimit = strLimit != null ? Integer.parseInt(strLimit) : Integer.MAX_VALUE; try { m_client = ConnectionHelper.createConnection( Thread.currentThread().getId(), servers, user, password, ratelimit); } catch (Exception e) { e.printStackTrace(); throw new DBException(e.getMessage()); } m_workingData = new byte[1024 * 1024]; m_writeBuf = ByteBuffer.wrap(m_workingData); }
/** * Connects to a broker with the given URI. The given URI should conform to an accepted * communication protocol format. * * @param brokerURI URI of the broker to connect to. * @return A BrokerState data structure to keep track of the state of the connection and related * operation * @throws ClientException In case the given URI is malformated, a connection already exists to * the specified broker, or a communication error is occurred. * @see BrokerState, NodeAddress */ public BrokerState connect(String brokerURI) throws ClientException { try { NodeAddress brokerAddr = ConnectionHelper.getAddress(brokerURI); if (brokerStates.containsKey(brokerAddr)) { throw new ClientException("Server connection already exists"); } else { if (brokerStates.size() == 0) setDefaultBrokerAddress(brokerAddr); MessageSender msgSender = commSystem.getMessageSender(brokerURI); BrokerState bState = addBrokerState(brokerAddr, msgSender); msgSender.connect( MessageDestination.formatClientDestination(clientID, brokerAddr.getNodeURI()), msgListener); return bState; } } catch (CommunicationException e) { exceptionLogger.error("Could not connect to broker: " + e); throw new ClientException("Could not connect to broker: " + e.getMessage(), e); } }
@Override public void cleanup() throws DBException { ConnectionHelper.disconnect(Thread.currentThread().getId()); }