SQL Batch : SQL Update « Database SQL JDBC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Database SQL JDBC » SQL UpdateScreenshots 
SQL Batch

/*

Database Programming with JDBC and Java, Second Edition
By George Reese
ISBN: 1-56592-616-1

Publisher: O'Reilly

*/


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Example 4.1.
 */
public class Batch {
  static public void main(String[] args) {
    Connection conn = null;

    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 = (Stringusers.next();

        stmt.setString(1, uid);
        stmt.addBatch();
      }
      stmt.executeBatch();
    catch (Exception e) {
      e.printStackTrace();
    finally {
      if (conn != null) {
        try {
          conn.close();
        catch (Exception e) {
        }
      }
    }
  }
}

           
       
Related examples in the same category
1.Create Coffees table
2.JDBC update
3.JDBC Update logic
4.Batch UpdateBatch Update
5.Get Metadata from prepareStatement
6.Batch Update: transactionBatch Update: transaction
7.Return generated keys
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.