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

public static void main(String[] args) {
    Connection conn = null;
    try {/* w w w.  jav a2s  .  c  o m*/
        String dbVendor = args[0];
        // get connection to a database
        System.out.println("dbVendor=" + dbVendor);
        System.out.println("conn=" + conn);
        System.out.println("valid connection = " + isValidConnection(conn, dbVendor));
    } catch (Exception e) {
        // handle the exception
        e.printStackTrace();
        System.exit(1);
    } finally {
        // release database resources
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);/* w  w w  .  j  a v a 2s .  co m*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    DatabaseMetaData metadata = connection.getMetaData();
    String driverName = metadata.getDriverName();
    String driverVersion = metadata.getDriverVersion();
    int majorVersion = metadata.getDriverMajorVersion();
    int minorVersion = metadata.getDriverMinorVersion();

    System.out.println("driverName = " + driverName);
    System.out.println("driverVersion = " + driverVersion);
    System.out.println("majorVersion = " + majorVersion);
    System.out.println("minorVersion = " + minorVersion);
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    float floatValue = 0001f;
    double doubleValue = 1.0001d;
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {//from  w w w . java  2  s  .  co m
        conn = getConnection();
        String query = "insert into double_table(id, float_column, double_column) values(?, ?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        pstmt.setFloat(2, floatValue);
        pstmt.setDouble(3, doubleValue);
        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.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("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();/*from  ww w . java2  s  . co m*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

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

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

From source file:Main.java

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

    conn.setAutoCommit(true);/* w  ww  .ja  v  a 2s .c om*/

    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();
    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();
}

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, register int );");

    String sql = "INSERT INTO survey (register) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    pstmt.setBoolean(1, true);//from   w w w.  ja  v  a  2 s. co  m
    pstmt.executeUpdate();

    pstmt.setBoolean(1, false);
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getString(2));
    }
    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String db_file_name_prefix = "c:\\mydbdir\\mydb";

    Connection con = null;
    Class.forName("org.hsqldb.jdbcDriver");

    con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix, // filenames
            "sa", // username
            ""); // password

    Statement statement = con.createStatement();
    ResultSet rs = statement.executeQuery("SELECT * FROM \"User\"");
    while (rs.next()) {
        System.out.print("ID: " + rs.getString("ID"));
        System.out.print(" first name: " + rs.getString("firstname"));
        System.out.println(" last name: " + rs.getString("lastname"));
    }/*from w  ww .jav a 2s.com*/
    statement.close();
    con.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, register int );");

    String sql = "INSERT INTO survey (register) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    pstmt.setBoolean(1, true);//from   w  w w.j a  v a 2  s  .c  om
    pstmt.executeUpdate();

    pstmt.setBoolean(1, false);
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getString(2));
    }

    rs.close();
    stmt.close();
    conn.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 BINARY );");
    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();
    pstmt.setBytes(1, binaryData);// ww  w .  j  a va 2s .  c om
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(rs.getByte(1));
    }
    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

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

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,'Tom')");

    ResultSet rs = st.executeQuery("SELECT id,name FROM survey");

    // Delete the first row
    rs.first();//from   ww  w . j  a va 2 s. co  m
    rs.deleteRow();

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