List of usage examples for java.sql PreparedStatement executeUpdate
int executeUpdate() throws SQLException;
PreparedStatement
object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT
, UPDATE
or DELETE
; or an SQL statement that returns nothing, such as a DDL statement. From source file:Main.java
public static void main(String args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String URL = "jdbc:odbc:dbname"; Connection dbConn = DriverManager.getConnection(URL, "user", "passw"); Employee employee = new Employee(42, "AA", 9); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(employee);//from w w w .j a v a2 s . c o m byte[] employeeAsBytes = baos.toByteArray(); PreparedStatement pstmt = dbConn.prepareStatement("INSERT INTO EMPLOYEE (emp) VALUES(?)"); ByteArrayInputStream bais = new ByteArrayInputStream(employeeAsBytes); pstmt.setBinaryStream(1, bais, employeeAsBytes.length); pstmt.executeUpdate(); pstmt.close(); Statement stmt = dbConn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT emp FROM Employee"); while (rs.next()) { byte[] st = (byte[]) rs.getObject(1); ByteArrayInputStream baip = new ByteArrayInputStream(st); ObjectInputStream ois = new ObjectInputStream(baip); Employee emp = (Employee) ois.readObject(); } stmt.close(); rs.close(); dbConn.close(); }
From source file:CreateTableAllTypesInOracle.java
public static void main(String[] args) { PreparedStatement pstmt = null; Connection conn = null;//w w w. ja va 2 s . c om try { conn = getConnection(); pstmt = conn.prepareStatement("CREATE TYPE varray_type is VARRAY(5) OF VARCHAR(10)"); pstmt.executeUpdate(); // Create an OBJECT type pstmt = conn.prepareStatement( "CREATE TYPE oracle_object is OBJECT(column_string VARCHAR(128), column_integer INTEGER)"); pstmt.executeUpdate(); StringBuffer allTypesTable = new StringBuffer("CREATE TABLE oracle_all_types("); // Column Name Oracle Type Java Type allTypesTable.append("column_short SMALLINT, "); // short allTypesTable.append("column_int INTEGER, "); // int allTypesTable.append("column_float REAL, "); // float; can also be NUMBER allTypesTable.append("column_double DOUBLE PRECISION, "); // double; can also be FLOAT or NUMBER allTypesTable.append("column_bigdecimal DECIMAL(13,0), "); // BigDecimal allTypesTable.append("column_string VARCHAR2(254), "); // String; can also be CHAR(n) allTypesTable.append("column_characterstream LONG, "); // CharacterStream or AsciiStream allTypesTable.append("column_bytes RAW(2000), "); // byte[]; can also be LONG RAW(n) allTypesTable.append("column_binarystream RAW(2000), "); // BinaryStream; can also be LONG RAW(n) allTypesTable.append("column_timestamp DATE, "); // Timestamp allTypesTable.append("column_clob CLOB, "); // Clob allTypesTable.append("column_blob BLOB, "); // Blob; can also be BFILE allTypesTable.append("column_bfile BFILE, "); // oracle.sql.BFILE allTypesTable.append("column_array varray_type, "); // oracle.sql.ARRAY allTypesTable.append("column_object oracle_object)"); // oracle.sql.OBJECT pstmt.executeUpdate(allTypesTable.toString()); } catch (Exception e) { // creation of table failed. // handle the exception e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/* w w w. jav a 2 s .com*/ String url = "jdbc:odbc:databaseName"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); String changeLastName = "UPDATE authors SET lastname = ? WHERE authid = ?"; PreparedStatement updateLastName = connection.prepareStatement(changeLastName); updateLastName.setString(1, "Martin"); // Set lastname placeholder value updateLastName.setInt(2, 4); // Set author ID placeholder value int rowsUpdated = updateLastName.executeUpdate(); // execute the update System.out.println("Rows affected: " + rowsUpdated); connection.close(); } catch (ClassNotFoundException cnfe) { System.err.println(cnfe); } catch (SQLException sqle) { System.err.println(sqle); } }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/* ww w . j a v a 2 s .c o m*/ Connection conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myDate DATE );"); String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime()); pstmt.setDate(2, sqlDate); pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); rs.close(); st.close(); conn.close(); } catch (SQLException e) { while (e != null) { String errorMessage = e.getMessage(); System.err.println("sql error message:" + errorMessage); // This vendor-independent string contains a code. String sqlState = e.getSQLState(); System.err.println("sql state:" + sqlState); int errorCode = e.getErrorCode(); System.err.println("error code:" + errorCode); // String driverName = conn.getMetaData().getDriverName(); // System.err.println("driver name:"+driverName); // processDetailError(drivername, errorCode); e = e.getNextException(); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getOracleConnection(); String[] columnNames = { "id", "name", "content", "date_created" }; Object[] inputValues = new Object[columnNames.length]; inputValues[0] = new java.math.BigDecimal(100); inputValues[1] = new String("String Value"); inputValues[2] = new String("This is my resume."); inputValues[3] = new Timestamp((new java.util.Date()).getTime()); String insert = "insert into resume (id, name, content, date_created ) values(?, ?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(insert); pstmt.setObject(1, inputValues[0]);//from ww w. ja va2s .co m pstmt.setObject(2, inputValues[1]); pstmt.setObject(3, inputValues[2]); pstmt.setObject(4, inputValues[3]); pstmt.executeUpdate(); String query = "select id, name, content, date_created from resume where id=?"; PreparedStatement pstmt2 = conn.prepareStatement(query); pstmt2.setObject(1, inputValues[0]); ResultSet rs = pstmt2.executeQuery(); Object[] outputValues = new Object[columnNames.length]; if (rs.next()) { for (int i = 0; i < columnNames.length; i++) { outputValues[i] = rs.getObject(i + 1); } } System.out.println("id=" + ((java.math.BigDecimal) outputValues[0]).toString()); System.out.println("name=" + ((String) outputValues[1])); System.out.println("content=" + ((Clob) outputValues[2])); System.out.println("date_created=" + ((java.sql.Date) outputValues[3]).toString()); rs.close(); pstmt.close(); pstmt2.close(); conn.close(); }
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); // create some binary data String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); // set value for the prepared statement pstmt.setBytes(1, binaryData);//from w w w . j a v a 2s . co m // insert the data pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getBytes(2).length + " "); } rs.close(); stmt.close(); conn.close(); }
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); // create some binary data String myData = "some string data ..."; byte[] binaryData = myData.getBytes(); // set value for the prepared statement pstmt.setBytes(1, binaryData);//from w ww . ja v a2 s .c o m // insert the data pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(new String(rs.getBytes(2))); } 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(); st.executeUpdate("create table survey (id int,myDate DATE);"); String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime()); pstmt.setDate(2, sqlDate);/* ww w . j a v a 2s.c om*/ pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); System.out.print("\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.getColumnLabel(i)); } System.out.print("\nAuto Increment\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isAutoIncrement(i)); } for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isCaseSensitive(i)); } System.out.print("\nSearchable\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isSearchable(i)); } System.out.print("\nCurrency\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isCurrency(i)); } System.out.print("\nAllows nulls\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isNullable(i)); } System.out.print("\nSigned\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isSigned(i)); } System.out.print("\nRead only\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isReadOnly(i)); } System.out.print("\nWritable\t"); for (int i = 1; i <= numCols; i++) { System.out.print(rsmd.isWritable(i)); } System.out.print("\nDefinitely Writable\t"); for (int i = 1; i <= numCols; i++) { System.out.println(rsmd.isDefinitelyWritable(i)); } conn.close(); }
From source file:DemoPreparedStatementSetBoolean.java
public static void main(String[] args) throws Exception { boolean booleanValue = true; Connection conn = null;// www . ja v a 2s.com PreparedStatement pstmt = null; try { conn = getConnection(); String query = "insert into boolean_table(id, boolean_column) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); pstmt.setBoolean(2, booleanValue); 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 stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name BINARY);"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); File file = new File("yourFileName.txt"); long fileLength = file.length(); Reader fileReader = (Reader) new BufferedReader(new FileReader(file)); pstmt.setCharacterStream(1, fileReader, (int) fileLength); int rowCount = pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getBytes(2)); }/* w ww .j a va 2s.c o m*/ rs.close(); stmt.close(); conn.close(); }