Example usage for java.sql Connection isClosed

List of usage examples for java.sql Connection isClosed

Introduction

In this page you can find the example usage for java.sql Connection isClosed.

Prototype

boolean isClosed() throws SQLException;

Source Link

Document

Retrieves whether this Connection object has been closed.

Usage

From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyOlrLock(java.lang.String, boolean)
 *//*  w  w w.  j av  a 2s.co m*/
public synchronized boolean modifyOlrLock(final String userId, final boolean isLocked)
        throws UserManagementException {
    final String methodName = SQLUserManager.CNAME
            + "#modifyOlrLock(final String userId, final boolean value) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", userId);
        DEBUGGER.debug("Value: {}", isLocked);
    }

    Connection sqlConn = null;
    boolean isComplete = false;
    CallableStatement stmt = null;

    try {
        sqlConn = SQLUserManager.dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);

        // first make sure the existing password is proper
        // then make sure the new password doesnt match the existing password
        stmt = sqlConn.prepareCall("{ CALL updateOlrLock(?, ?,}");
        stmt.setString(1, userId);
        stmt.setBoolean(2, isLocked);

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        if (stmt.executeUpdate() == 1) {
            isComplete = true;
        }
    } catch (SQLException sqx) {
        throw new UserManagementException(sqx.getMessage(), sqx);
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }

            if (!(sqlConn == null) && (!(sqlConn.isClosed()))) {
                sqlConn.close();
            }
        } catch (SQLException sqx) {
            throw new UserManagementException(sqx.getMessage(), sqx);
        }
    }

    return isComplete;
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyUserContact(java.lang.String, java.util.List)
 *///from   w  w  w . j a  va 2 s  . c  om
public synchronized boolean modifyUserContact(final String userId, final List<String> values)
        throws UserManagementException {
    final String methodName = SQLUserManager.CNAME
            + "#modifyUserContact(final String userId, final List<String> values) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", userId);
        DEBUGGER.debug("Values: {}", values);
    }

    Connection sqlConn = null;
    boolean isComplete = false;
    CallableStatement stmt = null;

    try {
        sqlConn = SQLUserManager.dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);

        // first make sure the existing password is proper
        // then make sure the new password doesnt match the existing password
        stmt = sqlConn.prepareCall("{ CALL updateUserContact(?, ?, ?) }");
        stmt.setString(1, userId);
        stmt.setString(2, values.get(0));
        stmt.setString(2, values.get(1));

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        if (stmt.executeUpdate() == 1) {
            isComplete = true;
        }
    } catch (SQLException sqx) {
        throw new UserManagementException(sqx.getMessage(), sqx);
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }

            if (!(sqlConn == null) && (!(sqlConn.isClosed()))) {
                sqlConn.close();
            }
        } catch (SQLException sqx) {
            throw new UserManagementException(sqx.getMessage(), sqx);
        }
    }

    return isComplete;
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyOtpSecret(java.lang.String, boolean, java.lang.String)
 *///from w  w w.ja v  a 2  s.  c om
public synchronized boolean modifyOtpSecret(final String userId, final boolean addSecret, final String secret)
        throws UserManagementException {
    final String methodName = SQLUserManager.CNAME
            + "#modifyOtpSecret(final String userId, final boolean addSecret, final String secret) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("userId: {}", userId);
    }

    Connection sqlConn = null;
    boolean isComplete = false;
    CallableStatement stmt = null;

    try {
        sqlConn = SQLUserManager.dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);

        if (addSecret) {
            stmt = sqlConn.prepareCall("{ CALL addUserSecret(?, ?) }");
            stmt.setString(1, userId);
            stmt.setString(2, secret);
        } else {
            stmt = sqlConn.prepareCall("{ CALL removeUserSecret(?) }");
            stmt.setString(1, userId);
        }

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        if (stmt.executeUpdate() == 1) {
            isComplete = true;
        }
    } catch (SQLException sqx) {
        throw new UserManagementException(sqx.getMessage(), sqx);
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }

            if (!(sqlConn == null) && (!(sqlConn.isClosed()))) {
                sqlConn.close();
            }
        } catch (SQLException sqx) {
            throw new UserManagementException(sqx.getMessage(), sqx);
        }
    }

    return isComplete;
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyUserSecurity(java.lang.String, java.util.List)
 *///from  w  w  w .  j  av a  2s.c  om
public synchronized boolean modifyUserSecurity(final String userId, final List<String> values)
        throws UserManagementException {
    final String methodName = SQLUserManager.CNAME
            + "#modifyUserSecurity(final String userId, final List<String> values) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("userId: {}", userId);
    }

    Connection sqlConn = null;
    boolean isComplete = false;
    CallableStatement stmt = null;

    try {
        sqlConn = SQLUserManager.dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);

        // first make sure the existing password is proper
        // then make sure the new password doesnt match the existing password
        stmt = sqlConn.prepareCall("{ CALL updateUserSecurity(?, ?, ?, ?, ?) }");
        stmt.setString(1, userId);
        stmt.setString(2, values.get(0));
        stmt.setString(3, values.get(1));
        stmt.setString(4, values.get(2));
        stmt.setString(5, values.get(3));

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        if (stmt.executeUpdate() == 1) {
            isComplete = true;
        }
    } catch (SQLException sqx) {
        throw new UserManagementException(sqx.getMessage(), sqx);
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }

            if (!(sqlConn == null) && (!(sqlConn.isClosed()))) {
                sqlConn.close();
            }
        } catch (SQLException sqx) {
            throw new UserManagementException(sqx.getMessage(), sqx);
        }
    }

    return isComplete;
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#addUserAccount(java.util.List, java.util.List)
 *//*from  ww w. j  a v a  2 s.c o  m*/
public synchronized boolean addUserAccount(final List<String> userAccount, final List<String> roles)
        throws UserManagementException {
    final String methodName = SQLUserManager.CNAME
            + "#addUserAccount(final List<String> userAccount, final List<String> roles) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", userAccount);
        DEBUGGER.debug("Value: {}", roles);
    }

    Connection sqlConn = null;
    boolean isComplete = false;
    CallableStatement stmt = null;

    try {
        sqlConn = SQLUserManager.dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);

        stmt = sqlConn.prepareCall("{ CALL addUserAccount(?, ?, ?, ?, ?, ?, ?, ?) }");
        stmt.setString(1, userAccount.get(0)); // guid
        stmt.setString(2, userAccount.get(1)); // username
        stmt.setString(3, userAccount.get(2)); // password
        stmt.setBoolean(4, Boolean.valueOf(userAccount.get(3))); // suspended
        stmt.setString(5, userAccount.get(4)); // surname
        stmt.setString(6, userAccount.get(5)); // givenname
        stmt.setString(7, userAccount.get(6)); // displayname
        stmt.setString(8, userAccount.get(7)); // email

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        if (!(stmt.execute())) {
            isComplete = true;
        }
    } catch (SQLException sqx) {
        throw new UserManagementException(sqx.getMessage(), sqx);
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }

            if (!(sqlConn == null) && (!(sqlConn.isClosed()))) {
                sqlConn.close();
            }
        } catch (SQLException sqx) {
            throw new UserManagementException(sqx.getMessage(), sqx);
        }
    }

    return isComplete;
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyUserSuspension(java.lang.String, boolean)
 *//*w  w w .  j a  v  a  2 s. c  om*/
public synchronized boolean modifyUserSuspension(final String userId, final boolean isSuspended)
        throws UserManagementException {
    final String methodName = SQLUserManager.CNAME
            + "#modifyUserSuspension(final String userId, final boolean isSuspended) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", userId);
        DEBUGGER.debug("Value: {}", isSuspended);
    }

    Connection sqlConn = null;
    boolean isComplete = false;
    CallableStatement stmt = null;

    try {
        sqlConn = SQLUserManager.dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);

        stmt = sqlConn.prepareCall("{ CALL modifyUserSuspension(?, ?) }");
        stmt.setString(1, userId);
        stmt.setBoolean(2, isSuspended);

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        int x = stmt.executeUpdate();

        if (DEBUG) {
            DEBUGGER.debug("Update: {}", x);
        }

        if (x == 1) {
            isComplete = true;
        }
    } catch (SQLException sqx) {
        throw new UserManagementException(sqx.getMessage(), sqx);
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }

            if (!(sqlConn == null) && (!(sqlConn.isClosed()))) {
                sqlConn.close();
            }
        } catch (SQLException sqx) {
            throw new UserManagementException(sqx.getMessage(), sqx);
        }
    }

    return isComplete;
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#validateUserAccount(java.lang.String, java.lang.String)
 */// w ww .  j av a 2  s. co m
public synchronized boolean validateUserAccount(final String userId, final String userGuid)
        throws UserManagementException {
    final String methodName = SQLUserManager.CNAME
            + "#validateUserAccount(final String userId, final String userGuid) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", userId);
        DEBUGGER.debug("Value: {}", userGuid);
    }

    boolean isValid = false;
    Connection sqlConn = null;
    ResultSet resultSet = null;
    CallableStatement stmt = null;

    try {
        sqlConn = SQLUserManager.dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);

        stmt = sqlConn.prepareCall("{ CALL getUserByAttribute(?, ?) }");
        stmt.setString(1, userId);
        stmt.setInt(2, 0);

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        if (stmt.execute()) {
            resultSet = stmt.executeQuery();

            if (DEBUG) {
                DEBUGGER.debug("ResultSet: {}", resultSet);
            }

            if (resultSet.next()) {
                resultSet.beforeFirst();

                while (resultSet.next()) {
                    if ((StringUtils.equals(resultSet.getString(1), userGuid))
                            || (StringUtils.equals(resultSet.getString(2), userId))) {
                        resultSet.close();
                        stmt.close();
                        sqlConn.close();

                        throw new UserManagementException(
                                "A user currently exists with the provided information.");
                    }
                }
            }
        }
    } catch (SQLException sqx) {
        throw new UserManagementException(sqx.getMessage(), sqx);
    } finally {
        try {
            if (resultSet != null) {
                resultSet.close();
            }

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

            if (!(sqlConn == null) && (!(sqlConn.isClosed()))) {
                sqlConn.close();
            }
        } catch (SQLException sqx) {
            throw new UserManagementException(sqx.getMessage(), sqx);
        }
    }

    return isValid;
}

From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java

@SuppressWarnings("rawtypes")
public void update(Object obj, String[] columns) throws Exception {
    // ???/*from  w w  w .j a va2s  . c  om*/
    Class clazz = obj.getClass();
    String tableName = getTableName(clazz);

    if (StringUtils.isEmpty(tableName))
        throw new SQLException("No @Table annotation in Class " + clazz.getName());
    List<Column2Property> setterColumnsNames = getColumnsFromObj(obj, columns);
    if (null == setterColumnsNames || setterColumnsNames.size() == 0)
        throw new SQLException("Column is nul, you must specified update columns.");

    StringBuffer sb = new StringBuffer("update " + tableName);
    sb.append(" set ");
    int size = setterColumnsNames.size();
    Column2Property c;
    for (int i = 0; i < size; i++) {
        c = setterColumnsNames.get(i);
        if (i == 0)
            sb.append(c.columnName + " = ?");
        else
            sb.append("," + c.columnName + " = ? ");
    }

    Connection conn = null;
    try {
        conn = getJdbcTemplate().getDataSource().getConnection();

        TableMetaManager tableManager = TableMetaManager.getInstance();
        Table t = tableManager.getTable(tableName);
        if (t == null) {
            _loadTable_(conn, tableName, clazz);
            t = tableManager.getTable(tableName);
        }

        sb.append(" where " + t.getIdColumnName() + " = ?");

        if (conn.isClosed()) {
            throw new SQLException("Connection is closed!");
        }
        PreparedStatement st = conn.prepareStatement(sb.toString());
        for (int i = 1; i <= size; i++) {
            Column2Property column = setterColumnsNames.get(i - 1);
            if (obj == null) {
                st.setNull(i, java.sql.Types.NULL);
                continue;
            }

            Object value = MethodUtils.invokeMethod(obj, column.getterMethodName, null);
            if (value == null) {
                st.setNull(i, java.sql.Types.NULL);
                continue;
            }

            setColumnValue(st, column, t.getMeta(column.columnName), value, i);
        }

        // ?ID
        Column2Property id = getIdFromObject(obj.getClass());
        Object idValue = MethodUtils.invokeMethod(obj, id.getterMethodName, null);
        setColumnValue(st, t.getIdColumnName(), t.getIdMetaData(), idValue, size + 1);

        st.execute();
    } finally {
        try {
            conn.close();
        } catch (Exception e) {
        }
    }
}

From source file:updatePledge.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    String PledgeID = jComboBox4.getSelectedItem().toString();
    String PaymentFrequency, LastPaymentDate;
    PaymentFrequency = jComboBox5.getSelectedItem().toString();
    LastPaymentDate = jTextField2.getText();
    String Pay = jTextField1.getText();

    System.out.println(Pay);//from  w ww .  j a  va  2s.  c o  m

    StringCC str = new StringCC();
    Float PayToDate = Float.parseFloat(str.CleanUp(Pay));

    try {
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(viewDonors.class.getName()).log(Level.SEVERE, null, ex);
        }
        Connection con;
        con = DriverManager.getConnection(DBcon.Connect(), DBcon.Login(), DBcon.Pass()); //(file path, db login, db password) - since it doesnt have a login, leave it blank

        Statement s = con.createStatement();

        System.out.println("Connection to DB established...");

        s.executeUpdate("UPDATE [Pledges] SET " //also remember to space everything correctly, program was reading SETIND instead of SET IND
                + "Pledges.PaymentFrequency = " + "'" + PaymentFrequency + "'," + "Pledges.PayToDate = " + "'"
                + PayToDate + "'," + "Pledges.LastPaymentDate = " + "'" + LastPaymentDate + "' "
                + "WHERE (Pledges.PledgeID = '" + PledgeID + "')");

        System.out.println("Is connection closed: " + con.isClosed());
        System.out.println("Connection to DB established...");
        System.out.println("Update Executed.");

        con.commit();
        con.close();
        System.out.println("Is connection closed: " + con.isClosed());
    } catch (SQLException ex) {
        Logger.getLogger(userLogin.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(updatePledge.class.getName()).log(Level.SEVERE, null, ex);
    }
    jLabel4.setText("Updated.");

}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#listUserAccounts()
 *//* w  w  w.jav  a  2 s. c om*/
public synchronized List<String[]> listUserAccounts() throws UserManagementException {
    final String methodName = SQLUserManager.CNAME + "#listUserAccounts() throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
    }

    Connection sqlConn = null;
    ResultSet resultSet = null;
    CallableStatement stmt = null;
    List<String[]> results = null;

    try {
        sqlConn = SQLUserManager.dataSource.getConnection();

        if (sqlConn.isClosed()) {
            throw new SQLException("Unable to obtain application datasource connection");
        }

        sqlConn.setAutoCommit(true);

        stmt = sqlConn.prepareCall("{ CALL listUserAccounts() }");

        if (DEBUG) {
            DEBUGGER.debug("CallableStatement: {}", stmt);
        }

        if (stmt.execute()) {
            resultSet = stmt.getResultSet();

            if (resultSet.next()) {
                resultSet.beforeFirst();
                results = new ArrayList<String[]>();

                while (resultSet.next()) {
                    String[] userData = new String[] { resultSet.getString("cn"), resultSet.getString("uid") };

                    if (DEBUG) {
                        for (String str : userData) {
                            DEBUGGER.debug(str);
                        }
                    }

                    results.add(userData);
                }

                if (DEBUG) {
                    DEBUGGER.debug("List: {}", results);
                }
            }
        }
    } catch (SQLException sqx) {
        throw new UserManagementException(sqx.getMessage(), sqx);
    } finally {
        try {
            if (resultSet != null) {
                resultSet.close();
            }

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

            if (!(sqlConn == null) && (!(sqlConn.isClosed()))) {
                sqlConn.close();
            }
        } catch (SQLException sqx) {
            throw new UserManagementException(sqx.getMessage(), sqx);
        }
    }

    return results;
}