List of usage examples for java.sql PreparedStatement close
void close() throws SQLException;
Statement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); 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')"); PreparedStatement pstmt = null; ParameterMetaData paramMetaData = null; String query = "select * from survey where id > ? "; System.out.println("conn=" + conn); pstmt = conn.prepareStatement(query); paramMetaData = pstmt.getParameterMetaData(); if (paramMetaData == null) { System.out.println("db vendor does NOT support ParameterMetaData"); } else {//from ww w.jav a 2 s.c o m System.out.println("db vendor supports ParameterMetaData"); // find out the number of dynamic parameters int paramCount = paramMetaData.getParameterCount(); System.out.println("paramCount=" + paramCount); } pstmt.close(); conn.close(); }
From source file:InsertTextFileToOracle.java
public static void main(String[] args) throws Exception { String id = "001"; String fileName = "fileName.txt"; FileInputStream fis = null;/*from w w w .j a v a2 s . co m*/ PreparedStatement pstmt = null; Connection conn = null; try { conn = getConnection(); conn.setAutoCommit(false); File file = new File(fileName); fis = new FileInputStream(file); pstmt = conn.prepareStatement("insert into DataFiles(id, fileName, fileBody) values (?, ?, ?)"); pstmt.setString(1, id); pstmt.setString(2, fileName); pstmt.setAsciiStream(3, fis, (int) file.length()); pstmt.executeUpdate(); conn.commit(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); e.printStackTrace(); } finally { pstmt.close(); fis.close(); conn.close(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null;//w w w .j a v a 2 s .c om PreparedStatement pstmt = null; java.sql.Array sqlArray = null; conn = getOracleConnection(); // ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("CHAR_ARRAY", conn); String[] content = { "v1", "v2", "v3", "v4" }; // sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content); String query = "insert into CHAR_ARRAY_TABLE(id, array) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); pstmt.setArray(2, sqlArray); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); System.out.println("--Demo_PreparedStatement_SetArray end--"); pstmt.close(); conn.close(); }
From source file:DemoPreparedStatementSetIntegers.java
public static void main(String[] args) throws Exception { String id = "0001"; byte byteValue = 1; short shortValue = 1; int intValue = 12345; long longValue = 100000000L; Connection conn = null;// www. j a v a2 s .c om PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into integer_table(id, byte_column, " + "short_column, int_column, long_column) values(?, ?, ?, ?, ?)"; // create PrepareStatement object pstmt = conn.prepareStatement(query); pstmt.setString(1, id); pstmt.setByte(2, byteValue); pstmt.setShort(3, shortValue); pstmt.setInt(4, intValue); pstmt.setLong(5, longValue); // 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 = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("create view surveyView as (select * from survey);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); String query = "select * from survey where id > ? and name = ?"; PreparedStatement pstmt = conn.prepareStatement(query); ParameterMetaData paramMetaData = pstmt.getParameterMetaData(); if (paramMetaData == null) { System.out.println("db vendor does NOT support ParameterMetaData"); } else {/*from w ww .ja v a 2s .co m*/ System.out.println("db vendor supports ParameterMetaData"); int paramCount = paramMetaData.getParameterCount(); System.out.println("paramCount=" + paramCount); System.out.println("-------------------"); for (int param = 1; param <= paramCount; param++) { System.out.println("param number=" + param); System.out.println(paramMetaData.getPrecision(param)); } } pstmt.close(); conn.close(); }
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 w ww . ja va2s . co 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 = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("create view surveyView as (select * from survey);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); String query = "select * from survey where id > ? and name = ?"; PreparedStatement pstmt = conn.prepareStatement(query); ParameterMetaData paramMetaData = pstmt.getParameterMetaData(); if (paramMetaData == null) { System.out.println("db vendor does NOT support ParameterMetaData"); } else {//w w w.java2 s. co m System.out.println("db vendor supports ParameterMetaData"); int paramCount = paramMetaData.getParameterCount(); System.out.println("paramCount=" + paramCount); System.out.println("-------------------"); for (int param = 1; param <= paramCount; param++) { System.out.println("param number=" + param); System.out.println(paramMetaData.getParameterClassName(param)); } } pstmt.close(); conn.close(); }
From source file:DemoPreparedStatementSetAsciiStream.java
public static void main(String[] args) { Connection conn = null;//w w w . java 2 s . c o m PreparedStatement pstmt = null; String query = null; try { conn = getConnection(); String fileName = "fileName.txt"; File file = new File(fileName); int fileLength = (int) file.length(); InputStream stream = (InputStream) new FileInputStream(file); query = "insert into LONG_VARCHAR_TABLE(id, stream) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, fileName); pstmt.setAsciiStream(2, stream, fileLength); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); } catch (Exception e) { e.printStackTrace(); } finally { try { pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:DemoPreparedStatementSetTimeAndTimestamp.java
public static void main(String[] args) throws Exception { String id = "0001"; Connection conn = null;/* w w w . j a va 2s. c o m*/ PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into time_table(id,time_column, timestamp_column) values(?, ?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, id); java.sql.Time time = getCurrentJavaSqlTime(); System.out.println("time=" + time); pstmt.setTime(2, time); java.sql.Timestamp timestamp = getCurrentJavaSqlTimestamp(); System.out.println("timestamp=" + timestamp); pstmt.setTimestamp(3, timestamp); // 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:GetDateFromOracle.java
public static void main(String args[]) { String GET_RECORD = "select date_column, time_column, " + "timestamp_column from TestDates where id = ?"; ResultSet rs = null;// ww w .j ava2 s . co m Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); pstmt = conn.prepareStatement(GET_RECORD); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); while (rs.next()) { java.sql.Date dbSqlDate = rs.getDate(1); java.sql.Time dbSqlTime = rs.getTime(2); java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3); System.out.println("dbSqlDate=" + dbSqlDate); System.out.println("dbSqlTime=" + dbSqlTime); System.out.println("dbSqlTimestamp=" + dbSqlTimestamp); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }