Example usage for java.sql SQLException SQLException

List of usage examples for java.sql SQLException SQLException

Introduction

In this page you can find the example usage for java.sql SQLException SQLException.

Prototype

public SQLException(Throwable cause) 

Source Link

Document

Constructs a SQLException object with a given cause.

Usage

From source file:org.apache.lucene.store.jdbc.index.AbstractJdbcIndexOutput.java

public void close() throws IOException {
    super.close();
    final long length = length();
    doBeforeClose();/*from  w  w w  .  ja  v  a 2s. c  om*/
    jdbcDirectory.getJdbcTemplate().update(jdbcDirectory.getTable().sqlInsert(), new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            InputStream is = null;
            try {
                is = openInputStream();
                if (jdbcDirectory.getDialect().useInputStreamToInsertBlob()) {
                    ps.setBinaryStream(2, is, (int) length());
                } else {
                    ps.setBlob(2, new InputStreamBlob(is, length));
                }
                ps.setLong(3, length);
                ps.setBoolean(4, false);
            } catch (IOException e) {
                throw new SQLException(e);
            }
        }
    });
    doAfterClose();
}

From source file:com.qubole.quark.catalog.db.encryption.MysqlAES.java

public String convertToEntityAttribute(String phrase) throws SQLException {
    try {/*from w  w w. j  a  va 2 s .c om*/
        Cipher decryptCipher = Cipher.getInstance("AES");
        decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(this.key, "UTF-8"));

        return new String(decryptCipher.doFinal(Hex.decodeHex(phrase.toCharArray())));
    } catch (Exception e) {
        throw new SQLException(e);
    }

}

From source file:org.jtalks.poulpe.util.databasebackup.persistence.KeyListProcessor.java

@Override
public Object processMetaData(DatabaseMetaData dmd) throws SQLException {
    Set<TableKey> tableKeySet = new HashSet<TableKey>();
    ResultSet rs = null;//from   w w w .  j  a v a2  s .  c  o  m
    try {
        rs = tableKeyPerformer.getResultSet(dmd, tableName);
        while (rs.next()) {
            tableKeyPerformer.addKeyToSet(rs, tableKeySet);
        }
    } catch (SQLException e) {
        throw new SQLException(e);
    } finally {
        if (rs != null) {
            rs.close();
        }
    }
    return tableKeySet;
}

From source file:com.jolbox.bonecp.spring.DynamicDataSourceProxy.java

/** Switch to a new DataSource using the given configuration.
 * @param newConfig BoneCP DataSource to use.
 * @throws SQLException/*from ww w . j  a  va 2  s  . c  o  m*/
 */
public void switchDataSource(BoneCPConfig newConfig) throws SQLException {
    logger.info("Switch to new datasource requested. New Config: " + newConfig);
    DataSource oldDS = getTargetDataSource();

    if (!(oldDS instanceof BoneCPDataSource)) {
        throw new SQLException("Unknown datasource type! Was expecting BoneCPDataSource but received "
                + oldDS.getClass() + ". Not switching datasource!");
    }

    BoneCPDataSource newDS = new BoneCPDataSource(newConfig);
    newDS.getConnection().close(); // initialize a connection (+ throw it away) to force the datasource to initialize the pool

    // force application to start using the new one 
    setTargetDataSource(newDS);

    logger.info("Shutting down old datasource slowly. Old Config: " + oldDS);
    // tell the old datasource to terminate. This terminates the pool lazily so existing checked out connections can still be used.
    ((BoneCPDataSource) oldDS).close();
}

From source file:com.tesora.dve.mysqlapi.repl.MyReplicationVisitorDispatch.java

public static void executeLDB(ServerDBConnection dbCon, final ChannelHandlerContext channelHandlerContext,
        final byte[] readData) throws SQLException {
    try {/*from   w  w w .  j a v  a 2s .co m*/
        final SSConnection ssCon1 = dbCon.getSSConn();
        Throwable t = ssCon1.executeInContext(new Callable<Throwable>() {
            public Throwable call() {
                try {
                    LoadDataBlockExecutor.executeInsert(channelHandlerContext, ssCon1,
                            LoadDataBlockExecutor.processDataBlock(channelHandlerContext, ssCon1, readData));
                } catch (Throwable e) {
                    return e;
                }
                return null;
            }
        });
        if (t != null && t.getCause() != null) {
            throw new PEException(t);
        }
    } catch (Throwable t) {
        throw new SQLException(t);
    }
}

From source file:com.iksgmbh.sql.pojomemodb.sqlparser.CreateTableParser.java

public TableMetaData parseCreateTableStatement(final String sql) throws SQLException {
    final InterimParseResult parseResult = parseTableName(sql);
    final String tableName = removeSurroundingQuotes(parseResult.parsedValue);
    final TableMetaData tableMetaData = new Table(tableName);

    if (!parseResult.unparsedRest.startsWith("(")) {
        throw new SQLException("Left parenthis missing...");
    }/*from w  w  w . ja va2s  .c  om*/

    int numOpen = StringParseUtil.countOccurrencesOf(parseResult.unparsedRest, OPENING_PARENTHESIS.charAt(0));
    int numClose = StringParseUtil.countOccurrencesOf(parseResult.unparsedRest, CLOSING_PARENTHESIS.charAt(0));
    if (numOpen > numClose) {
        throw new SQLException("Missing closing parenthesis in '" + parseResult.unparsedRest + "'.");
    }

    processColumnData(parseResult.unparsedRest.substring(1, parseResult.unparsedRest.length() - 1),
            tableMetaData);

    return tableMetaData;
}

From source file:com.taobao.adfs.database.tdhsocket.client.util.ConvertUtil.java

public static int getIntFromString(String stringVal) throws SQLException {
    if (StringUtils.isBlank(stringVal)) {
        return 0;
    }//from  w  ww.  java2  s  .  c  om
    try {
        int decimalIndex = stringVal.indexOf(".");

        if (decimalIndex != -1) {
            double valueAsDouble = Double.parseDouble(stringVal);
            return (int) valueAsDouble;
        }

        return Integer.parseInt(stringVal);
    } catch (NumberFormatException e) {
        throw new SQLException("Parse integer error:" + stringVal);
    }
}

From source file:com.ts.db.connector.ConnectorDriverManager.java

static Driver getDriver(String url, String driverClassName, String classpath) throws SQLException {
    assert !StringUtils.isBlank(url);
    final boolean hasClasspath = !StringUtils.isBlank(classpath);
    if (!hasClasspath) {
        for (Driver driver : new ArrayList<Driver>(drivers)) {
            if (driver.acceptsURL(url)) {
                return driver;
            }//from w  w w  .  j av  a2 s .c o m
        }
    }
    List<File> jars = new ArrayList<File>();
    ClassLoader cl;
    if (hasClasspath) {
        List<URL> urls = new ArrayList<URL>();
        for (String path : classpath.split(pathSeparator)) {
            final File file = new File(path);
            if (isJarFile(file)) {
                jars.add(file);
            }
            try {
                urls.add(file.toURI().toURL());
            } catch (MalformedURLException ex) {
                log.warn(ex.toString());
            }
        }
        cl = new URLClassLoader(urls.toArray(new URL[urls.size()]));
    } else {
        jars.addAll(getJarFiles("."));
        jars.addAll(driverFiles);
        List<URL> urls = new ArrayList<URL>();
        for (File file : jars) {
            try {
                urls.add(file.toURI().toURL());
            } catch (MalformedURLException ex) {
                log.warn(ex.toString());
            }
        }
        cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), ClassLoader.getSystemClassLoader());
    }
    driverFiles.addAll(jars);
    final boolean hasDriverClassName = !StringUtils.isBlank(driverClassName);
    if (hasDriverClassName) {
        try {
            Driver driver = DynamicLoader.newInstance(driverClassName, cl);
            assert driver != null;
            return driver;
        } catch (DynamicLoadingException ex) {
            Throwable cause = (ex.getCause() != ex) ? ex.getCause() : ex;
            SQLException exception = new SQLException(cause.toString());
            exception.initCause(cause);
            throw exception;
        }
    }
    final String jdbcDrivers = System.getProperty("jdbc.drivers");
    if (!StringUtils.isBlank(jdbcDrivers)) {
        for (String jdbcDriver : jdbcDrivers.split(":")) {
            try {
                Driver driver = DynamicLoader.newInstance(jdbcDriver, cl);
                if (driver != null) {
                    if (!hasClasspath) {
                        drivers.add(driver);
                    }
                    return driver;
                }
            } catch (DynamicLoadingException ex) {
                log.warn(ex.toString());
            }
        }
    }
    for (File jar : jars) {
        try {
            Driver driver = getDriver(jar, url, cl);
            if (driver != null) {
                if (!hasClasspath) {
                    drivers.add(driver);
                }
                return driver;
            }
        } catch (IOException ex) {
            log.warn(ex.toString());
        }
    }
    for (String path : System.getProperty("java.class.path", "").split(pathSeparator)) {
        if (isJarFile(path)) {
            Driver driver;
            try {
                driver = getDriver(new File(path), url, cl);
                if (driver != null) {
                    drivers.add(driver);
                    return driver;
                }
            } catch (IOException ex) {
                log.warn(ex.toString());
            }
        }
    }
    throw new SQLException("driver not found");
}

From source file:com.cws.esolutions.security.dao.reference.impl.SecurityReferenceDAOImpl.java

/**
 * @see com.cws.esolutions.security.dao.reference.interfaces.ISecurityReferenceDAO#obtainApprovedServers()
 *///from ww w.  java2s.  co  m
public synchronized List<String> obtainApprovedServers() throws SQLException {
    final String methodName = ISecurityReferenceDAO.CNAME + "#obtainApprovedServers() throws SQLException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
    }

    Connection sqlConn = null;
    ResultSet resultSet = null;
    CallableStatement stmt = null;
    List<String> securityList = null;

    try {
        sqlConn = dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);
        stmt = sqlConn.prepareCall("{CALL retrApprovedServers()}");

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        if (stmt.execute()) {
            resultSet = stmt.getResultSet();

            if (DEBUG) {
                DEBUGGER.debug("ResultSet: {}", resultSet);
            }

            if (resultSet.next()) {
                resultSet.beforeFirst();

                securityList = new ArrayList<String>();

                while (resultSet.next()) {
                    if (DEBUG) {
                        DEBUGGER.debug(resultSet.getString(1));
                    }

                    // check if column is null
                    securityList.add(resultSet.getString(1));
                }

                if (DEBUG) {
                    DEBUGGER.debug("securityList: {}", securityList);
                }
            }
        }
    } catch (SQLException sqx) {
        throw new SQLException(sqx.getMessage(), sqx);
    } finally {
        if (resultSet != null) {
            resultSet.close();
        }

        if (stmt != null) {
            stmt.close();
        }

        if ((sqlConn != null) && (!(sqlConn.isClosed()))) {
            sqlConn.close();
        }
    }

    return securityList;
}

From source file:gridool.db.partitioning.monetdb.MonetDBInvokeCopyIntoOperation.java

@Override
public Serializable execute() throws SQLException {
    final Connection conn;
    try {/* w w  w  . j a  v a  2  s  .co  m*/
        conn = getConnection();
    } catch (ClassNotFoundException e) {
        LOG.error(e);
        throw new SQLException(e.getMessage());
    }

    final File loadFile = prepareLoadFile(tableName);
    final String query = complementCopyIntoQuery(copyIntoQuery, loadFile);
    try {
        JDBCUtils.update(conn, query);
        conn.commit();
    } catch (SQLException e) {
        LOG.error("rollback a transaction", e);
        conn.rollback();
        throw e;
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            LOG.debug(e);
        }
        if (!loadFile.delete()) {
            LOG.warn("Could not remove a tempolary file: " + loadFile.getAbsolutePath());
        }
    }

    return Boolean.TRUE;
}