Example usage for java.sql Statement toString

List of usage examples for java.sql Statement toString

Introduction

In this page you can find the example usage for java.sql Statement toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:com.tacitknowledge.util.migration.jdbc.util.SqlUtil.java

/**
 * Ensures the given connection, statement, and result are properly closed.
 *
 * @param conn the connection to close; may be <code>null</code>
 * @param stmt the statement to close; may be <code>null</code>
 * @param rs   the result set to close; may be <code>null</code>
 *//*from  w w  w .jav a  2  s.  c om*/
public static void close(Connection conn, Statement stmt, ResultSet rs) {
    if (rs != null) {
        try {
            log.debug("Closing ResultSet: " + rs.toString());
            rs.close();
        } catch (SQLException e) {
            log.error("Error closing ResultSet", e);
        }
    }

    if (stmt != null) {
        try {
            log.debug("Closing Statement: " + stmt.toString());
            stmt.close();
        } catch (SQLException e) {
            log.error("Error closing Statement", e);
        }
    }

    if (conn != null) {
        try {
            if (!conn.isClosed()) {
                log.debug("Closing Connection " + conn.toString());
                conn.close();
            } else {
                log.debug("Connection (" + conn.toString() + ") already closed.");
            }
        } catch (SQLException e) {
            log.error("Error closing Connection", e);
        }
    }
}

From source file:io.agi.framework.persistence.jdbc.JdbcUtil.java

/**
 * Execute a SQL query that returns data.
 *
 * @param dbUrl/*from  ww w .  ja  v  a  2 s .com*/
 * @param user
 * @param password
 * @param sql
 * @param cb
 */
public static void ExecuteQuery(String dbUrl, String user, String password, String sql, ResultSetCallback cb) {
    Connection c = null;
    Statement s = null;
    try {

        //            c = DriverManager.getConnection(dbUrl, user, password);
        c = GetConnection(dbUrl, user, password);

        //STEP 4: Execute a query
        s = c.createStatement();
        ResultSet rs = s.executeQuery(sql);

        if (cb != null) {
            cb.onResultSet(rs);
        }

        //STEP 6: Clean-up environment
        rs.close();
        s.close();
        c.close();
    } catch (SQLException se) {
        logger.error(se.toString(), se);
    } catch (Exception e) {
        logger.error(s.toString(), s);
    } finally {
        try {
            if (s != null)
                s.close();
        } catch (SQLException se2) {
            logger.error(se2.toString(), se2);
        }

        try {
            if (c != null)
                c.close();
        } catch (SQLException se) {
            logger.error(se.toString(), se);
        }

    }
}

From source file:io.agi.framework.persistence.jdbc.JdbcUtil.java

/**
 * Execute an INSERT or UPDATE statement, that doesn't return any data.
 *
 * @param dbUrl/*from w ww. j  a  v  a2 s. co  m*/
 * @param user
 * @param password
 * @param sql
 */
public static void Execute(String dbUrl, String user, String password, String sql) {
    Connection c = null;
    Statement s = null;
    try {
        c = DriverManager.getConnection(dbUrl, user, password);

        //STEP 4: Execute a query
        //_logger.info( "JDBC T: {} @1 jdbc = {}", System.currentTimeMillis(), sql );
        s = c.createStatement();
        //_logger.info( "JDBC T: {} @2 ", System.currentTimeMillis() );
        s.execute(sql);
        //_logger.info( "JDBC T: {} @3 ", System.currentTimeMillis() );

        //STEP 6: Clean-up environment
        //            s.close();
        //            c.close();
    } catch (SQLException se) {
        logger.error(se.toString(), se);
    } catch (Exception e) {
        logger.error(s.toString(), s);
    } finally {
        try {
            if (s != null)
                s.close();
        } catch (SQLException se2) {
            logger.error(se2.toString(), se2);
        } finally {
            try {
                if (c != null)
                    c.close();
            } catch (SQLException se) {
                logger.error(se.toString(), se);
            }
        }
    }
}

From source file:com.wso2telco.dep.reportingservice.dao.TaxDAO.java

/**
 * Gets the taxes for tax list./*from   ww w . ja v a  2  s  .c om*/
 *
 * @param taxList the tax list
 * @return the taxes for tax list
 * @throws Exception the exception
 */
public List<Tax> getTaxesForTaxList(List<String> taxList) throws Exception {
    Connection connection = null;
    Statement st = null;
    ResultSet results = null;

    List<Tax> taxes = new ArrayList<Tax>();

    if (taxList == null || taxList.isEmpty()) {
        return taxes;
    }

    // CSV format surrounded by single quote
    String taxListStr = taxList.toString().replace("[", "'").replace("]", "'").replace(", ", "','");

    String sql = "SELECT type,effective_from,effective_to,value FROM " + ReportingTable.TAX + " WHERE type IN ("
            + taxListStr + ")";

    try {
        connection = DbUtils.getDbConnection(DataSourceNames.WSO2AM_STATS_DB);
        st = connection.createStatement();
        log.debug("In getTaxesForTaxList");
        log.debug("SQL (PS) ---> " + st.toString());
        results = st.executeQuery(sql);
        while (results.next()) {
            Tax tax = new Tax();
            tax.setType(results.getString("type"));
            tax.setEffective_from(results.getDate("effective_from"));
            tax.setEffective_to(results.getDate("effective_to"));
            tax.setValue(results.getBigDecimal("value"));
            taxes.add(tax);
        }
        st.close();
    } catch (SQLException e) {
        log.error("SQL Error in getTaxesForTaxList");
        log.error(e.getStackTrace());
        handleException("Error occurred while getting Taxes for Tax List", e);
    } finally {
        DbUtils.closeAllConnections(null, connection, results);
    }
    return taxes;
}

From source file:org.apache.jmeter.protocol.jdbc.AbstractJDBCTestElement.java

public static void close(Statement s) {
    try {//  w  w w. j a  v a 2  s.  c  o  m
        if (s != null) {
            s.close();
        }
    } catch (SQLException e) {
        log.warn("Error closing Statement " + s.toString(), e);
    }
}

From source file:org.apache.jmeter.protocol.jdbc.AbstractJDBCwoTimeOutTestElement.java

public static void close(final Statement s) {
    try {//from  w  w w  .  j a va2s  . c om
        if (s != null) {
            s.close();
        }
    } catch (final SQLException e) {
        log.warn("Error closing Statement " + s.toString(), e);
    }
}

From source file:org.mskcc.cbio.cgds.dao.JdbcUtil.java

/**
 * Gets the SQL string statement associated with a PreparedStatement.
 * <p/>//  www.j  a  va2  s  . com
 * This method compensates for a bug in the DBCP Code.  DBCP wraps an
 * original PreparedStatement object, but when you call toString() on the
 * wrapper, it returns a generic String representation that does not include
 * the actual SQL code which gets executed.  To get around this bug, this
 * method checks to see if we have a DBCP wrapper.  If we do, we get the
 * original delegate, and properly call its toString() method.  This
 * results in the actual SQL statement sent to the database.
 *
 * @param pstmt PreparedStatement Object.
 * @return toString value.
 */
public static String getSqlQuery(PreparedStatement pstmt) {
    if (pstmt instanceof DelegatingPreparedStatement) {
        DelegatingPreparedStatement dp = (DelegatingPreparedStatement) pstmt;
        Statement delegate = dp.getDelegate();
        return delegate.toString();
    } else {
        return pstmt.toString();
    }
}

From source file:org.opencms.db.CmsDbSqlException.java

/**
 * Returns the query that let the statement crash.<p>
 * /*  www .  j  a v a 2s. c o m*/
 * @param stmt the Statement to get the crashed query from
 * @return the crashed query
 */
public static String getErrorQuery(Statement stmt) {

    if (stmt != null) {
        // unfortunately, DelegatingPreparedStatement has no toString() method implementation
        Statement s = stmt;
        while (s instanceof DelegatingPreparedStatement) {
            s = ((DelegatingPreparedStatement) s).getInnermostDelegate();
        }
        if (s != null) {
            // the query that crashed
            return s.toString();
        }
    }
    return "";
}

From source file:storybook.model.oldModel.ModelMigration.java

/**
 * Closes the statement/*from w ww .java 2  s  . co m*/
 *
 * @param stmt The Statement that needs to close
 */
public void closeStatement(Statement stmt) {
    try {
        if (stmt != null) {
            stmt.close();
        }
    } catch (SQLException se) {
        SbApp.error("*** ModelMigration.closeStatement(" + stmt.toString() + ")", se);
    }
}

From source file:storybook.model.oldModel.ModelMigration.java

private void executeSQLStatement(String sql, Statement stmt) {
    SbApp.trace("ModelMigration.executeSQLStatement(" + sql.toString() + "," + stmt.toString() + ")");
    try {/*w w  w  . java 2s .  c om*/
        stmt.execute(sql);
    } catch (SQLException e) {
        SbApp.error("ModelMigration.executeSQLStatement(" + sql + "," + stmt.toString() + ")", e);
        ExceptionDialog dlg = new ExceptionDialog(e);
        SwingUtil.showModalDialog(dlg, mainFrame);
    }
}