List of usage examples for java.sql Connection close
void close() throws SQLException;
Connection
object's database and JDBC resources immediately instead of waiting for them to be automatically released. From source file:InsertCustomType2_Oracle.java
public static void main(String[] args) { String id = "001"; String isbn = "1234567890"; String title = "Java Oracle"; String author = "java2s"; int edition = 1; // create the Book object Book book = new Book(isbn, title, author, edition); book.print();/*from www . j a va 2s . c o m*/ Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); // create type map java.util.Map map = conn.getTypeMap(); System.out.println("map=" + map); map.put("BOOK", Class.forName("Book")); System.out.println("map=" + map); String insert = "insert into book_table(ID, BOOK) values(?, ?)"; pstmt = conn.prepareStatement(insert); pstmt.setString(1, id); pstmt.setObject(2, book); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { try { 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(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 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 to the row to update rs.first();// www . ja v a 2 s .com // Update the value of column column_1 on that row rs.updateString(2, "new data"); // Update the row; if autocommit is enabled, // update is committed rs.updateRow(); rs.close(); st.close(); conn.close(); }
From source file:ConnPool.java
public static void main(String[] args) throws Exception { OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource(); ocpds.setURL("jdbc:oracle:thin:@localhost:1521:ORCL"); ocpds.setUser("user"); ocpds.setPassword("password"); PooledConnection pc_1 = ocpds.getPooledConnection(); Connection conn_1 = pc_1.getConnection(); Statement stmt = conn_1.createStatement(); ResultSet rs = stmt.executeQuery("SELECT count(*) FROM v$session WHERE username = 'SYS'"); rs.next();/* w ww. ja v a 2 s . c o m*/ String msg = "Total connections after "; System.out.println(msg + "conn_1: " + rs.getString(1)); Connection conn_2 = pc_1.getConnection(); stmt = conn_2.createStatement(); rs = stmt.executeQuery("SELECT count(*) FROM v$session WHERE username = 'SYS'"); rs.next(); System.out.println(msg + "conn_2: " + rs.getString(1)); PooledConnection pc_2 = ocpds.getPooledConnection(); rs = stmt.executeQuery("SELECT count(*) FROM v$session WHERE username = 'SYS'"); rs.next(); System.out.println(msg + "pc_2: " + rs.getString(1)); conn_1.close(); conn_2.close(); pc_1.close(); pc_2.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_UPDATABLE); 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 to the row to update rs.first();// w ww. j a va 2 s .c o m // Update the value of column column_1 on that row rs.updateString("name", "new data"); // Update the row; if autocommit is enabled, // update is committed rs.updateRow(); 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(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 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')"); st = conn.createStatement();/* w w w. ja v a 2 s . c o m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { String name = rs.getString(2); if (rs.wasNull()) { System.out.println("was NULL"); } else { System.out.println("not NULL"); } } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/*w ww. ja va2 s. c o m*/ String url = "jdbc:odbc:yourdatabasename"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP"; ResultSet rs = stmt.executeQuery(sqlQuery); rs.last(); // Move the cursor backwards through the ResultSet while (rs.previous()) { String nbr = rs.getString(1); String name = rs.getString(2); String job = rs.getString(3); String mgr = rs.getString(4); Timestamp hireDate = rs.getTimestamp(5); System.out.println(name); } rs.close(); stmt.close(); connection.close(); } catch (Exception e) { System.err.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 int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); // prepare small binary stream File smallFile = new File("yourFileName.txt"); int smallFileLength = (int) smallFile.length(); InputStream smallStream = (InputStream) new FileInputStream(smallFile); pstmt.setBinaryStream(2, smallStream, smallFileLength); // insert the data pstmt.executeUpdate();// w ww . ja v a 2s.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: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"); rs.last();/*from ww w. ja va 2s . c o m*/ // Move cursor up 2 rows from the current row. If this moves // cursor beyond the first row, cursor is put before the first row rs.relative(-2); // Get data at cursor String id = rs.getString("id"); System.out.println(id); 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(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 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')"); st = conn.createStatement();//from w w w .ja v a2s .c om ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { int id = rs.getInt(1); // index 1 is the "id" column String name = rs.getString(2); // index 2 is the "name" column System.out.println(id); System.out.println(name); } rs.close(); st.close(); conn.close(); }
From source file:ScrollableRs.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd"); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES"); while (rs.next()) { printRow(rs);//from w ww. j a v a 2 s . c om } rs.afterLast(); System.out.println("\"After-last-row\" = " + rs.isAfterLast()); rs.beforeFirst(); System.out.println("\"Before-first-row\" = " + rs.isBeforeFirst()); rs.first(); printRow(rs); rs.last(); printRow(rs); rs.previous(); printRow(rs); rs.next(); printRow(rs); rs.absolute(3); printRow(rs); rs.relative(-2); printRow(rs); if (conn != null) conn.close(); }