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:de.iew.stagediver.fx.database.hibernate.HBConnectionProvider.java

@Override
public Connection getConnection() throws SQLException {
    try {/*w  w  w. j  av  a  2s  .  c  o  m*/
        final ConnectionService connectionService = LOOKUP.lookup(ConnectionService.class);
        return connectionService.checkoutBuildInDatabaseConnection(this.database, this.username, this.password);
    } catch (ConnectException e) {
        throw new SQLException(e);
    }
}

From source file:com.taobao.tddl.jdbc.atom.jdbc.TStatementWrapper.java

protected void recordWriteTimes() throws SQLException {
    AtomDbStatusEnum status = datasourceWrapper.connectionProperties.dbStatus;
    if (status != AtomDbStatusEnum.W_STATUS && status != AtomDbStatusEnum.RW_STATUS) {
        throw new SQLException("db do not allow to execute write ! dbStatus is " + status);
    }//from w  w w .  java 2 s .com
    /*
    int writeRestrictionTimes = datasourceWrapper.connectionProperties.writeRestrictionTimes;
    int currentWriteTimes = datasourceWrapper.writeTimes.incrementAndGet();
    if (writeRestrictionTimes != 0) {
       if (currentWriteTimes > writeRestrictionTimes) {
    datasourceWrapper.writeTimesReject.incrementAndGet();
    throw new SQLException("max write times , " + currentWriteTimes);
       }
    }
    */
    if (!datasourceWrapper.writeFlowControl.allow()) {
        throw new SQLException(datasourceWrapper.writeFlowControl.reportExceed());
    }
}

From source file:com.cloudant.sync.datastore.AttachmentManager.java

public void addAttachment(PreparedAttachment a, DocumentRevision rev) throws IOException, SQLException {

    // do it this way to only go thru inputstream once
    // * write to temp location using copyinputstreamtofile
    // * get sha1
    // * stick it into database
    // * move file using sha1 as name

    ContentValues values = new ContentValues();
    long sequence = rev.getSequence();
    String filename = a.attachment.name;
    byte[] sha1 = a.sha1;
    String type = a.attachment.type;
    int encoding = a.attachment.encoding.ordinal();
    long length = a.tempFile.length();
    long revpos = CouchUtils.generationFromRevId(rev.getRevision());

    values.put("sequence", sequence);
    values.put("filename", filename);
    values.put("key", sha1);
    values.put("type", type);
    values.put("encoding", encoding);
    values.put("length", length);
    values.put("encoded_length", length);
    values.put("revpos", revpos);

    // delete and insert in case there is already an attachment at this seq (eg copied over from a previous rev)
    datastore.getSQLDatabase().delete("attachments", " filename = ? and sequence = ? ",
            new String[] { filename, String.valueOf(sequence) });
    long result = datastore.getSQLDatabase().insert("attachments", values);
    if (result == -1) {
        // if we can't insert into DB then don't copy the attachment
        a.tempFile.delete();/*w  ww.  j  a  v a 2 s  .  co  m*/
        throw new SQLException("Could not insert attachment " + a + " into database with values " + values
                + "; not copying to attachments directory");
    }
    // move file to blob store, with file name based on sha1
    File newFile = fileFromKey(sha1);
    try {
        FileUtils.moveFile(a.tempFile, newFile);
    } catch (FileExistsException fee) {
        // File with same SHA1 hash in the store, we assume it's the same content so can discard
        // the duplicate data we have just downloaded
        a.tempFile.delete();
    }
}

From source file:com.concursive.connect.web.modules.badges.dao.BadgeLogoFile.java

public boolean insert(Connection db) throws SQLException {
    boolean result = false;
    // The required linkModuleId
    linkModuleId = Constants.BADGE_FILES;
    // Determine if the database is in auto-commit mode
    boolean doCommit = false;
    try {//from   w ww .j  av  a  2  s .  c om
        if (doCommit = db.getAutoCommit()) {
            db.setAutoCommit(false);
        }
        // Insert the record
        result = super.insert(db);
        // Update the referenced pointer
        if (result) {
            int i = 0;
            PreparedStatement pst = db
                    .prepareStatement("UPDATE badge " + "SET logo_id = ? " + "WHERE badge_id = ? ");
            pst.setInt(++i, id);
            pst.setInt(++i, linkItemId);
            int count = pst.executeUpdate();
            result = (count == 1);
        }
        if (doCommit) {
            db.commit();
        }
    } catch (Exception e) {
        if (doCommit) {
            db.rollback();
        }
        throw new SQLException(e.getMessage());
    } finally {
        if (doCommit) {
            db.setAutoCommit(true);
        }
    }
    return result;
}

From source file:net.mlw.vlh.adapter.jdbc.util.ResultSetMapGenerator.java

/**
 * <p>Loads and returns the <code>Class</code> of the given name.
 * By default, a load from the thread context class loader is attempted.
 * If there is no such class loader, the class loader used to load this
 * class will be utilized.</p>//from   w  ww  .ja va 2  s. co  m
 *
 * @exception SQLException if an exception was thrown trying to load
 *  the specified class
 */
protected Class loadClass(String className) throws SQLException {
    try {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        if (cl == null) {
            cl = this.getClass().getClassLoader();
        }
        return (cl.loadClass(className));
    } catch (Exception e) {
        throw new SQLException("Cannot load column class '" + className + "': " + e);
    }
}

From source file:com.softberries.klerk.dao.AddressDao.java

public void create(Address c, QueryRunner run, Connection conn, ResultSet generatedKeys) throws SQLException {
    PreparedStatement st = conn.prepareStatement(SQL_INSERT_ADDRESS, Statement.RETURN_GENERATED_KEYS);
    st.setString(1, c.getCountry());//from w  w  w  .  ja  v  a2s .  c  o  m
    st.setString(2, c.getCity());
    st.setString(3, c.getStreet());
    st.setString(4, c.getPostCode());
    st.setString(5, c.getHouseNumber());
    st.setString(6, c.getFlatNumber());
    st.setString(7, c.getNotes());
    st.setBoolean(8, c.isMain());
    if (c.getPerson_id().longValue() == 0 && c.getCompany_id().longValue() == 0) {
        throw new SQLException("For Address either Person or Company needs to be specified");
    }
    if (c.getPerson_id().longValue() != 0) {
        st.setLong(9, c.getPerson_id());
    } else {
        st.setNull(9, java.sql.Types.NUMERIC);
    }
    if (c.getCompany_id().longValue() != 0) {
        st.setLong(10, c.getCompany_id());
    } else {
        st.setNull(10, java.sql.Types.NUMERIC);
    }
    // run the query
    int i = st.executeUpdate();
    System.out.println("i: " + i);
    if (i == -1) {
        System.out.println("db error : " + SQL_INSERT_ADDRESS);
    }
    generatedKeys = st.getGeneratedKeys();
    if (generatedKeys.next()) {
        c.setId(generatedKeys.getLong(1));
    } else {
        throw new SQLException("Creating address failed, no generated key obtained.");
    }
}

From source file:org.sakaiproject.orm.ibatis.support.AbstractLobTypeHandler.java

/**
 * This implementation delegates to setParameterInternal,
 * passing in a transaction-synchronized LobCreator for the
 * LobHandler of this type./*from   ww  w .java 2  s .  c o m*/
 * @see #setParameterInternal
 */
public final void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType)
        throws SQLException {

    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        throw new IllegalStateException("Spring transaction synchronization needs to be active for "
                + "setting values in iBATIS TypeHandlers that delegate to a Spring LobHandler");
    }
    final LobCreator lobCreator = this.lobHandler.getLobCreator();
    try {
        setParameterInternal(ps, i, parameter, jdbcType, lobCreator);
    } catch (IOException ex) {
        throw new SQLException("I/O errors during LOB access: " + ex.getMessage());
    }

    TransactionSynchronizationManager.registerSynchronization(new LobCreatorSynchronization(lobCreator));
}

From source file:gridool.mapred.db.DBReduceJob.java

public static void createView(final String dstDbUrl, final String createTables, final String createView,
        final DBMapReduceJobConf jobConf) throws SQLException {
    final Connection conn;
    try {//w  w w  .j a  v a  2s.  c om
        conn = jobConf.getConnection(dstDbUrl, true);
    } catch (ClassNotFoundException e) {
        throw new SQLException(e);
    }
    try {
        Statement st = conn.createStatement();
        st.executeUpdate(createTables);
        st.executeUpdate(createView);
        st.close();
        conn.commit();
        if (LOG.isInfoEnabled()) {
            LOG.info(createTables);
            LOG.info(createView);
        }
    } catch (SQLException sqle) {
        conn.rollback();
        throw sqle;
    } finally {
        conn.close();
    }
}

From source file:com.dynamobi.ws.util.ConnectionManager.java

/**
 * Used by the caller to get their Connection resource.
 * This will block if the connection is not available yet.
 * @param auth - Spring authentication for the caller.
 * @return Either a new SQL Connection or one previously created.
 *//*  w ww.j a v  a 2  s.c  om*/
public static Connection request_connection(Authentication auth)
        throws SQLException, ClassNotFoundException, InterruptedException {
    String uname = auth.getName();
    String pw = null;
    String[] parts = auth.getCredentials().toString().split(":", 2);
    // uuid, optionally password
    boolean first;
    if (parts.length == 2) { // a first call, possibly
        first = true;
        pw = parts[1];
    } else if (parts.length == 1) { // subsequent calls
        first = false;
    } else { // wtf? (They shouldn't have gotten this far, something is wrong.)
        throw new SQLException("SEVERE: Server received unexpected invalid " + "authentication token.");
    }

    String uuid = parts[0];
    System.out.println(uuid);
    if (conns.containsKey(uuid) && !first) {
        ConnectionInfo info = conns.get(uuid);
        if (info.uname.equals(uname)) {
            while (info.get_busy()) {
                Thread.sleep(1000);
            }
            if (info.closed) { // they may have timed out and come back, recreate c
                info.set_connection(stupid_connection(info.uname, info.pw));
            }
            info.update_time();

            info.busy = true;
            return info.connection;
        } else {
            throw new SQLException("SEVERE: UUID collision among different users.");
        }
    } else if (conns.containsKey(uuid) && first) {
        // enforce them not to send the password twice.
        throw new SQLException("Please do not continue to send your password " + "raw after authenticating.");
    } else if (first) { // make new connection for them.
        Connection c = stupid_connection(uname, pw);
        ConnectionInfo info = new ConnectionInfo();
        conns.put(uuid, info);

        info.uname = uname;
        info.pw = pw;
        info.set_connection(c);
        info.busy = true;
        return c;
    } else { // !first, no known, we need a raw pass from them to make a conn.
        throw new SQLException(
                "Your raw password is required for the initial " + "request to form a connection.");
    }
}

From source file:com.taobao.tdhs.jdbc.TDHSPreparedStatement.java

public void setDString(int parameterIndex, @Nullable String x) throws SQLException {
    checkclose();/*w  w w  .ja  va  2 s.  c o m*/
    if (parameterIndex < 1 || parameterIndex > this.parameterNumber) {
        throw new SQLException("parameterIndex is out of range,parameterIndex is " + parameterIndex);
    }
    parameters.put(parameterIndex, x);
}