Example usage for java.sql Connection close

List of usage examples for java.sql Connection close

Introduction

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

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*from w ww  .  ja v  a2s . c om*/
        String url = "jdbc:odbc:databaseName";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        String changeLastName = "UPDATE authors SET lastname = ? WHERE authid = ?";
        PreparedStatement updateLastName = connection.prepareStatement(changeLastName);

        updateLastName.setString(1, "Martin"); // Set lastname placeholder value
        updateLastName.setInt(2, 4); // Set author ID placeholder value

        int rowsUpdated = updateLastName.executeUpdate(); // execute the update
        System.out.println("Rows affected: " + rowsUpdated);
        connection.close();
    } catch (ClassNotFoundException cnfe) {
        System.err.println(cnfe);
    } catch (SQLException sqle) {
        System.err.println(sqle);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] data = new byte[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        data[i] = (byte) (64 + (i % 32));
    }/*  w  ww .j  a  v a 2s.com*/
    ByteArrayInputStream stream = new ByteArrayInputStream(data);
    // DriverManager.registerDriver(new OracleDriver());
    Connection c = DriverManager.getConnection("jdbc:oracle:thin:@some_database", "user", "password");

    String sql = "DECLARE\n" + //
            "  l_line    CLOB;\n" + //
            "BEGIN\n" + //
            "  l_line := ?;\n" + //
            "  UPDATE table SET log = log || l_line || CHR(10) WHERE id = ?;\n" + //
            "END;\n"; //

    PreparedStatement stmt = c.prepareStatement(sql);
    stmt.setAsciiStream(1, stream, SIZE);
    stmt.setInt(2, 1);
    stmt.execute();
    stmt.close();
    c.commit();
    c.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name CHAR(5) );");

    stmt.executeUpdate("INSERT INTO survey(id, name)VALUES(111, '123456789')");

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    outputResultSet(rs);//from  w w  w .j  a  v a2s  .  c  o m

    stmt.executeUpdate("update survey set name='newName' where id = 111");

    rs = stmt.executeQuery("SELECT * FROM survey");
    outputResultSet(rs);

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

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

    Connection con = null;
    Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
    con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/Keyspace");

    String query = "SELECT * FROM column_family where gender=f ";

    Statement stmt = con.createStatement();
    ResultSet result = stmt.executeQuery(query);

    while (result.next()) {
        System.out.println(result.getString("user_name"));
        System.out.println(result.getString("gender"));
        System.out.println(result.getString("password"));
    }// w w w  .j  av a2  s . c o m
    con.close();
}

From source file:TestRegisterDriverApp.java

public static void main(String args[]) {

    try {/*from w  w  w  . ja va2s.  c  o  m*/
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    } catch (SQLException e) {
        System.out.println("Oops! Got a SQL error: " + e.getMessage());
        System.exit(1);
    }

    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");

        stmt = conn.createStatement();
        rset = stmt.executeQuery("select 'Hello '||USER||'!' result from dual");
        while (rset.next())
            System.out.println(rset.getString(1));
        rset.close();
        rset = null;
        stmt.close();
        stmt = null;
        conn.close();
        conn = null;
    } catch (SQLException e) {
        System.out.println("Darn! A SQL error: " + e.getMessage());
    } finally {
        if (rset != null)
            try {
                rset.close();
            } catch (SQLException ignore) {
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException ignore) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
    }
}

From source file:TestClassForNameApp.java

public static void main(String args[]) {

    try {/*from w  ww  .  ja va  2s .c o  m*/
        Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
        System.out.println("Oops! Can't find class oracle.jdbc.driver.OracleDriver");
        System.exit(1);
    }

    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");

        stmt = conn.createStatement();
        rset = stmt.executeQuery("select 'Hello '||USER||'!' result from dual");
        while (rset.next())
            System.out.println(rset.getString(1));
        rset.close();
        rset = null;
        stmt.close();
        stmt = null;
        conn.close();
        conn = null;
    } catch (SQLException e) {
        System.out.println("Darn! A SQL error: " + e.getMessage());
    } finally {
        if (rset != null)
            try {
                rset.close();
            } catch (SQLException ignore) {
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException ignore) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);
    conn.setAutoCommit(false);/*from w ww .j a v a2s  .c om*/

    String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "java.gif");
    stmt.setString(2, "Java Official Logo");

    File image = new File("D:\\a.gif");
    FileInputStream fis = new FileInputStream(image);
    stmt.setBinaryStream(3, fis, (int) image.length());
    stmt.execute();

    conn.commit();
    fis.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();

    DatabaseMetaData mtdt = conn.getMetaData();
    System.out.println("URL in use: " + mtdt.getURL());
    System.out.println("User name: " + mtdt.getUserName());
    System.out.println("DBMS name: " + mtdt.getDatabaseProductName());
    System.out.println("DBMS version: " + mtdt.getDatabaseProductVersion());
    System.out.println("Driver name: " + mtdt.getDriverName());
    System.out.println("Driver version: " + mtdt.getDriverVersion());
    System.out.println("supp. SQL Keywords: " + mtdt.getSQLKeywords());

    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myDate DATE);");
    String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    pstmt.setNull(2, java.sql.Types.DATE);

    pstmt.executeUpdate();//from w w w.  j a  v  a2s  .co  m

    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();/*  ww  w .  j a  v a  2s  . c o m*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    rs.close();
    st.close();
    conn.close();
}