Example usage for java.sql PreparedStatement toString

List of usage examples for java.sql PreparedStatement toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.mzd.shap.sql.UpdateFeatureTable.java

/**
 * @param args//from  www.jav  a  2s  .c  om
 */
public static void main(String[] args) throws SQLException {

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost/BBay01a");
    ds.setUsername("test");
    ds.setPassword("xeno12");

    Connection conn = ds.getConnection();

    // Get the Sequence ID list.
    PreparedStatement select = null;
    PreparedStatement update = null;
    ResultSet result = null;

    select = conn.prepareStatement("SELECT DISTINCT SEQUENCE_ID FROM Features");
    result = select.executeQuery();
    List<Integer> sequenceIds = new ArrayList<Integer>();
    result.beforeFirst();
    while (result.next()) {
        sequenceIds.add(result.getInt(1));
    }
    result.close();
    select.close();

    // Get the list of Feature IDs for this Sequence ID
    select = conn.prepareStatement("SELECT FEATURE_ID FROM Features WHERE SEQUENCE_ID=? ORDER BY start");

    // Update the idx column for this feature id
    update = conn.prepareStatement("UPDATE Features SET featureOrder=? where FEATURE_ID=?");

    for (Integer seqId : sequenceIds) {
        select.setInt(1, seqId);
        result = select.executeQuery();

        int n = 0;
        result.beforeFirst();
        while (result.next()) {
            int featId = result.getInt(1);
            update.setInt(1, n++);
            update.setInt(2, featId);
            if (update.executeUpdate() != 1) {
                System.out.println("Failed update [" + update.toString() + "]");
                throw new SQLException();
            }
        }
        result.close();
    }

    update.close();
    select.close();

    conn.close();
    ds.close();
}

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

/**
 * Gets the SQL string statement associated with a PreparedStatement.
 * <p/>//  ww  w .  java 2  s.  c  o m
 * 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:com.wso2telco.util.DbUtil.java

public static String updateRegistrationStatus(String uuid, String status)
        throws SQLException, AuthenticatorException {

    Connection connection;/*from  w ww.j av  a2  s  .c o  m*/
    PreparedStatement ps;
    String sql = "UPDATE regstatus SET status = ? WHERE uuid = ?;";
    connection = getConnectDBConnection();
    ps = connection.prepareStatement(sql);
    ps.setString(1, status);
    ps.setString(2, uuid);
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
    return uuid;
}

From source file:org.intermine.sql.DatabaseUtil.java

/**
 * Add a column to an existing database table, if it does not already exist.
 * It is the users responsibility to close the connection after use.
 * @param con A connection to the database.
 * @param tableName The table to add the database too
 * @param columnName The column to add//from w  w w .j ava 2  s . co  m
 * @param type The SQL type to add
 * @throws SQLException if something goes wrong
 */
public static void addColumn(Connection con, String tableName, String columnName, Type type)
        throws SQLException {
    if (!DatabaseUtil.tableExists(con, tableName)) {
        throw new IllegalArgumentException(
                "there is no table named " + tableName + " in this" + " database to add a new column to");
    }
    if (DatabaseUtil.columnExists(con, tableName, columnName)) {
        return;
    }
    if (!DatabaseUtil.isLegalColumnName(columnName)) {
        throw new IllegalArgumentException("This is not a legal column name: " + columnName);
    }
    String sql = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + " " + type.getSQLType();
    PreparedStatement stmt = con.prepareStatement(sql);
    LOG.info(stmt.toString());
    stmt.executeUpdate();
}

From source file:org.intermine.sql.DatabaseUtil.java

/**
 * Set the default value in a column for all values.
 * @param con A connection to the database to use
 * @param tableName the table where update the column
 * @param columnName the column to Update
 * @param newValue the value to update/*from   w  ww .j  av a 2s .co  m*/
 * @throws SQLException if there is a database problem
 *
 * Note, it is the user's responsibility to ensure the connection given is closed.
 */
public static void updateColumnValue(Connection con, String tableName, String columnName, Object newValue)
        throws SQLException {
    if (DatabaseUtil.columnExists(con, tableName, columnName)) {
        String sql = "UPDATE " + tableName + " SET " + columnName + " = ?";
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setObject(1, newValue);
        LOG.info(stmt.toString());
        stmt.executeUpdate();
    }
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void incrementPinAttempts(String sessionId) throws SQLException, AuthenticatorException {

    Connection connection = null;
    PreparedStatement ps = null;

    String sql = "update multiplepasswords set attempts=attempts +1 where ussdsessionid = ?;";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setString(1, sessionId);/*www  .ja v a 2 s . c  o m*/
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void updateIdsRegStatus(String username, String status)
        throws SQLException, AuthenticatorException {
    Connection connection;/*w  w  w . j ava 2s . com*/
    PreparedStatement ps;

    String sql = "update `regstatus` set " + "status=? where " + "username=?;";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setString(1, status);
    ps.setString(2, username);
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void updatePin(int pin, String sessionId) throws SQLException, AuthenticatorException {

    Connection connection = null;
    PreparedStatement ps = null;

    String sql = "update multiplepasswords set pin=? where ussdsessionid = ?;";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setInt(1, pin);/*from w  ww  .j av a  2s  . c  om*/
    ps.setString(2, sessionId);
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void updateAuthenticateData(String msisdn, String status)
        throws SQLException, AuthenticatorException {
    Connection connection = null;
    PreparedStatement ps = null;
    String sql = "update `authenticated_login` set " + "status=? where " + "msisdn=?;";

    connection = getConnectDBConnection();
    ps = connection.prepareStatement(sql);
    ps.setString(1, status);//from  w  w w  .ja  va  2 s . c  o  m
    ps.setString(2, msisdn);
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void updateMultiplePasswordNoOfAttempts(String username, int attempts)
        throws SQLException, AuthenticatorException {

    Connection connection = null;
    PreparedStatement ps = null;

    String sql = "update `multiplepasswords` set  attempts=? where  username=?;";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setInt(1, attempts);//from   w  w w .  ja  va 2 s .c o m
    ps.setString(2, username);
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
}