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

    ResultSet rs = null;/*from  ww  w  .  j a  va 2  s  .com*/
    DatabaseMetaData meta = conn.getMetaData();
    // The Oracle database stores its table names as Upper-Case,
    // if you pass a table name in lowercase characters, it will not work.
    // MySQL database does not care if table name is uppercase/lowercase.
    //
    rs = meta.getPrimaryKeys(null, null, "survey");

    java.util.List list = new java.util.ArrayList();
    while (rs.next()) {
        String columnName = rs.getString("COLUMN_NAME");
        System.out.println("getPrimaryKeys(): columnName=" + columnName);
    }

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

From source file:MainClass.java

public static void main(String args[]) {
    Connection conn = null;
    Statement stmt = null;//from  w ww.j  a v  a2  s  .  c o  m
    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;
    Statement stmt = null;/*  w ww  .  j a  v a  2  s.  c o  m*/
    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 = null;
    PreparedStatement pstmt = null;
    try {//  w w  w  . j  a v a  2 s  .c o  m
        conn = getConnection();
        String query = "insert into message values(?, ?, ?)";

        pstmt = conn.prepareStatement(query); // create a statement
        pstmt.setInt(1, 5); // set input parameter 1
        pstmt.setString(2, "head5"); // set input parameter 2
        pstmt.setString(3, "data5"); // set input parameter 3
        pstmt.executeUpdate(); // execute insert statement
        pstmt = conn.prepareStatement("insert into msgtag values(?, ?, ?)"); // create a statement
        pstmt.setInt(1, 55); // set input parameter 1
        pstmt.setString(2, "tag5"); // set input parameter 2
        pstmt.setInt(3, 5); // set input parameter 3
        pstmt.executeUpdate(); // execute insert statement
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:Update.java

public static void main(String args[]) {
    Connection con = null;

    if (args.length != 2) {
        System.out.println("Syntax: <java UpdateApp [number] [string]>");
        return;//from  ww w .j ava  2  s  . c o  m
    }
    try {
        String driver = "com.imaginary.sql.msql.MsqlDriver";

        Class.forName(driver).newInstance();
        String url = "jdbc:msql://carthage.imaginary.com/ora";
        con = DriverManager.getConnection(url, "borg", "");
        Statement s = con.createStatement();
        String test_id = args[0];
        String test_val = args[1];
        int update_count = s.executeUpdate(
                "INSERT INTO test (test_id, test_val) " + "VALUES(" + test_id + ", '" + test_val + "')");

        System.out.println(update_count + " rows inserted.");
        s.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (con != null) {
            try {
                con.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();//from w w  w  . j  a  va2  s.com
    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);
        // get the designated column's name.
        System.out.println(rsMetaData.getColumnLabel(i));

    }

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

From source file:GetNumberOfRowsScrollableResultSet_MySQL.java

public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;//from w  w w  .j  ava2 s . com
    ResultSet rs = null;
    try {
        conn = getConnection();
        String query = "select id from employees";
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        rs = stmt.executeQuery(query);
        // extract data from the ResultSet scroll from top
        while (rs.next()) {
            String id = rs.getString(1);
            System.out.println("id=" + id);
        }
        // move to the end of the result set
        rs.last();
        // get the row number of the last row which is also the row count
        int rowCount = rs.getRow();
        System.out.println("rowCount=" + rowCount);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            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();/*w w  w  . jav a  2  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);

        // get the designated column's SQL type.
        System.out.println(rsMetaData.getColumnType(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);

        // get the designated column's table name.
        System.out.println(rsMetaData.getTableName(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();//www.  j  av a 2  s  .  com
    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);

        // get the designated column's class name.
        System.out.println(rsMetaData.getColumnClassName(i));
    }

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