List of usage examples for java.sql PreparedStatement addBatch
void addBatch() throws SQLException;
PreparedStatement
object's batch of commands. From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); con.setAutoCommit(false);/* w ww.j a v a2 s . c om*/ String sql = "INSERT product VALUES(?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, "A"); prest.setInt(2, 2002); prest.addBatch(); prest.setString(1, "B"); prest.setInt(2, 1998); prest.addBatch(); prest.setString(1, "C"); prest.setInt(2, 1980); prest.addBatch(); prest.setString(1, "D"); prest.setInt(2, 1975); prest.addBatch(); int count[] = prest.executeBatch(); con.commit(); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/*ww w.j a va2s . c o m*/ 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.addBatch(); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.addBatch(); int[] updateCounts = pstmt.executeBatch(); checkUpdateCounts(updateCounts); conn.commit(); ResultSet 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 { 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(); prep.setString(1, "Turing"); prep.setString(2, "computers"); prep.addBatch();// w w w . j ava2 s .c om 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:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); conn.setAutoCommit(false);/*from ww w . j av a2s. c o m*/ 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.addBatch(); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.addBatch(); // execute the batch int[] updateCounts = pstmt.executeBatch(); checkUpdateCounts(updateCounts); // since there were no errors, commit conn.commit(); ResultSet 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(); conn.setAutoCommit(false);//from w w w .j a v a 2s . co m 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.addBatch(); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.addBatch(); try { // execute the batch int[] updateCounts = pstmt.executeBatch(); } catch (BatchUpdateException e) { int[] updateCounts = e.getUpdateCounts(); checkUpdateCounts(updateCounts); try { conn.rollback(); } catch (Exception e2) { e.printStackTrace(); System.exit(1); } } // since there were no errors, commit conn.commit(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); rs.close(); st.close(); conn.close(); }
From source file:MainClass.java
public static void main(String[] args) { Connection connection = null; PreparedStatement statement = null; try {/*from w w w .j a va2 s . c om*/ Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = "jdbc:mysql://localhost/database"; connection = DriverManager.getConnection(url, "username", "password"); String sql = "UPDATE employees SET email = ? WHERE employee_id = ?"; statement = connection.prepareStatement(sql); statement.setString(1, "a@a.com"); statement.setLong(2, 1); statement.addBatch(); statement.setString(1, "b@b.com"); statement.setLong(2, 2); statement.addBatch(); statement.setString(1, "c@c.com"); statement.setLong(2, 3); statement.addBatch(); statement.executeBatch(); } catch (Exception e) { e.printStackTrace(); } finally { if (statement != null) { try { statement.close(); } catch (SQLException e) { } // nothing we can do } if (connection != null) { try { connection.close(); } catch (SQLException e) { } // nothing we can do } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Connection dbConnection = null; PreparedStatement preparedStatement = null; Class.forName(DB_DRIVER);//from w ww .j a v a2s .c om dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); String insertTableSQL = "INSERT INTO Person" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" + "(?,?,?,?)"; preparedStatement = dbConnection.prepareStatement(insertTableSQL); dbConnection.setAutoCommit(false); java.util.Date today = new java.util.Date(); preparedStatement.setInt(1, 101); preparedStatement.setString(2, "101"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime())); preparedStatement.addBatch(); preparedStatement.setInt(1, 102); preparedStatement.setString(2, "102"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime())); preparedStatement.addBatch(); preparedStatement.setInt(1, 103); preparedStatement.setString(2, "103"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime())); preparedStatement.addBatch(); preparedStatement.executeBatch(); dbConnection.commit(); preparedStatement.close(); dbConnection.close(); }
From source file:Batch.java
static public void main(String[] args) { Connection conn = null;//from w w w . jav a2 s . co m try { ArrayList breakable = new ArrayList(); PreparedStatement stmt; Iterator users; ResultSet rs; Class.forName(args[0]).newInstance(); conn = DriverManager.getConnection(args[1], args[2], args[3]); stmt = conn.prepareStatement("SELECT user_id, password " + "FROM user"); rs = stmt.executeQuery(); while (rs.next()) { String uid = rs.getString(1); String pw = rs.getString(2); // Assume PasswordCracker is some class that provides // a single static method called crack() that attempts // to run password cracking routines on the password // if( PasswordCracker.crack(uid, pw) ) { // breakable.add(uid); // } } stmt.close(); if (breakable.size() < 1) { return; } stmt = conn.prepareStatement("UPDATE user " + "SET bad_password = 'Y' " + "WHERE uid = ?"); users = breakable.iterator(); while (users.hasNext()) { String uid = (String) users.next(); stmt.setString(1, uid); stmt.addBatch(); } stmt.executeBatch(); } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (Exception e) { } } } }
From source file:Main.java
public static void execute_delete_stmt(PreparedStatement pstmt, int[] indexes) throws SQLException { int cnt = 0;//from ww w . ja v a2s . c o m for (int i = 0; i < indexes.length; i++) { pstmt.setInt(1, indexes[i]); pstmt.addBatch(); cnt++; if (cnt > 5000) { pstmt.executeBatch(); cnt = 0; } } if (cnt > 0) pstmt.executeBatch(); }
From source file:Main.java
public static void execute_stmt(PreparedStatement pstmt, int[] indexes) throws SQLException { int cnt = 0;//from w w w .j a v a 2 s .c om for (int i = 0; i < indexes.length; i++) { pstmt.setInt(1, indexes[i]); pstmt.addBatch(); cnt++; if (cnt > 5000) { pstmt.executeBatch(); cnt = 0; } } if (cnt > 0) pstmt.executeBatch(); }