Java 最简单的批处理

批处理Batch && PreparedStatement :

import java.sql.*;

public class TestBatch {
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException
	{
		Class.forName("com.mysql.jdbc.Driver");
		
		Connection conn = DriverManager.getConnection
		("jdbc:mysql://localhost/test","root","ZHEN"); 
		
		/*
		Statement stmt = conn.createStatement();  //执行静态 SQL 语句并返回它所生成结果的对象。 
		
		stmt.addBatch("INSERT INTO student VALUES(30,'ddd',12,'ii')");
		stmt.addBatch("INSERT INTO student VALUES(31,'ddd',12,'ii')");
		stmt.addBatch("INSERT INTO student VALUES(32,'ddd',12,'ii')");
		stmt.addBatch("INSERT INTO student VALUES(33,'ddd',12,'ii')");
		
		stmt.executeBatch();
		
		stmt.close();
		
		*/
		
		PreparedStatement pstmt = 
			conn.prepareStatement("INSERT INTO student VALUES(?,?,?,?)");
		
		pstmt.setInt(1, 40);
		pstmt.setString(2,"qq");
		pstmt.setInt(3,5);
		pstmt.setString(4,"ww");
		pstmt.addBatch();
		
		pstmt.setInt(1, 41);
		pstmt.setString(2,"qq");
		pstmt.setInt(3,5);
		pstmt.setString(4,"ww");
		pstmt.addBatch();
		
		pstmt.setInt(1, 42);
		pstmt.setString(2,"qq");
		pstmt.setInt(3,5);
		pstmt.setString(4,"ww");
		pstmt.addBatch();
		
		pstmt.executeBatch();
		pstmt.close();
		
		conn.close();
		
	}

}

  

posted on 2015-08-04 13:50  手撕高达的村长  阅读(8179)  评论(0编辑  收藏  举报

导航