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 {
    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);

    // prepare text stream
    File file = new File("yourFileName.txt");
    int fileLength = (int) file.length();
    InputStream stream = (InputStream) new FileInputStream(file);

    pstmt.setString(1, "001");
    pstmt.setAsciiStream(2, stream, fileLength);

    // insert the data
    pstmt.executeUpdate();/*from   w w w  .  jav  a2 s  .c  o m*/

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(new String(rs.getBytes(2)));
    }

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

From source file:DemoPreparedStatementSetAsciiStream.java

public static void main(String[] args) {
    Connection conn = null;
    PreparedStatement pstmt = null;
    String query = null;/*w  ww .j a v  a 2  s  .  c om*/
    try {
        conn = getConnection();
        String fileName = "fileName.txt";
        File file = new File(fileName);
        int fileLength = (int) file.length();
        InputStream stream = (InputStream) new FileInputStream(file);

        query = "insert into  LONG_VARCHAR_TABLE(id, stream) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, fileName);
        pstmt.setAsciiStream(2, stream, fileLength);

        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:DemoPreparedStatementSetTimeAndTimestamp.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {/*w w w. ja va2s. co m*/
        conn = getConnection();
        String query = "insert into time_table(id,time_column, timestamp_column) values(?, ?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        java.sql.Time time = getCurrentJavaSqlTime();
        System.out.println("time=" + time);
        pstmt.setTime(2, time);
        java.sql.Timestamp timestamp = getCurrentJavaSqlTimestamp();
        System.out.println("timestamp=" + timestamp);
        pstmt.setTimestamp(3, timestamp);
        // 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();

    conn.setAutoCommit(false);// w w w.j a  v  a2 s  .c o m
    Statement st = conn.createStatement();
    try {
        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,'nameValue')");
        // commits all the transactions
        conn.commit();
    } catch (Exception e) {
        //cancel (roll back) all the transactions
        conn.rollback();
        // to see what went wrong
        e.printStackTrace();
    }

    st = conn.createStatement();
    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();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    JdbcRowSet jdbcRS = new JdbcRowSetImpl(conn);
    jdbcRS.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
    String sql = "SELECT * FROM survey";
    jdbcRS.setCommand(sql);//from   ww  w  .  j a v a2 s.c  o m
    jdbcRS.execute();
    jdbcRS.addRowSetListener(new ExampleListener());

    while (jdbcRS.next()) {
        System.out.println("id=" + jdbcRS.getString(1));
        System.out.println("name=" + jdbcRS.getString(2));
    }
    conn.close();
}

From source file:Main.java

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

    conn.setAutoCommit(false);/*from   www  . j a v  a 2 s  . com*/
    Statement st = conn.createStatement();
    try {
        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,'nameValue')");
        // commits all the transactions
        conn.commit();
    } catch (Exception e) {
        // cancel (roll back) all the transactions
        conn.rollback();
        // to see what went wrong
        e.printStackTrace();
    }

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

    outputResultSet(rs);

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

From source file:GetDateFromOracle.java

public static void main(String args[]) {
    String GET_RECORD = "select date_column, time_column, " + "timestamp_column from TestDates where id = ?";
    ResultSet rs = null;/* www.j  a va 2s.c  o  m*/
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(GET_RECORD);
        pstmt.setString(1, "0001");
        rs = pstmt.executeQuery();
        while (rs.next()) {
            java.sql.Date dbSqlDate = rs.getDate(1);
            java.sql.Time dbSqlTime = rs.getTime(2);
            java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);
            System.out.println("dbSqlDate=" + dbSqlDate);
            System.out.println("dbSqlTime=" + dbSqlTime);
            System.out.println("dbSqlTimestamp=" + dbSqlTimestamp);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

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, b CLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

    File file = new File("c:/Java_Dev/data.txt");

    FileReader reader = new FileReader(file);
    pstmt.setCharacterStream(1, reader);

    pstmt.execute();//from  w w  w.j a v a 2  s .c o  m

    ResultSet resultSet = pstmt.executeQuery("select b from survey ");

    File data = new File("C:\\a.txt");
    Reader dataReader = resultSet.getCharacterStream(1);
    FileWriter writer = new FileWriter(data);
    char[] buffer = new char[1];
    while (dataReader.read(buffer) > 0) {
        writer.write(buffer);
    }
    writer.close();

    reader.close();
    st.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_READ_ONLY);

    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 * FROM survey");
    // Move cursor forward
    while (rs.next()) {
        String id = rs.getString("id");
        System.out.println(id);/* w  w  w.j  a  v a  2 s  .  c  o  m*/
    }
    // Move cursor to the first row
    rs.first();
    // Get data at cursor
    String id = rs.getString("id");
    System.out.println(id);

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

From source file:CreateStores.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;
    String createTable;/* w w  w .  j  a  va 2  s  .c o  m*/
    String createArray;
    createArray = "CREATE TYPE COF_ARRAY AS ARRAY(10) OF VARCHAR(40)";
    createTable = "CREATE TABLE STORES ( " + "STORE_NO INTEGER, LOCATION ADDRESS, "
            + "COF_TYPES COF_ARRAY, MGR REF MANAGER )";
    Statement stmt;

    try {
        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {
        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        stmt = con.createStatement();

        stmt.executeUpdate(createArray);
        stmt.executeUpdate(createTable);

        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}