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 = getConnection();
    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.executeUpdate("insert into survey (id,name ) values (2,null)");
    st = conn.createStatement();//www  .ja v  a  2 s .  c o m
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    // extract data from the ResultSet
    while (rs.next()) {
        int id = rs.getInt(1);
        System.out.println("id=" + id);
        String name = rs.getString(2);
        System.out.println("name=" + name);
        if (rs.wasNull()) {
            System.out.println("name is null");
        } else {
            System.out.println("name is not null");
        }
        System.out.println("---------------");
    }
    rs.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();

    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 = conn.createStatement();//from  ww w . ja v a 2 s .  co m
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    // extract data from the ResultSet
    while (rs.next()) {
        int id = rs.getInt("id");
        System.out.println("id=" + id);
        String name = rs.getString(2);
        System.out.println("name=" + name);
        if (rs.wasNull()) {
            System.out.println("name is null");
        } else {
            System.out.println("name is not null");
        }
        System.out.println("---------------");
    }
    rs.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();

    st.executeUpdate("create table images (Id int, b BLOB);");

    File file = new File("myimage.gif");
    FileInputStream fis = new FileInputStream(file);
    PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)");
    ps.setString(1, "10");
    ps.setBinaryStream(2, fis);//  w ww  . j  a v  a2  s. co  m
    ps.executeUpdate();

    ResultSet rset = st.executeQuery("select b from images");
    InputStream stream = rset.getBinaryStream(1);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int a1 = stream.read();
    while (a1 >= 0) {
        output.write((char) a1);
        a1 = stream.read();
    }
    Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
    output.close();

    ps.close();

    fis.close();
    st.close();
    conn.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')");

    st = conn.createStatement();//w  w w  . j ava 2  s .  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);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);

        // indicates the nullability of values
        // in the designated column.
        System.out.println(rsMetaData.isNullable(i));
    }

    st.close();
    conn.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')");

    st = conn.createStatement();/*from w w  w  .j ava2  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);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);

        // indicates whether a write on the designated
        // column will succeed.
        System.out.println(rsMetaData.isWritable(i));
    }

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

From source file:MainClass.java

public static void main(String args[]) {
    Connection conn = null;/*  ww  w . j a v a2  s.co m*/
    Statement stmt = null;
    try {
        conn = getConnection();
        stmt = conn.createStatement();
        stmt.executeUpdate(EMPLOYEE_TABLE);
        stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(100, 'A')");
        stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(200, 'B')");
        System.out.println("CreateEmployeeTableOracle: main(): table created.");
    } catch (ClassNotFoundException e) {
        System.out.println("error: failed to load Oracle driver.");
        e.printStackTrace();
    } catch (SQLException e) {
        System.out.println("error: failed to create a connection object.");
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("other error:");
        e.printStackTrace();
    } finally {
        try {
            stmt.close();
            conn.close();
        } catch (Exception e) {
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Connection conn = null;/*from ww w. ja v  a  2  s  . com*/
    Statement stmt = null;
    try {
        conn = getConnection();
        stmt = conn.createStatement();
        stmt.executeUpdate(EMPLOYEE_TABLE);
        stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(100, 'A')");
        stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(200, 'B')");
        System.out.println("CreateEmployeeTableMySQL: main(): table created.");
    } catch (ClassNotFoundException e) {
        System.out.println("error: failed to load MySQL driver.");
        e.printStackTrace();
    } catch (SQLException e) {
        System.out.println("error: failed to create a connection object.");
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("other error:");
        e.printStackTrace();
    } finally {
        try {
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

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')");

    st = conn.createStatement();/* www  .  j a v  a2  s  .  c om*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

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

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);

        // indicates whether values in the designated
        // column are signed numbers.
        System.out.println(rsMetaData.isSigned(i));
    }

    st.close();
    conn.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')");

    st = conn.createStatement();//  w w  w  .ja va  2s . c  om
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

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

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);

        // Indicates whether the designated column
        // is definitely not writable.
        System.out.println(rsMetaData.isReadOnly(i));
    }

    st.close();
    conn.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')");

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

    ResultSetMetaData rsMetaData = rs.getMetaData();

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

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);

        // Indicates whether a column's case matters
        // in the designated column.
        System.out.println(rsMetaData.isSearchable(i));
    }

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