List of usage examples for java.sql SQLException getSQLState
public String getSQLState()
SQLException
object. From source file:solidbase.core.DBVersion.java
/** * Initializes this instance by trying to read the version tables. It will succeed when one or all of the version tables does not exist. *///from w w w .j a va 2 s.c o m protected void init() { Assert.isTrue(this.stale); this.version = null; this.target = null; this.statements = 0; Connection connection = this.database.getDefaultConnection(); try { try { PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.versionTableName); try { ResultSet resultSet = statement.executeQuery(); // Resultset is closed when the statement is closed if (Util.hasColumn(resultSet, "SPEC")) this.specColumnExists = true; else Assert.isFalse(this.specColumnExists, "SPEC column in DBVERSION table has disappeared"); if (resultSet.next()) { this.version = resultSet.getString("VERSION"); this.target = resultSet.getString("TARGET"); this.statements = resultSet.getInt("STATEMENTS"); if (this.specColumnExists) setSpec(resultSet.getString("SPEC")); else setSpec(SPEC10); Assert.isFalse(resultSet.next()); this.callBack.debug("version=" + this.version + ", target=" + this.target + ", statements=" + this.statements); this.versionRecordExists = true; } else { Assert.isFalse(this.versionRecordExists, "Record in DBVERSION has disappeared"); } } finally { statement.close(); } } catch (SQLException e) { String sqlState = e.getSQLState(); // Oracle: 42000, MySQL: 42S02, Derby: 42X05, HSQLDB: S0002 if (!(sqlState.startsWith("42") || sqlState.startsWith("S0"))) throw new SystemException(e); Assert.isFalse(this.versionRecordExists, "DBVERSION table has disappeared"); } } finally { // PostgreSQL: if the SELECT above threw an SQLException, the transaction is in an 'aborted' state until it ends, // which means that we need to commit here too. try { connection.commit(); } catch (SQLException e) { throw new SystemException(e); } } try { try { PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.logTableName); try { statement.executeQuery(); this.logTableExists = true; } finally { statement.close(); } } catch (SQLException e) { String sqlState = e.getSQLState(); // Oracle: 42000, MySQL: 42S02, Derby: 42X05, HSQLDB: S0002 if (!(sqlState.startsWith("42") || sqlState.startsWith("S0"))) throw new SystemException(e); Assert.isFalse(this.logTableExists, "DBVERSIONLOG table has disappeared"); } } finally { try { connection.commit(); } catch (SQLException e) { throw new SystemException(e); } } this.stale = false; }
From source file:org.jumpmind.vaadin.ui.sqlexplorer.SqlRunner.java
protected String buildErrorMessage(Throwable ex) { StringBuilder errorMessage = new StringBuilder("<span style='color: red'>"); if (ex instanceof SQLException) { SQLException sqlException = (SQLException) ex; errorMessage.append("SQL Message: ").append(ex.getMessage()); errorMessage.append("\nSQL State: "); errorMessage.append(sqlException.getSQLState()); errorMessage.append("\nError Code: "); errorMessage.append(sqlException.getErrorCode()); } else {//from www .j ava2 s. c o m errorMessage.append(ex.getMessage()); errorMessage.append(ExceptionUtils.getStackTrace(ex)); } errorMessage.append("</span>"); return errorMessage.toString(); }
From source file:com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.java
protected void executeQueryWithCallback(StatementScope statementScope, Connection conn, Object parameterObject, Object resultObject, RowHandler rowHandler, int skipResults, int maxResults) throws SQLException { ErrorContext errorContext = statementScope.getErrorContext(); errorContext.setActivity("preparing the mapped statement for execution"); errorContext.setObjectId(this.getId()); errorContext.setResource(this.getResource()); try {//from w w w . j a v a 2 s. c o m parameterObject = validateParameter(parameterObject); Sql sql = getSql(); errorContext.setMoreInfo("Check the parameter map."); ParameterMap parameterMap = sql.getParameterMap(statementScope, parameterObject); errorContext.setMoreInfo("Check the result map."); ResultMap resultMap = sql.getResultMap(statementScope, parameterObject); statementScope.setResultMap(resultMap); statementScope.setParameterMap(parameterMap); errorContext.setMoreInfo("Check the parameter map."); Object[] parameters = parameterMap.getParameterObjectValues(statementScope, parameterObject); errorContext.setMoreInfo("Check the SQL statement."); String sqlString = sql.getSql(statementScope, parameterObject); errorContext.setActivity("executing mapped statement"); errorContext.setMoreInfo("Check the SQL statement or the result map."); RowHandlerCallback callback = new RowHandlerCallback(resultMap, resultObject, rowHandler); sqlExecuteQuery(statementScope, conn, sqlString, parameters, skipResults, maxResults, callback); errorContext.setMoreInfo("Check the output parameters."); if (parameterObject != null) { postProcessParameterObject(statementScope, parameterObject, parameters); } errorContext.reset(); sql.cleanup(statementScope); notifyListeners(); } catch (SQLException e) { errorContext.setCause(e); throw new NestedSQLException(errorContext.toString(), e.getSQLState(), e.getErrorCode(), e); } catch (Exception e) { errorContext.setCause(e); throw new NestedSQLException(errorContext.toString(), e); } }
From source file:com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.java
private void executePaginatedCountWithCallback(StatementScope statementScope, Connection conn, Object parameterObject, Object resultObject, RowHandler rowHandler) throws SQLException { ErrorContext errorContext = statementScope.getErrorContext(); errorContext.setActivity("preparing the mapped statement for execution"); errorContext.setObjectId(this.getId()); errorContext.setResource(this.getResource()); try {// ww w. j a v a 2s . c o m parameterObject = validateParameter(parameterObject); Sql sql = getSql(); errorContext.setMoreInfo("Check the parameter map."); ParameterMap parameterMap = sql.getParameterMap(statementScope, parameterObject); errorContext.setMoreInfo("Check the result map."); ResultMap resultMap = sql.getResultMap(statementScope, parameterObject); statementScope.setResultMap(resultMap); statementScope.setParameterMap(parameterMap); errorContext.setMoreInfo("Check the parameter map."); Object[] parameters = parameterMap.getParameterObjectValues(statementScope, parameterObject); errorContext.setMoreInfo("Check the SQL statement."); String sqlString = sql.getSql(statementScope, parameterObject); sqlString = paginatedCountSQL(sqlString); errorContext.setActivity("executing mapped statement"); errorContext.setMoreInfo("Check the SQL statement or the result map."); RowHandlerCallback callback = new RowHandlerCallback(resultMap, resultObject, rowHandler); sqlExecuteQuery(statementScope, conn, sqlString, parameters, SqlExecutor.NO_SKIPPED_RESULTS, SqlExecutor.NO_MAXIMUM_RESULTS, callback); errorContext.setMoreInfo("Check the output parameters."); if (parameterObject != null) { postProcessParameterObject(statementScope, parameterObject, parameters); } errorContext.reset(); sql.cleanup(statementScope); notifyListeners(); } catch (SQLException e) { errorContext.setCause(e); throw new NestedSQLException(errorContext.toString(), e.getSQLState(), e.getErrorCode(), e); } catch (Exception e) { errorContext.setCause(e); throw new NestedSQLException(errorContext.toString(), e); } }
From source file:com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.java
/** * @param statementScope/*from w w w .j a v a 2 s . c o m*/ * @param conn * @param parameterObject * @param resultObject * @param rowHandler * @param skipResults * @param maxResults * @throws SQLException * @author shaq */ protected void executePaginatedQueryWithCallback(StatementScope statementScope, Connection conn, Object parameterObject, Object resultObject, RowHandler rowHandler, int startRow, int endRow) throws SQLException { ErrorContext errorContext = statementScope.getErrorContext(); errorContext.setActivity("preparing the mapped statement for execution"); errorContext.setObjectId(this.getId()); errorContext.setResource(this.getResource()); try { parameterObject = validateParameter(parameterObject); Sql sql = getSql(); errorContext.setMoreInfo("Check the parameter map."); ParameterMap parameterMap = sql.getParameterMap(statementScope, parameterObject); errorContext.setMoreInfo("Check the result map."); ResultMap resultMap = sql.getResultMap(statementScope, parameterObject); statementScope.setResultMap(resultMap); statementScope.setParameterMap(parameterMap); errorContext.setMoreInfo("Check the parameter map."); Object[] parameters = parameterMap.getParameterObjectValues(statementScope, parameterObject); errorContext.setMoreInfo("Check the SQL statement."); String sqlString = sql.getSql(statementScope, parameterObject); sqlString = paginatedSQL(sqlString, startRow, endRow); errorContext.setActivity("executing mapped statement"); errorContext.setMoreInfo("Check the SQL statement or the result map."); RowHandlerCallback callback = new RowHandlerCallback(resultMap, resultObject, rowHandler); sqlExecuteQuery(statementScope, conn, sqlString, parameters, SqlExecutor.NO_SKIPPED_RESULTS, SqlExecutor.NO_MAXIMUM_RESULTS, callback); errorContext.setMoreInfo("Check the output parameters."); if (parameterObject != null) { postProcessParameterObject(statementScope, parameterObject, parameters); } errorContext.reset(); sql.cleanup(statementScope); notifyListeners(); } catch (SQLException e) { errorContext.setCause(e); throw new NestedSQLException(errorContext.toString(), e.getSQLState(), e.getErrorCode(), e); } catch (Exception e) { errorContext.setCause(e); throw new NestedSQLException(errorContext.toString(), e); } }
From source file:com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.java
public int executeUpdate(StatementScope statementScope, Transaction trans, Object parameterObject) throws SQLException { ErrorContext errorContext = statementScope.getErrorContext(); errorContext.setActivity("preparing the mapped statement for execution"); errorContext.setObjectId(this.getId()); errorContext.setResource(this.getResource()); statementScope.getSession().setCommitRequired(true); try {/*from w w w .j a v a 2s .c o m*/ parameterObject = validateParameter(parameterObject); Sql sql = getSql(); errorContext.setMoreInfo("Check the parameter map."); ParameterMap parameterMap = sql.getParameterMap(statementScope, parameterObject); errorContext.setMoreInfo("Check the result map."); ResultMap resultMap = sql.getResultMap(statementScope, parameterObject); statementScope.setResultMap(resultMap); statementScope.setParameterMap(parameterMap); int rows = 0; errorContext.setMoreInfo("Check the parameter map."); Object[] parameters = parameterMap.getParameterObjectValues(statementScope, parameterObject); errorContext.setMoreInfo("Check the SQL statement."); String sqlString = sql.getSql(statementScope, parameterObject); errorContext.setActivity("executing mapped statement"); errorContext.setMoreInfo("Check the statement or the result map."); rows = sqlExecuteUpdate(statementScope, trans.getConnection(), sqlString, parameters); errorContext.setMoreInfo("Check the output parameters."); if (parameterObject != null) { postProcessParameterObject(statementScope, parameterObject, parameters); } errorContext.reset(); sql.cleanup(statementScope); notifyListeners(); return rows; } catch (SQLException e) { errorContext.setCause(e); throw new NestedSQLException(errorContext.toString(), e.getSQLState(), e.getErrorCode(), e); } catch (Exception e) { errorContext.setCause(e); throw new NestedSQLException(errorContext.toString(), e); } }
From source file:com.mirth.connect.donkey.server.controllers.ChannelController.java
public Long getLocalChannelId(String channelId) { int attemptsRemaining = 3; while (true) { try {/*ww w .j a v a 2 s.co m*/ Long localChannelId = null; DonkeyDao dao = donkey.getDaoFactory().getDao(); try { localChannelId = dao.getLocalChannelIds().get(channelId); } finally { dao.close(); } if (localChannelId == null) { localChannelId = createChannel(channelId); } return localChannelId; } catch (DonkeyDaoException e) { /* * MIRTH-3475 If two server instances connected to a shared database attempt to * create channels at the same time, they may both obtain the same next local * channel ID, which will result in a duplicate key error. In the rare case that * this happens, we retry 2 more times. */ if (e.getCause() instanceof SQLException) { SQLException sqlException = (SQLException) e.getCause(); /* * The second part of this conditional tests if the SQLException was the result * of a duplicate key violation. MySQL, Oracle and SQL Server generate an * exception with SQLState == 23000, while Postgres returns SQLState == 23505. */ if (--attemptsRemaining == 0 || !(StringUtils.equals(sqlException.getSQLState(), "23000") || StringUtils.equals(sqlException.getSQLState(), "23505") || StringUtils.containsIgnoreCase(sqlException.getMessage(), "duplicate") || StringUtils.containsIgnoreCase(sqlException.getMessage(), "unique constraint"))) { throw e; } /* * If another server is in the middle of creating tables for this channel, wait * for a bit to let it finish. */ try { Thread.sleep(500); } catch (InterruptedException e1) { } } else { throw e; } } } }
From source file:org.h2gis.drivers.osm.OSMParser.java
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String type = attributes.getValue("type"); if (progress.isCanceled()) { throw new SAXException("Canceled by user"); }// w w w. j a va 2s .c om if (localName.compareToIgnoreCase("node") == 0) { nodeOSMElement = new NodeOSMElement(Double.valueOf(attributes.getValue("lat")), Double.valueOf(attributes.getValue("lon"))); setCommonsAttributes(nodeOSMElement, attributes); tagLocation = TAG_LOCATION.NODE; } else if (localName.compareToIgnoreCase("way") == 0) { wayOSMElement = new WayOSMElement(); setCommonsAttributes(wayOSMElement, attributes); tagLocation = TAG_LOCATION.WAY; } else if (localName.compareToIgnoreCase("tag") == 0) { String key = attributes.getValue("k"); String value = attributes.getValue("v"); boolean insertTag = true; switch (tagLocation) { case NODE: insertTag = nodeOSMElement.addTag(key, value); break; case WAY: insertTag = wayOSMElement.addTag(key, value); break; case RELATION: insertTag = relationOSMElement.addTag(key, value); break; } try { if (insertTag && !insertedTagsKeys.contains(key)) { tagPreparedStmt.setObject(1, key); tagPreparedStmt.execute(); insertedTagsKeys.add(key); } } catch (SQLException ex) { if (ex.getErrorCode() != ErrorCode.DUPLICATE_KEY_1 && !TAG_DUPLICATE_EXCEPTION.equals(ex.getSQLState())) { throw new SAXException("Cannot insert the tag : {" + key + " , " + value + "}", ex); } } } else if (localName.compareToIgnoreCase("nd") == 0) { wayOSMElement.addRef(attributes.getValue("ref")); } else if (localName.compareToIgnoreCase("relation") == 0) { relationOSMElement = new OSMElement(); setCommonsAttributes(relationOSMElement, attributes); tagLocation = TAG_LOCATION.RELATION; } else if (localName.compareToIgnoreCase("member") == 0) { if (type.equalsIgnoreCase("node")) { try { nodeMemberPreparedStmt.setObject(1, relationOSMElement.getID()); nodeMemberPreparedStmt.setObject(2, Long.valueOf(attributes.getValue("ref"))); nodeMemberPreparedStmt.setObject(3, attributes.getValue("role")); nodeMemberPreparedStmt.setObject(4, idMemberOrder); nodeMemberPreparedStmt.addBatch(); nodeMemberPreparedStmtBatchSize++; } catch (SQLException ex) { throw new SAXException( "Cannot insert the node member for the relation : " + relationOSMElement.getID(), ex); } } else if (type.equalsIgnoreCase("way")) { try { wayMemberPreparedStmt.setObject(1, relationOSMElement.getID()); wayMemberPreparedStmt.setObject(2, Long.valueOf(attributes.getValue("ref"))); wayMemberPreparedStmt.setObject(3, attributes.getValue("role")); wayMemberPreparedStmt.setObject(4, idMemberOrder); wayMemberPreparedStmt.addBatch(); wayMemberPreparedStmtBatchSize++; } catch (SQLException ex) { throw new SAXException( "Cannot insert the way member for the relation : " + relationOSMElement.getID(), ex); } } else if (type.equalsIgnoreCase("relation")) { try { relationMemberPreparedStmt.setObject(1, relationOSMElement.getID()); relationMemberPreparedStmt.setObject(2, Long.valueOf(attributes.getValue("ref"))); relationMemberPreparedStmt.setObject(3, attributes.getValue("role")); relationMemberPreparedStmt.setObject(4, idMemberOrder); relationMemberPreparedStmt.addBatch(); relationMemberPreparedStmtBatchSize++; } catch (SQLException ex) { throw new SAXException( "Cannot insert the relation member for the relation : " + relationOSMElement.getID(), ex); } } } }
From source file:jongo.RestController.java
/** * Method in charge of handling the possible exceptions thrown by the JDBCExecutor or any other * operation. The current implementation handles SQLException, JongoBadRequestException & * IllegalArgumentException to return different errors. For any other exception * a {@link jongo.rest.xstream.JongoError} with a 500 status code is returned. * @param t the exception to handle./*from w ww. ja v a 2 s . c om*/ * @param resource the name of the resource which is throwing the exception. * @return a {@link jongo.rest.xstream.JongoError} with different error codes depending * on the exception being handled. If we can't handle the exception, a 500 error code is used. */ private JongoResponse handleException(final Throwable t, final String resource) { JongoResponse response; StringBuilder b; if (t instanceof SQLException) { SQLException ex = (SQLException) t; b = new StringBuilder("Received a SQLException "); b.append(ex.getMessage()); b.append(" state ["); b.append(ex.getSQLState()); b.append("] & code ["); b.append(ex.getErrorCode()); b.append("]"); l.debug(b.toString()); response = new JongoError(resource, ex); } else if (t instanceof JongoBadRequestException) { b = new StringBuilder("Received a JongoBadRequestException "); b.append(t.getMessage()); l.debug(b.toString()); response = new JongoError(resource, Response.Status.BAD_REQUEST, t.getMessage()); } else if (t instanceof IllegalArgumentException) { b = new StringBuilder("Received an IllegalArgumentException "); b.append(t.getMessage()); l.debug(b.toString()); response = new JongoError(resource, Response.Status.BAD_REQUEST, t.getMessage()); } else { b = new StringBuilder("Received an Unhandled Exception "); b.append(t.getMessage()); l.error(b.toString()); response = new JongoError(resource, Response.Status.INTERNAL_SERVER_ERROR); } return response; }
From source file:org.openadaptor.auxil.connector.jdbc.JDBCConnection.java
/** * interprets SQLException, this make the distinction between * ConnectionExceptions (db server is unavilable) and ProcessingException (SQL * failed)/*from ww w . j av a 2 s . c om*/ * * @param e * the SQLException * @param message * an additonal message to append to the RuntimeException */ public void handleException(SQLException e, String message) { throw new ConnectionException((message != null ? message + ", " : "") + ", SQLException, " + e.getMessage() + ", Error Code = " + e.getErrorCode() + ", State = " + e.getSQLState(), e, this); }