Java Code Examples for java.sql.SQLException

The following are top voted examples for showing how to use java.sql.SQLException. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: sslcertx   File: MySQLPreparedStatement.java View source code Vote up 7 votes
@Override
public int[] executeBatch() throws SQLException {
    int[] ret = new int[batchPreparedStatements.size()];
    int i = 0;
    try {
        synchronized (this.getProtocol()) {
            for(; i < batchPreparedStatements.size(); i++)  {
                PreparedStatement ps =  batchPreparedStatements.get(i);
                ps.execute();
                int updateCount = ps.getUpdateCount();
                if (updateCount == -1) {
                    ret[i] = SUCCESS_NO_INFO;
                } else {
                    ret[i] = updateCount;
                }
            }
        }
    } catch (SQLException sqle) {
        throw new BatchUpdateException(sqle.getMessage(), sqle.getSQLState(), Arrays.copyOf(ret, i), sqle);
    } finally {
        clearBatch();
    }
    return ret;
}
 
Example 2
Project: flow-android   File: UserFriendsLoader.java View source code Vote up 6 votes
@Override
protected UserFriends loadDelegate() {
    // we first check if we should load from database or from the network
    if (mBaseFragment != null) {
        final ProfileFragment profileFragment = (ProfileFragment) mBaseFragment;
        if (profileFragment != null && profileFragment.getProfileID() != null) {
            // TODO add this call when we decide to add friends to user friend profile
            //return null;
        }
    }

    List<User> userFriends = new ArrayList<User>();
    try {
        Dao<User, String> userDao = flowDatabaseHelper.getUserDao();
        QueryBuilder<User, String> queryBuilder =
                userDao.queryBuilder();
        userFriends = userDao.query(queryBuilder.where().eq(User.IS_ME, false).prepare());
    } catch (SQLException e) {
        e.printStackTrace();
        Crashlytics.logException(e);
    }
    UserFriends users = new UserFriends();
    users.setFriends(new ArrayList<User>(userFriends));
    return users;
}
 
Example 3
Project: sslcertx   File: MySQLXAResource.java View source code Vote up 6 votes
XAException mapXAException(SQLException sqle) {
    int XAErrorCode;

    switch(sqle.getErrorCode()) {
        case 1397:
            XAErrorCode = XAException.XAER_NOTA;
            break;
        case 1398:
            XAErrorCode = XAException.XAER_INVAL;
            break;
        case 1399:
            XAErrorCode = XAException.XAER_RMFAIL;
            break;
        case 1400:
            XAErrorCode = XAException.XAER_OUTSIDE;
            break;
        case 1401:
            XAErrorCode = XAException.XAER_RMERR;
            break;
        case 1402:
            XAErrorCode = XAException.XA_RBROLLBACK;
            break;
        default:
            XAErrorCode = 0;
    }
    if (XAErrorCode != 0)
        return new XAException(XAErrorCode);
    else
        return new XAException(sqle.getMessage());
}
 
Example 4
Project: aws-toolkit-eclipse   File: JdbcStatement.java View source code Vote up 6 votes
SQLException wrapIntoSqlException(final Exception e) {
    SQLException ex;
    if (e instanceof SQLException) {
        ex = (SQLException) e;
    } else {
        ex = new SQLException(e.getLocalizedMessage());
        ex.initCause(e);
    }
    return ex;
}
 
Example 5
Project: jena   File: SDBConnectionFactory.java View source code Vote up 6 votes
public static Connection createSqlConnection(String jdbcURL, String user, String password)
{
    try {
        return JDBC.createConnection(jdbcURL, user, password) ;
    } catch (SQLException e)
    {
        SQLException e2 = e.getNextException() ;
        String more = "" ;
        if ( e2 != null )
            more = " : "+e2.getMessage() ;
        throw new SDBException("SQL Exception while connecting to database: "+jdbcURL+" : "+e.getMessage()+more) ;
    }
}
 
Example 6
Project: NemoVelo   File: JDBC4Connection.java View source code Vote up 6 votes
protected JDBC4ClientInfoProvider getClientInfoProviderImpl() throws SQLException {
	synchronized (getConnectionMutex()) {
		if (this.infoProvider == null) {
			try {
				try {
					this.infoProvider = (JDBC4ClientInfoProvider)Util.getInstance(getClientInfoProvider(), 
							new Class[0], new Object[0], getExceptionInterceptor());
				} catch (SQLException sqlEx) {
					if (sqlEx.getCause() instanceof ClassCastException) {
						// try with package name prepended
						this.infoProvider = (JDBC4ClientInfoProvider)Util.getInstance(
								"com.mysql.jdbc." + getClientInfoProvider(), 
								new Class[0], new Object[0], getExceptionInterceptor());
					}
				}
			} catch (ClassCastException cce) {
				throw SQLError.createSQLException(Messages
						.getString("JDBC4Connection.ClientInfoNotImplemented", new Object[] {getClientInfoProvider()}), 
						SQLError.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
			}
			
			this.infoProvider.initialize(this, this.props);
		}
		
		return this.infoProvider;
	}
}
 
Example 7
Project: SharkFW   File: SQLSTSet.java View source code Vote up 6 votes
/**
 * Creates iterator of tags of given type. Note: Semantic Tags cover also
 * time, spatial and peer semantic tags
 * @param type
 * @return
 * @throws SharkKBException 
 */
protected Iterator tags(int type) throws SharkKBException {
    List<SemanticTag> tagList;
    Statement statement = null;

    try {
        statement  = this.kb.getConnection().createStatement();
        
        String sqlString = "SELECT id, st_type FROM " + SQLSharkKB.ST_TABLE;
        
        // select specific tag type if not of general semantic tag type
        switch(type) {
            case SQLSharkKB.PEER_SEMANTIC_TAG_TYPE:
            case SQLSharkKB.SPATIAL_SEMANTIC_TAG_TYPE:
            case SQLSharkKB.TIME_SEMANTIC_TAG_TYPE:
                sqlString += " WHERE st_type = " + type;
                break;
        }
        
        ResultSet result = statement.executeQuery(sqlString);
        
        tagList = SQLSharkKB.createSTListBySTTableEntries(this.kb, result);
        
    }
    catch(SQLException e) {
        throw new SharkKBException(e.getLocalizedMessage());
    }
    finally {
        if(statement != null) {
            try {
                statement.close();
            } catch (SQLException ex) {
                // ignore
            }
        }
    }
    
    return tagList.iterator();
}
 
Example 8
Project: RSQLServer   File: MSCursorResultSet.java View source code Vote up 6 votes
/**
 * Close a server side cursor.
 *
 * @throws SQLException
 */
private void cursorClose() throws SQLException {
    TdsCore tds = statement.getTds();

    statement.clearWarnings();

    // Consume rest of output and remember any exceptions
    tds.clearResponseQueue();
    SQLException ex = statement.getMessages().exceptions;

    ParamInfo param[] = new ParamInfo[1];

    // Setup cursor handle param
    param[0] = PARAM_CURSOR_HANDLE;

    tds.executeSQL(null, "sp_cursorclose", param, false,
            statement.getQueryTimeout(), -1, -1, true);
    tds.clearResponseQueue();
    
    if (ex != null) {
        ex.setNextException(statement.getMessages().exceptions);
        throw ex;
    } else {
        statement.getMessages().checkErrors();
    }
}
 
Example 9
Project: RSQLServer   File: TdsData.java View source code Vote up 5 votes
/**
     * Read a MSQL 2000 sql_variant data value from the input stream.
     * <p>SQL_VARIANT has the following structure:
     * <ol>
     * <li>INT4 total size of data
     * <li>INT1 TDS data type (text/image/ntext/sql_variant not allowed)
     * <li>INT1 Length of extra type descriptor information
     * <li>Optional additional type info required by some types
     * <li>byte[0...n] the actual data
     * </ol>
     *
     * @param connection used to obtain collation/charset information
     * @param in         the server response stream
     * @return the SQL_VARIANT data
     */
    private static Object getVariant(ConnectionJDBC2 connection,
                                     ResponseStream in)
            throws IOException, ProtocolException {
        byte[] bytes;
        int len = in.readInt();

        if (len == 0) {
            // Length of zero means item is null
            return null;
        }

        ColInfo ci = new ColInfo();
        len -= 2;
        ci.tdsType = in.read(); // TDS Type
        len -= in.read(); // Size of descriptor

        switch (ci.tdsType) {
            case SYBINT1:
                return new Integer(in.read() & 0xFF);

            case SYBINT2:
                return new Integer(in.readShort());

            case SYBINT4:
                return new Integer(in.readInt());

            case SYBINT8:
                return new Long(in.readLong());

            case XSYBCHAR:
            case XSYBVARCHAR:
                // FIXME Use collation for reading
                getCollation(in, ci);
                try {
                    setColumnCharset(ci, connection);
                } catch (SQLException ex) {
                    // Skip the buffer size and value
                    in.skip(2 + len);
                    throw new ProtocolException(ex.toString() + " [SQLState: "
                            + ex.getSQLState() + ']');
                }

                in.skip(2); // Skip buffer size
                return in.readNonUnicodeString(len);

            case XSYBNCHAR:
            case XSYBNVARCHAR:
                // XXX Why do we need collation for Unicode strings?
                in.skip(7); // Skip collation and buffer size

                return in.readUnicodeString(len / 2);

            case XSYBVARBINARY:
            case XSYBBINARY:
                in.skip(2); // Skip buffer size
                bytes = new byte[len];
                in.read(bytes);

                return bytes;

            case SYBMONEY4:
            case SYBMONEY:
                return getMoneyValue(in, ci.tdsType);

            case SYBDATETIME4:
            case SYBDATETIME:
                return getDatetimeValue(in, ci.tdsType);

            case SYBBIT:
                return (in.read() != 0) ? Boolean.TRUE : Boolean.FALSE;

            case SYBREAL:
                return new Float(Float.intBitsToFloat(in.readInt()));

            case SYBFLT8:
                return new Double(Double.longBitsToDouble(in.readLong()));

            case SYBUNIQUE:
                bytes = new byte[len];
                in.read(bytes);

                return new UniqueIdentifier(bytes);

            case SYBNUMERIC:
            case SYBDECIMAL:
                ci.precision = in.read();
                ci.scale = in.read();
                int sign = in.read();
                len--;
                bytes = new byte[len];
                BigInteger bi;

                while (len-- > 0) {
                    bytes[len] = (byte)in.read();
                }

                bi = new BigInteger((sign == 0) ? -1 : 1, bytes);

                return new BigDecimal(bi, ci.scale);

            default:
                throw new ProtocolException("Unsupported TDS data type 0x"
                                            + Integer.toHexString(ci.tdsType)
                                            + " in sql_variant");
        }
        //
        // For compatibility with the MS driver convert to String.
        // Change the data type for sql_variant from OTHER to VARCHAR
        // Without this code the actual Object type can be retrieved
        // by using getObject(n).
        //
//        try {
//            value = Support.convert(value, java.sql.Types.VARCHAR, in.getCharset());
//        } catch (SQLException e) {
//            // Conversion failed just try toString();
//            value = value.toString();
//        }
    }