List of usage examples for java.sql SQLWarning getSQLState
public String getSQLState()
SQLException
object. From source file:edu.lternet.pasta.doi.DOIScannerTest.java
private static Connection getConnection() throws Exception { Connection conn = null;//from ww w. j ava 2 s. c om SQLWarning warn; Class.forName(dbDriver); // Make the database connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword); // If a SQLWarning object is available, print its warning(s). // There may be multiple warnings chained. warn = conn.getWarnings(); if (warn != null) { while (warn != null) { System.err.println("SQLState: " + warn.getSQLState()); System.err.println("Message: " + warn.getMessage()); System.err.println("Vendor: " + warn.getErrorCode()); warn = warn.getNextWarning(); } } return conn; }
From source file:com.splicemachine.derby.impl.sql.execute.operations.InsertOperationIT.java
@Test public void testDataTruncationWarningIsEmitted() throws Exception { PreparedStatement ps = methodWatcher.prepareStatement("insert into WARNING values cast(? as char(1))"); ps.setString(1, "12"); int updated = ps.executeUpdate(); Assert.assertEquals("Incorrect number of rows updated!", 1, updated); SQLWarning warning = ps.getWarnings(); String sqlState = warning.getSQLState(); Assert.assertEquals("Incorrect warning code returned!", "01004", sqlState); }
From source file:edu.lternet.pasta.token.TokenManager.java
/** * Returns a connection to the database. * * @return The database Connection object. *//*from w w w . j a v a 2s . c o m*/ private Connection getConnection() throws ClassNotFoundException { Connection conn = null; SQLWarning warn; // Load the jdbc driver. try { Class.forName(this.dbDriver); } catch (ClassNotFoundException e) { logger.error("Can't load driver " + e.getMessage()); throw (e); } // Make the database connection try { conn = DriverManager.getConnection(this.dbURL, this.dbUser, this.dbPassword); // If a SQLWarning object is available, print its warning(s). // There may be multiple warnings chained. warn = conn.getWarnings(); if (warn != null) { while (warn != null) { logger.warn("SQLState: " + warn.getSQLState()); logger.warn("Message: " + warn.getMessage()); logger.warn("Vendor: " + warn.getErrorCode()); warn = warn.getNextWarning(); } } } catch (SQLException e) { logger.error("Database access failed " + e); } return conn; }
From source file:cc.tooyoung.common.db.JdbcTemplate.java
/** * Throw an SQLWarningException if we're not ignoring warnings, * else log the warnings (at debug level). * @param stmt the current JDBC statement * @throws SQLWarningException if not ignoring warnings * @see org.springframework.jdbc.SQLWarningException *//*w w w . java 2s . c o m*/ protected void handleWarnings(Statement stmt) throws SQLException { if (isIgnoreWarnings()) { if (ApiLogger.isTraceEnabled()) { SQLWarning warningToLog = stmt.getWarnings(); while (warningToLog != null) { ApiLogger.trace("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() + "', error code '" + warningToLog.getErrorCode() + "', message [" + warningToLog.getMessage() + "]"); warningToLog = warningToLog.getNextWarning(); } } } else { handleWarnings(stmt.getWarnings()); } }
From source file:lib.JdbcTemplate.java
/** * Throw an SQLWarningException if we're not ignoring warnings, * else log the warnings (at debug level). * @param stmt the current JDBC statement * @throws SQLWarningException if not ignoring warnings * @see org.springframework.jdbc.SQLWarningException *//* w ww .j a v a2 s .co m*/ protected void handleWarnings(Statement stmt) throws SQLException { if (isIgnoreWarnings()) { if (logger.isDebugEnabled()) { SQLWarning warningToLog = stmt.getWarnings(); while (warningToLog != null) { logger.debug("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() + "', error code '" + warningToLog.getErrorCode() + "', message [" + warningToLog.getMessage() + "]"); warningToLog = warningToLog.getNextWarning(); } } } else { handleWarnings(stmt.getWarnings()); } }
From source file:nl.nn.adapterframework.util.JdbcUtil.java
public static XmlBuilder warningsToXmlBuilder(SQLWarning warnings) { if (warnings != null) { XmlBuilder warningsElem = new XmlBuilder("warnings"); while (warnings != null) { XmlBuilder warningElem = new XmlBuilder("warning"); warningElem.addAttribute("errorCode", "" + warnings.getErrorCode()); warningElem.addAttribute("sqlState", "" + warnings.getSQLState()); String message = warnings.getMessage(); // getCause() geeft unresolvedCompilationProblem (bij Peter Leeuwenburgh?) Throwable cause = warnings.getCause(); if (cause != null) { warningElem.addAttribute("cause", cause.getClass().getName()); if (message == null) { message = cause.getMessage(); } else { message = message + ": " + cause.getMessage(); }/*from w ww . j av a2 s .co m*/ } warningElem.addAttribute("message", message); warningsElem.addSubElement(warningElem); warnings = warnings.getNextWarning(); } return warningsElem; } return null; }
From source file:org.gbif.ipt.service.manage.impl.SourceManagerImpl.java
private Connection getDbConnection(SqlSource source) throws SQLException { Connection conn = null;/*from w w w. ja v a2 s.co m*/ // try to connect to db via simple JDBC if (source.getHost() != null && source.getJdbcUrl() != null && source.getJdbcDriver() != null) { try { DriverManager.setLoginTimeout(CONNECTION_TIMEOUT_SECS); Class.forName(source.getJdbcDriver()); conn = DriverManager.getConnection(source.getJdbcUrl(), source.getUsername(), source.getPassword()); // If a SQLWarning object is available, log its // warning(s). There may be multiple warnings chained. SQLWarning warn = conn.getWarnings(); while (warn != null) { log.warn("SQLWarning: state=" + warn.getSQLState() + ", message=" + warn.getMessage() + ", vendor=" + warn.getErrorCode()); warn = warn.getNextWarning(); } } catch (java.lang.ClassNotFoundException e) { String msg = String.format( "Couldnt load JDBC driver to create new external datasource connection with JDBC Class=%s and URL=%s. Error: %s", source.getJdbcDriver(), source.getJdbcUrl(), e.getMessage()); log.warn(msg, e); throw new SQLException(msg, e); } catch (Exception e) { String msg = String.format( "Couldnt create new external datasource connection with JDBC Class=%s, URL=%s, user=%s. Error: %s", source.getJdbcDriver(), source.getJdbcUrl(), source.getUsername(), e.getMessage()); log.warn(msg, e); throw new SQLException(msg); } } return conn; }
From source file:org.springframework.batch.item.database.AbstractCursorItemReader.java
/** * Throw a SQLWarningException if we're not ignoring warnings, else log the * warnings (at debug level).//www .j a v a 2s. c o m * * @param statement the current statement to obtain the warnings from, if there are any. * @throws SQLException if interaction with provided statement fails. * * @see org.springframework.jdbc.SQLWarningException */ protected void handleWarnings(Statement statement) throws SQLWarningException, SQLException { if (ignoreWarnings) { if (log.isDebugEnabled()) { SQLWarning warningToLog = statement.getWarnings(); while (warningToLog != null) { log.debug("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() + "', error code '" + warningToLog.getErrorCode() + "', message [" + warningToLog.getMessage() + "]"); warningToLog = warningToLog.getNextWarning(); } } } else { SQLWarning warnings = statement.getWarnings(); if (warnings != null) { throw new SQLWarningException("Warning not ignored", warnings); } } }
From source file:org.springframework.jdbc.datasource.init.ScriptUtils.java
/** * Execute the given SQL script.// w ww .ja va 2 s . co m * <p>Statement separators and comments will be removed before executing * individual statements within the supplied script. * <p><strong>Warning</strong>: this method does <em>not</em> release the * provided {@link Connection}. * @param connection the JDBC connection to use to execute the script; already * configured and ready to use * @param resource the resource (potentially associated with a specific encoding) * to load the SQL script from * @param continueOnError whether or not to continue without throwing an exception * in the event of an error * @param ignoreFailedDrops whether or not to continue in the event of specifically * an error on a {@code DROP} statement * @param commentPrefix the prefix that identifies single-line comments in the * SQL script — typically "--" * @param separator the script statement separator; defaults to * {@value #DEFAULT_STATEMENT_SEPARATOR} if not specified and falls back to * {@value #FALLBACK_STATEMENT_SEPARATOR} as a last resort; may be set to * {@value #EOF_STATEMENT_SEPARATOR} to signal that the script contains a * single statement without a separator * @param blockCommentStartDelimiter the <em>start</em> block comment delimiter; never * {@code null} or empty * @param blockCommentEndDelimiter the <em>end</em> block comment delimiter; never * {@code null} or empty * @throws ScriptException if an error occurred while executing the SQL script * @see #DEFAULT_STATEMENT_SEPARATOR * @see #FALLBACK_STATEMENT_SEPARATOR * @see #EOF_STATEMENT_SEPARATOR * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection */ public static void executeSqlScript(Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, @Nullable String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException { try { if (logger.isInfoEnabled()) { logger.info("Executing SQL script from " + resource); } long startTime = System.currentTimeMillis(); String script; try { script = readScript(resource, commentPrefix, separator); } catch (IOException ex) { throw new CannotReadScriptException(resource, ex); } if (separator == null) { separator = DEFAULT_STATEMENT_SEPARATOR; } if (!EOF_STATEMENT_SEPARATOR.equals(separator) && !containsSqlScriptDelimiters(script, separator)) { separator = FALLBACK_STATEMENT_SEPARATOR; } List<String> statements = new LinkedList<>(); splitSqlScript(resource, script, separator, commentPrefix, blockCommentStartDelimiter, blockCommentEndDelimiter, statements); int stmtNumber = 0; Statement stmt = connection.createStatement(); try { for (String statement : statements) { stmtNumber++; try { stmt.execute(statement); int rowsAffected = stmt.getUpdateCount(); if (logger.isDebugEnabled()) { logger.debug(rowsAffected + " returned as update count for SQL: " + statement); SQLWarning warningToLog = stmt.getWarnings(); while (warningToLog != null) { logger.debug("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() + "', error code '" + warningToLog.getErrorCode() + "', message [" + warningToLog.getMessage() + "]"); warningToLog = warningToLog.getNextWarning(); } } } catch (SQLException ex) { boolean dropStatement = StringUtils.startsWithIgnoreCase(statement.trim(), "drop"); if (continueOnError || (dropStatement && ignoreFailedDrops)) { if (logger.isDebugEnabled()) { logger.debug(ScriptStatementFailedException.buildErrorMessage(statement, stmtNumber, resource), ex); } } else { throw new ScriptStatementFailedException(statement, stmtNumber, resource, ex); } } } } finally { try { stmt.close(); } catch (Throwable ex) { logger.debug("Could not close JDBC Statement", ex); } } long elapsedTime = System.currentTimeMillis() - startTime; if (logger.isInfoEnabled()) { logger.info("Executed SQL script from " + resource + " in " + elapsedTime + " ms."); } } catch (Exception ex) { if (ex instanceof ScriptException) { throw (ScriptException) ex; } throw new UncategorizedScriptException( "Failed to execute database script from resource [" + resource + "]", ex); } }