Example usage for java.sql Statement close

List of usage examples for java.sql Statement close

Introduction

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

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/* w w w .  j a  va2 s .  com*/
    Statement stmt = null;

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Deleting database...");
    stmt = conn.createStatement();

    conn = DriverManager.getConnection(DB_URL, USER, PASS);

    stmt = conn.createStatement();

    String sql = "DELETE FROM Person WHERE id = 1";
    stmt.executeUpdate(sql);
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Statement stmt = DriverManager.getConnection("jdbc:odbc:employee").createStatement();
    ResultSet rs = stmt.executeQuery("select lastname, firstname, id from [Sheet1$]");
    while (rs.next()) {
        String lname = rs.getString(1);
        String fname = rs.getString(2);
        int id = rs.getInt(3);
        System.out.println(fname + " " + lname + "  id : " + id);
    }//from  w  w w  . j a  v  a2 s  .c om
    rs.close();
    stmt.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    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')");

    DatabaseMetaData meta = conn.getMetaData();
    String sqlKeywords = meta.getSQLKeywords();
    System.out.println(sqlKeywords);

    st.close();
    conn.close();/*  w  w  w.  j a  v  a  2s . c om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {// www  .  j  a  va2  s  .c o m
        Connection conn = getHSQLConnection();

        conn.setAutoCommit(false);
        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");

        outputResultSet(rs);

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException sqle) {
        do { // loop through each exception
             // do something with each exception
            System.err.println("Exception occurred:\nMessage: " + sqle.getMessage());
            System.err.println("SQL state: " + sqle.getSQLState());
            System.err.println("Vendor code: " + sqle.getErrorCode() + "\n----------------");
        } while ((sqle = sqle.getNextException()) != null);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);/* www  . j a v a  2s. c  o  m*/
    Statement stmt = conn.createStatement();

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

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

    // since there were no errors, commit
    conn.commit();

    ResultSet 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 {
    try {//from  w  w  w .java2 s .c om
        Connection conn = getHSQLConnection();

        conn.setAutoCommit(false);
        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");

        outputResultSet(rs);

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException sqle) {
        String sqlMessage = sqle.getMessage();
        String sqlState = sqle.getSQLState();
        int vendorCode = sqle.getErrorCode();
        System.err.println("Exception occurred:");
        System.err.println("Message: " + sqlMessage);
        System.err.println("SQL state: " + sqlState);
        System.err.println("Vendor code: " + vendorCode + "\n----------------");
    }
}

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");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setAsciiStream(1, fis, (int) file.length());
    pstmt.execute();//w  w  w  . j  av a  2  s.co m

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

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbname";
    Connection dbConn = DriverManager.getConnection(URL, "user", "passw");
    Employee employee = new Employee(42, "AA", 9);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(employee);/*ww  w. j a v a  2  s  .  c  om*/
    byte[] employeeAsBytes = baos.toByteArray();
    PreparedStatement pstmt = dbConn.prepareStatement("INSERT INTO EMPLOYEE (emp) VALUES(?)");
    ByteArrayInputStream bais = new ByteArrayInputStream(employeeAsBytes);
    pstmt.setBinaryStream(1, bais, employeeAsBytes.length);
    pstmt.executeUpdate();
    pstmt.close();
    Statement stmt = dbConn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT emp FROM Employee");
    while (rs.next()) {
        byte[] st = (byte[]) rs.getObject(1);
        ByteArrayInputStream baip = new ByteArrayInputStream(st);
        ObjectInputStream ois = new ObjectInputStream(baip);
        Employee emp = (Employee) ois.readObject();
    }
    stmt.close();
    rs.close();
    dbConn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);/*from w w  w  . j  a  v  a  2s.co m*/
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myURL CHAR);");
    st.executeUpdate("insert into survey(id) values(01)");
    st.executeUpdate("insert into survey(id) values(02)");

    Savepoint mySavepoint = conn.setSavepoint("MYSAVEPOINT");

    st.executeUpdate("insert into survey(id) values(03)");
    conn.commit();

    conn.rollback(mySavepoint);

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

From source file:Main.java

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

    Connection conn = getHSQLConnection();

    conn.setAutoCommit(false);//from  w ww.  j  av  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");

    outputResultSet(rs);

    checkForWarning(rs.getWarnings());

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

}