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:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String connection = "jdbc:mysql://localhost:3306/YourDBName";
    String user = "root";
    String password = "root";
    Class.forName(driver);/*from  w  ww.ja v  a2  s.c o m*/
    Connection con = DriverManager.getConnection(connection, user, password);
    if (!con.isClosed()) {
        con.close();
    }
}

From source file:DBCPDemo.java

public static void main(String args[]) throws Exception {

    // create a generic pool
    GenericObjectPool pool = new GenericObjectPool(null);

    // use the connection factory which will wraped by
    // the PoolableConnectionFactory
    DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(
            "jdbc:jtds:sqlserver://myserver:1433/tandem", "user", "pass");

    PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, pool, null, "SELECT * FROM mysql.db",
            false, true);//from   w  ww.j a  va  2 s.c  o m

    // register our pool and give it a name
    new PoolingDriver().registerPool("myPool", pool);

    // get a connection and test it
    Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:myPool");

    // now we can use this pool the way we want.
    System.err.println("Are we connected? " + !conn.isClosed());

    System.err.println("Idle Connections: " + pool.getNumIdle() + ", out of " + pool.getNumActive());

}

From source file:JdbcConnect.java

public static void main(String[] args) throws Exception {
    Connection conn1 = null;
    Connection conn2 = null;/*  w  ww . j av a2s  .co  m*/
    Connection conn3 = null;

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    String jdbcUrl = "jdbc:odbc:authors";

    String user = "yourName";
    String pwd = "mypwd";

    conn1 = DriverManager.getConnection(jdbcUrl);
    if (conn1 != null) {
        System.out.println("Connection 1 successful!");
    }
    Properties prop = new Properties();
    prop.put("user", user);
    prop.put("password", pwd);

    conn2 = DriverManager.getConnection(jdbcUrl, prop);
    if (conn2 != null) {
        System.out.println("Connection 2 successful!");
    }
    conn3 = DriverManager.getConnection(jdbcUrl, user, pwd);
    if (conn3 != null) {
        System.out.println("Connection 3 successful!");
    }
    conn1.close();
    conn2.close();
    conn3.close();
    if (conn1.isClosed()) {
        System.out.println("Connection 1 is closed");
    }

    if (conn2.isClosed()) {
        System.out.println("Connection 2 is closed");
    }
    if (conn3.isClosed()) {
        System.out.println("Connection 3 is closed");
    }

    conn1.close();
    conn2.close();
    conn3.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost/testdb";
    String username = "root";
    String password = "";
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = null;
    try {/*w w  w .  j a va 2  s  .c  o  m*/
        conn = DriverManager.getConnection(url, username, password);
        conn.setAutoCommit(false);

        Statement st = conn.createStatement();
        st.execute("INSERT INTO orders (username, order_date) VALUES ('java', '2007-12-13')",
                Statement.RETURN_GENERATED_KEYS);

        ResultSet keys = st.getGeneratedKeys();
        int id = 1;
        while (keys.next()) {
            id = keys.getInt(1);
        }
        PreparedStatement pst = conn.prepareStatement(
                "INSERT INTO order_details (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)");
        pst.setInt(1, id);
        pst.setString(2, "1");
        pst.setInt(3, 10);
        pst.setDouble(4, 100);
        pst.execute();

        conn.commit();
        System.out.println("Transaction commit...");
    } catch (SQLException e) {
        if (conn != null) {
            conn.rollback();
            System.out.println("Connection rollback...");
        }
        e.printStackTrace();
    } finally {
        if (conn != null && !conn.isClosed()) {
            conn.close();
        }
    }
}

From source file:org.mskcc.cbio.dbcache.DatabaseUtil.java

/**
 * Frees Database Connection.//w w  w  .j av  a 2 s  . c  o  m
 *
 * @param con Connection Object.
 */
private static void closeConnection(Connection con) throws SQLException {
    if (con != null && !con.isClosed()) {
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.owasp.webgoat.session.DatabaseUtilities.java

/**
 * <p>getConnection.</p>/*from   w ww.  jav  a 2s. c  o  m*/
 *
 * @param user a {@link java.lang.String} object.
 * @param context a {@link org.owasp.webgoat.session.WebgoatContext} object.
 * @return a {@link java.sql.Connection} object.
 * @throws java.sql.SQLException if any.
 */
public static synchronized Connection getConnection(String user, WebgoatContext context) throws SQLException {
    Connection conn = connections.get(user);
    if (conn != null && !conn.isClosed())
        return conn;
    conn = makeConnection(user, context);
    connections.put(user, conn);

    if (dbBuilt.get(user) == null) {
        new CreateDB().makeDB(conn);
        dbBuilt.put(user, Boolean.TRUE);
    }

    return conn;
}

From source file:org.smart.migrate.util.ConnectionUtils.java

/**
 * disconnect to database//from w ww.  java  2 s .c o  m
 * ?
 * @param connection
 */
public static void disconnect(Connection connection) {
    if (connection == null) {
        return;
    }
    try {
        if (!connection.isClosed()) {
            connection.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

From source file:org.owasp.webgoat.session.DatabaseUtilities.java

/**
 * <p>returnConnection.</p>//from ww  w  .  jav a2 s.c  o  m
 *
 * @param user a {@link java.lang.String} object.
 */
public static synchronized void returnConnection(String user) {
    try {
        Connection connection = connections.get(user);
        if (connection == null || connection.isClosed())
            return;

        if (connection.getMetaData().getDatabaseProductName().toLowerCase().contains("oracle"))
            connection.close();
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}

From source file:org.mili.test.ConnectionUtils.java

/**
 * Shutdown connection.//from   www.  j  a va 2s.c  o  m
 *
 * @param c the connection
 * @throws SQLException if errors occurs
 */
static void shutdownConnection(Connection c) throws SQLException {
    TestUtils.update(c, "SHUTDOWN");
    if (!c.isClosed()) {
        c.close();
    }
}

From source file:CheckJDBCInstallation.java

/**
 * Test Validity of JDBC Installation/*from w w  w .j  a v  a2s .  co m*/
 * 
 * @param conn
 *          a JDBC connection object
 * @param dbVendor
 *          db vendor {"oracle", "mysql" }
 * @return true if a given connection object is a valid one; otherwise return
 *         false.
 * @throws Exception
 *           Failed to determine if a given connection is valid.
 */
public static boolean isValidConnection(Connection conn, String dbVendor) throws Exception {
    if (conn == null) {
        return false;
    }

    if (conn.isClosed()) {
        return false;
    }
    // depends on the vendor of the database:
    //
    // for MySQL database:
    // you may use the connection object
    // with query of "select 1"; if the
    // query returns the result, then it
    // is a valid Connection object.
    //
    // for Oracle database:
    // you may use the Connection object
    // with query of "select 1 from dual"; if
    // the query returns the result, then it
    // is a valid Connection object.
    if (dbVendor.equalsIgnoreCase("mysql")) {
        return testConnection(conn, "select 1");
    } else if (dbVendor.equalsIgnoreCase("oracle")) {
        return testConnection(conn, "select 1 from dual");
    } else {
        return false;
    }

}