List of usage examples for java.sql Connection prepareStatement
PreparedStatement prepareStatement(String sql) throws SQLException;
PreparedStatement
object for sending parameterized SQL statements to the database. From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db"); Statement stat = conn.createStatement(); stat.executeUpdate("drop table if exists people;"); stat.executeUpdate("create table people (name, occupation);"); PreparedStatement prep = conn.prepareStatement("insert into people values (?, ?);"); prep.setString(1, "G"); prep.setString(2, "politics"); prep.addBatch();/*from ww w. ja v a 2 s . c om*/ prep.setString(1, "Turing"); prep.setString(2, "computers"); prep.addBatch(); prep.setString(1, "W"); prep.setString(2, "Tester"); prep.addBatch(); conn.setAutoCommit(false); prep.executeBatch(); conn.setAutoCommit(true); ResultSet rs = stat.executeQuery("select * from people;"); while (rs.next()) { System.out.println("name = " + rs.getString("name")); System.out.println("job = " + rs.getString("occupation")); } rs.close(); conn.close(); }
From source file:DemoPreparedStatementSetBlob.java
public static void main(String[] args) throws Exception { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null;// w w w .j av a 2 s.c om java.sql.Blob blob = null; try { conn = getConnection(); // prepare blob object from an existing binary column pstmt = conn.prepareStatement("select photo from my_pictures where id = ?"); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); blob = rs.getBlob(1); // prepare SQL query for inserting a new row using setBlob() String query = "insert into blob_table(id, blob_column) values(?, ?)"; // begin transaction conn.setAutoCommit(false); pstmt = conn.prepareStatement(query); pstmt.setString(1, "0002"); pstmt.setBlob(2, blob); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); // end transaction conn.commit(); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:DemoDisplayBinaryDataFromDatabase.java
public static void main(String args[]) throws Exception { Connection conn = null; ResultSet rs = null;/*ww w . j ava 2 s. c o m*/ PreparedStatement pstmt = null; String query = "SELECT raw_column, long_raw_column FROM binary_table WHERE id = ?"; try { conn = getConnection(); Object[] results = new Object[2]; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); // materialize binary data onto client results[0] = rs.getBytes("RAW_COLUMN"); results[1] = rs.getBytes("LONG_RAW_COLUMN"); } finally { rs.close(); 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("create table survey (id int, name VARCHAR(30) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setString(2, "name1"); pstmt.executeUpdate();/*from www. ja v a 2s . com*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.executeUpdate(); rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.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, id2 tinyint, id3 smallint, id4 bigint, id5 real);"); String sql = "INSERT INTO survey (id2,id3,id4,id5) VALUES(?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); byte b = 1;//from w w w .j a va2s .c o m short s = 2; pstmt.setByte(1, b); pstmt.setShort(2, s); pstmt.setInt(3, 3); pstmt.setLong(4, 4L); pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getString(2)); } rs.close(); stmt.close(); conn.close(); }
From source file:DemoPreparedStatementSetClob.java
public static void main(String[] args) throws Exception { String id = "0001"; String newID = "0002"; ResultSet rs = null;/* www . ja v a 2s . c o m*/ Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); // begin transaction conn.setAutoCommit(false); String query1 = "select clob_column from clob_table where id = ?"; pstmt = conn.prepareStatement(query1); pstmt.setString(1, id); rs = pstmt.executeQuery(); rs.next(); java.sql.Clob clob = (java.sql.Clob) rs.getObject(1); String query = "insert into clob_table(id, clob_column) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, newID); pstmt.setClob(2, clob); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); conn.commit(); } finally { rs.close(); 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("create table images (Id int, b BLOB);"); File file = new File("myimage.gif"); FileInputStream fis = new FileInputStream(file); PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)"); ps.setString(1, "10"); ps.setBinaryStream(2, fis);//w w w. j a v a 2 s .c o m ps.executeUpdate(); ResultSet rset = st.executeQuery("select b from images"); InputStream stream = rset.getBinaryStream(1); ByteArrayOutputStream output = new ByteArrayOutputStream(); int a1 = stream.read(); while (a1 >= 0) { output.write((char) a1); a1 = stream.read(); } Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray()); output.close(); ps.close(); fis.close(); st.close(); conn.close(); }
From source file:DemoPreparedStatementSetClob.java
public static void main(String[] args) throws Exception { String id = "0001"; String newID = "0002"; ResultSet rs = null;/*from w w w .j a v a 2 s. c om*/ Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); // begin transaction conn.setAutoCommit(false); String query1 = "select clob_column from clob_table where id = ?"; pstmt = conn.prepareStatement(query1); pstmt.setString(1, id); rs = pstmt.executeQuery(); rs.next(); java.sql.Clob clob = (java.sql.Clob) rs.getObject(1); String query = "insert into clob_table(id, clob_column) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, newID); pstmt.setClob(2, clob); // execute query, and return number of rows created int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); // end transaction conn.commit(); } finally { rs.close(); pstmt.close(); conn.close(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection dbConnection = null; PreparedStatement preparedStatement = null; Class.forName(DB_DRIVER);//from www . j av a 2 s . co m dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); String updateTableSQL = "UPDATE Person SET USERNAME = ? " + " WHERE USER_ID = ?"; preparedStatement = dbConnection.prepareStatement(updateTableSQL); preparedStatement.setString(1, "newValue"); preparedStatement.setInt(2, 1001); preparedStatement.executeUpdate(); preparedStatement.executeUpdate(); preparedStatement.close(); dbConnection.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, id2 tinyint, id3 smallint, id4 bigint, id5 real);"); String sql = "INSERT INTO survey (id2,id3,id4,id5) VALUES(?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); byte b = 1;//from w w w . j a v a 2 s . co m short s = 2; pstmt.setByte(1, b); pstmt.setShort(2, s); pstmt.setInt(3, 3); pstmt.setLong(4, 4L); pstmt.executeUpdate(); ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.println(rs.getString(2)); } rs.close(); stmt.close(); conn.close(); }