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

    DatabaseMetaData mtdt = conn.getMetaData();
    System.out.println("ANSI92 Entry Level: " + mtdt.supportsANSI92EntryLevelSQL());
    System.out.println("ANSI92 Intermediate: " + mtdt.supportsANSI92IntermediateSQL());
    System.out.println("ANSI92 Full SQL: " + mtdt.supportsANSI92FullSQL());
    System.out.println("Minimum SQL Grammar: " + mtdt.supportsMinimumSQLGrammar());
    System.out.println("Core SQL Grammar: " + mtdt.supportsCoreSQLGrammar());
    System.out.println("Extended SQL Grammar: " + mtdt.supportsExtendedSQLGrammar());

    conn.close();
}

From source file:UpdateableRs.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);//from w  w  w  .j ava 2  s  .  com

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble("salary") * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.close();
}

From source file:Main.java

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

    DatabaseMetaData meta = conn.getMetaData();
    try {/* w w w  .  ja va  2  s  . c o  m*/
        int majorVersion = meta.getDatabaseMajorVersion();
        System.out.println("major Version:" + majorVersion);
    } catch (Exception e) {
        System.out.println("major Version:unsupported feature");
    }

    try {
        int minorVersion = meta.getDatabaseMinorVersion();
        System.out.println("minorVersion:" + minorVersion);
    } catch (Exception e) {
        System.out.println("minorVersion unsupported feature");
    }

    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://192.168.100.100:3306/";
    String dbName = "databaseName";
    Statement stmt = null;//from   ww w .ja va  2  s.c  o  m
    ResultSet result = null;
    String driver = "com.mysql.jdbc.Driver";
    String databaseUserName = "admin";
    String databasePassword = "root";
    Class.forName(driver).newInstance();
    Connection conn = DriverManager.getConnection(url + dbName, databaseUserName, databasePassword);
    stmt = conn.createStatement();
    result = null;
    String password, username;
    result = stmt.executeQuery("select * from userTable where username ='user1' ");
    if (!result.isBeforeFirst()) {
        System.out.println("resultset contin no rows");
    }
    while (result.next()) {
        username = result.getString("username");
        password = result.getString("password");
        System.out.println(username + "  " + password);
    }
    conn.close();
}

From source file:UpdateRecordsUsingPreparedStatement.java

public static void main(String[] args) throws Exception {
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {/*from   w w  w.  jav  a 2 s  .  c  om*/
        conn = getConnection();
        String query = "update dept set DEPT_LOC = ? where DEPT_NUM = ? ";
        pstmt = conn.prepareStatement(query); // create a statement
        pstmt.setString(1, "deptLocation"); // set input parameter 1
        pstmt.setInt(2, 1001); // set input parameter 2
        pstmt.executeUpdate(); // execute update statement
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:ResultSetUpdate.java

public static void main(String args[]) {

    String url;/*  w  ww  .java2 s .  com*/
    url = "jdbc:odbc:UserDB";

    String user, pass;
    user = "ian";
    pass = "stjklsq";

    Connection con;
    Statement stmt;
    ResultSet rs;

    try {
        con = DriverManager.getConnection(url, user, pass);
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        rs = stmt.executeQuery("SELECT * FROM Users where nick=\"ian\"");

        // Get the resultset ready, update the passwd field, commit
        rs.first();
        rs.updateString("password", "unguessable");
        rs.updateRow();

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

From source file:Example.java

public static void main(String args[]) {
    try {/*from   ww  w .j a  v  a 2  s.  c o m*/
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con = DriverManager.getConnection("jdbc:odbc:inventory", "", "");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM inventory ORDER BY price");

        ResultSetMetaData rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
        int rowCount = 1;
        while (rs.next()) {
            for (int i = 1; i <= numberOfColumns; i++) {
                System.out.print(rs.getString(i) + " ");
            }
            rowCount++;
        }
        stmt.close();
        con.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

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 DECIMAL, name BINARY );");

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

    pstmt.setBigDecimal(1, new BigDecimal("1.00000"));

    // insert the data
    pstmt.executeUpdate();/*from  ww w .ja  va2s.  c  o  m*/

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

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

From source file:DemoPreparedStatementSetDate.java

public static void main(String[] args) throws Exception {
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {/*from  ww w .jav  a2  s  . co  m*/
        conn = getConnection();
        String query = "insert into date_table(id, date_column) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        java.sql.Date date = getCurrentJavaSqlDate();
        pstmt.setDate(2, date);

        // 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 = getConnection();
    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  .ja va 2s  .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();
}