Java Code Examples for java.sql.SQLException.toString()

The following are Jave code examples for showing how to use toString() of the java.sql.SQLException class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: uavstack   File: JdbcDriverAdapter.java   View Source Code Vote up 7 votes
/**
 * 解析resultset中的条数
 * 
 * @param resultSet
 * @return
 */
private String sqlResultSetParse(ResultSet rset) {

    try {
        // 只能向前遍历的游标,只能用next()来遍历
        if (rset.getType() == ResultSet.TYPE_FORWARD_ONLY) {
            return "FORWARD_ONLY";
        }
        else {
            // 可以滚动的游标,在用户使用游标之前,直接获取结果集大小,然后游标回到开始的地方(假装游标没有移动过)
            rset.last();
            int row = rset.getRow();
            rset.beforeFirst();
            return row + "";

        }
    }
    catch (SQLException e) {
        return e.toString();
    }
}
 
Example 2
Project: OpenDiabetes   File: JDBCXAResource.java   View Source Code Vote up 6 votes
/**
 * This rolls back the connection associated with <i>this</i> XAResource.
 *
 * @throws javax.transaction.xa.XAException generically, since the more
 * specific exceptions require a JTA API to compile.
 */
/* @throws javax.transaction.HeuristicCommitException
 *         if work was committed.
 * @throws javax.transaction.HeuristicMixedException
 *         if some work was committed and some work was rolled back
 */
public void rollbackThis() throws XAException {

    if (state != XA_STATE_PREPARED && state != XA_STATE_ENDED) {
        throw new XAException("Invalid XAResource state");
    }

    try {

        /**
         * @todo:  Determine if work was committed, rolled back, or both,
         * and return appropriate Heuristic Exception.
         */
        connection.rollback();    // real/phys.
    } catch (SQLException se) {
        throw new XAException(se.toString());
    }

    dispose();
}
 
Example 3
Project: lams   File: BlobFromLocator.java   View Source Code Vote up 6 votes
@Override
public int read(byte[] b) throws IOException {
    if (this.currentPositionInBlob + 1 > this.length) {
        return -1;
    }

    try {
        byte[] asBytes = getBytesInternal(this.pStmt, (this.currentPositionInBlob) + 1, b.length);

        if (asBytes == null) {
            return -1;
        }

        System.arraycopy(asBytes, 0, b, 0, asBytes.length);

        this.currentPositionInBlob += asBytes.length;

        return asBytes.length;
    } catch (SQLException sqlEx) {
        throw new IOException(sqlEx.toString());
    }
}
 
Example 4
Project: OpenDiabetes   File: TransferDb.java   View Source Code Vote up 6 votes
TransferResultSet getData(String statement)
throws DataAccessPointException {

    ResultSet rsData = null;

    try {
        if (srcStatement != null) {
            srcStatement.close();
        }

        srcStatement = conn.createStatement();
        rsData       = srcStatement.executeQuery(statement);
    } catch (SQLException e) {
        try {
            srcStatement.close();
        } catch (Exception e1) {}

        srcStatement = null;
        rsData       = null;

        throw new DataAccessPointException(e.toString());
    }

    return new TransferResultSet(rsData);
}
 
Example 5
Project: burstcoin   File: ValuesDbTable.java   View Source Code Vote up 6 votes
public final void insert(T t, List<V> values) {
    if (!Db.isInTransaction()) {
        throw new IllegalStateException("Not in transaction");
    }
    DbKey dbKey = dbKeyFactory.newKey(t);
    Db.getCache(table).put(dbKey, values);
    try (Connection con = Db.getConnection()) {
        if (multiversion) {
            try (PreparedStatement pstmt = con.prepareStatement("UPDATE " + table
                    + " SET latest = FALSE " + dbKeyFactory.getPKClause() + " AND latest = TRUE")) {
                dbKey.setPK(pstmt);
                pstmt.executeUpdate();
            }
        }
        for (V v : values) {
            save(con, t, v);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e.toString(), e);
    }
}
 
Example 6
Project: OpenDiabetes   File: TestUtil.java   View Source Code Vote up 6 votes
protected boolean test(Statement aStatement) {

        try {
            aStatement.execute(getSql());
        } catch (SQLException sqlX) {
            caught = sqlX;

            if (expectedState == null
                    || expectedState.equalsIgnoreCase(sqlX.getSQLState())) {
                return true;
            }

            message = "SQLState '" + sqlX.getSQLState() + "' : "
                      + sqlX.toString() + " instead of '" + expectedState
                      + "'";
        } catch (Exception x) {
            caught  = x;
            message = x.toString();
        }

        return false;
    }
 
Example 7
Project: burstcoin   File: BlockchainImpl.java   View Source Code Vote up 6 votes
@Override
public List<Long> getBlockIdsAfter(long blockId, int limit) {
    if (limit > 1440) {
        throw new IllegalArgumentException("Can't get more than 1440 blocks at a time");
    }
    try (Connection con = Db.getConnection();
         PreparedStatement pstmt = con.prepareStatement("SELECT id FROM block WHERE db_id > (SELECT db_id FROM block WHERE id = ?) ORDER BY db_id ASC LIMIT ?")) {
        List<Long> result = new ArrayList<>();
        pstmt.setLong(1, blockId);
        pstmt.setInt(2, limit);
        try (ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                result.add(rs.getLong("id"));
            }
        }
        return result;
    } catch (SQLException e) {
        throw new RuntimeException(e.toString(), e);
    }
}
 
Example 8
Project: the-vigilantes   File: BlobFromLocator.java   View Source Code Vote up 6 votes
@Override
public int read(byte[] b, int off, int len) throws IOException {
    if (this.currentPositionInBlob + 1 > this.length) {
        return -1;
    }

    try {
        byte[] asBytes = getBytesInternal(this.pStmt, (this.currentPositionInBlob) + 1, len);

        if (asBytes == null) {
            return -1;
        }

        System.arraycopy(asBytes, 0, b, off, asBytes.length);

        this.currentPositionInBlob += asBytes.length;

        return asBytes.length;
    } catch (SQLException sqlEx) {
        throw new IOException(sqlEx.toString());
    }
}
 
Example 9
Project: burstcoin   File: AT_API_Platform_Impl.java   View Source Code Vote up 6 votes
protected static Long findTransaction(int startHeight , int endHeight , Long atID, int numOfTx, long minAmount){
	try (Connection con = Db.getConnection();
			PreparedStatement pstmt = con.prepareStatement("SELECT id FROM transaction "
					+ "WHERE height>= ? AND height < ? and recipient_id = ? AND amount >= ? "
					+ "ORDER BY height, id "
					+ "LIMIT 1 OFFSET ?")){
		pstmt.setInt(1, startHeight);
		pstmt.setInt(2, endHeight);
		pstmt.setLong(3, atID);
		pstmt.setLong(4, minAmount);
		pstmt.setInt(5, numOfTx);
		ResultSet rs = pstmt.executeQuery();
		Long transactionId = 0L;
		if(rs.next()) {
			transactionId = rs.getLong("id");
		}
		rs.close();
		return transactionId;

	} catch (SQLException e) {
		throw new RuntimeException(e.toString(), e);
	}

}
 
Example 10
Project: burstcoin   File: ValuesDbTable.java   View Source Code Vote up 6 votes
public final List<V> get(DbKey dbKey) {
    List<V> values;
    if (Db.isInTransaction()) {
        values = (List<V>)Db.getCache(table).get(dbKey);
        if (values != null) {
            return values;
        }
    }
    try (Connection con = Db.getConnection();
         PreparedStatement pstmt = con.prepareStatement("SELECT * FROM " + table + dbKeyFactory.getPKClause()
         + (multiversion ? " AND latest = TRUE" : "") + " ORDER BY db_id DESC")) {
        dbKey.setPK(pstmt);
        values = get(con, pstmt);
        if (Db.isInTransaction()) {
            Db.getCache(table).put(dbKey, values);
        }
        return values;
    } catch (SQLException e) {
        throw new RuntimeException(e.toString(), e);
    }
}
 
Example 11
Project: dev-courses   File: TransferDb.java   View Source Code Vote up 5 votes
TransferDb(Connection c, Traceable t) throws DataAccessPointException {

        super(t);

        conn = c;

        if (c != null) {
            String productLowerName;

            try {
                meta              = c.getMetaData();
                databaseToConvert = c.getCatalog();
                productLowerName  = meta.getDatabaseProductName();

                if (productLowerName == null) {
                    productLowerName = "";
                } else {
                    productLowerName = productLowerName.toLowerCase();
                }

                helper = HelperFactory.getHelper(productLowerName);

                helper.set(this, t, meta.getIdentifierQuoteString());
            } catch (SQLException e) {
                throw new DataAccessPointException(e.toString());
            }
        }
    }
 
Example 12
Project: burstcoin   File: DbIterator.java   View Source Code Vote up 5 votes
public DbIterator(Connection con, PreparedStatement pstmt, ResultSetReader<T> rsReader) {
    this.con = con;
    this.pstmt = pstmt;
    this.rsReader = rsReader;
    try {
        this.rs = pstmt.executeQuery();
        this.hasNext = rs.next();
    } catch (SQLException e) {
        DbUtils.close(pstmt, con);
        throw new RuntimeException(e.toString(), e);
    }
}
 
Example 13
Project: burstcoin   File: BlockchainImpl.java   View Source Code Vote up 5 votes
@Override
public int getTransactionCount() {
    try (Connection con = Db.getConnection(); PreparedStatement pstmt = con.prepareStatement("SELECT COUNT(*) FROM transaction");
         ResultSet rs = pstmt.executeQuery()) {
        rs.next();
        return rs.getInt(1);
    } catch (SQLException e) {
        throw new RuntimeException(e.toString(), e);
    }
}
 
Example 14
Project: burstcoin   File: Db.java   View Source Code Vote up 5 votes
public static void rollbackTransaction() {
    DbConnection con = localConnection.get();
    if (con == null) {
        throw new IllegalStateException("Not in transaction");
    }
    try {
        con.doRollback();
    } catch (SQLException e) {
        throw new RuntimeException(e.toString(), e);
    }
    transactionCaches.get().clear();
    transactionBatches.get().clear();
}
 
Example 15
Project: burstcoin   File: ValuesDbTable.java   View Source Code Vote up 5 votes
private List<V> get(Connection con, PreparedStatement pstmt) {
    try {
        List<V> result = new ArrayList<>();
        try (ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                result.add(load(con, rs));
            }
        }
        return result;
    } catch (SQLException e) {
        throw new RuntimeException(e.toString(), e);
    }
}
 
Example 16
Project: OpenDiabetes   File: TransferDb.java   View Source Code Vote up 5 votes
void setAutoCommit(boolean flag) throws DataAccessPointException {

        try {
            conn.setAutoCommit(flag);
        } catch (SQLException e) {
            throw new DataAccessPointException(e.toString());
        }
    }
 
Example 17
Project: burstcoin   File: PeerDb.java   View Source Code Vote up 5 votes
static void addPeers(Collection<String> peers) {
    try (Connection con = Db.getConnection();
         PreparedStatement pstmt = con.prepareStatement("INSERT INTO peer (address) values (?)")) {
        for (String peer : peers) {
            pstmt.setString(1, peer);
            pstmt.executeUpdate();
        }
    } catch (SQLException e) {
        throw new RuntimeException(e.toString(), e);
    }
}
 
Example 18
Project: BibliotecaPS   File: BaseBugReport.java   View Source Code Vote up 5 votes
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 19
Project: burstcoin   File: Order.java   View Source Code Vote up 5 votes
private static Ask getNextOrder(long assetId) {
    try (Connection con = Db.getConnection();
         PreparedStatement pstmt = con.prepareStatement("SELECT * FROM ask_order WHERE asset_id = ? "
                 + "AND latest = TRUE ORDER BY price ASC, creation_height ASC, id ASC LIMIT 1")) {
        pstmt.setLong(1, assetId);
        try (DbIterator<Ask> askOrders = askOrderTable.getManyBy(con, pstmt, true)) {
            return askOrders.hasNext() ? askOrders.next() : null;
        }
    } catch (SQLException e) {
        throw new RuntimeException(e.toString(), e);
    }
}
 
Example 20
Project: OpenVertretung   File: BlobFromLocator.java   View Source Code Vote up 5 votes
@Override
public void close() throws IOException {
    if (this.pStmt != null) {
        try {
            this.pStmt.close();
        } catch (SQLException sqlEx) {
            throw new IOException(sqlEx.toString());
        }
    }

    super.close();
}