执行SQL Batch
2007-12-23 12:35:34 来源:WEB开发网核心提示:/*Database PRogramming with JDBC and java, Second EditionBy George ReeseISBN: 1-56592-616-1Publisher: O'Reilly*/import java.sql.Connection;import java.sql.D
/*
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 = (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) {
}
}
}
}
}
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 = (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) {
}
}
}
}
}
(出处:http://www.cncms.com)
- ››SQL Server 2008 R2 下如何清理数据库日志文件
- ››sqlite 存取中文的解决方法
- ››SQL2005、2008、2000 清空删除日志
- ››SQL Server 2005和SQL Server 2000数据的相互导入...
- ››sql server 2008 在安装了活动目录以后无法启动服...
- ››sqlserver 每30分自动生成一次
- ››sqlite 数据库 对 BOOL型 数据的插入处理正确用法...
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
更多精彩
赞助商链接