3.关闭

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/*
 * 批处理时候,最好使用statement,因为preparestatement的的编译空间有限,数据过于庞大时会发生异常
 */
public class Demo_main {

	public static void main(String[] args){
		Statement s = null;
		Connection c = null;
			try {
//加载驱动类
				Class.forName("com.mysql.jdbc.Driver");
//建立连接
				c = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc的使用","root","root");
		
			   
				
				
				
//批处理时关闭jdbc的自动提交
			    c.setAutoCommit(false);
//执行SQL语句		
			    long start = System.currentTimeMillis();
				s = c.createStatement();
			    //插入两万行数据
			    for(int i=0;i<2000;i++) {
			    	s.addBatch("insert into student (name,age) values ('张三"+i+"',1)");//里面写执行语句
			    }
			    long end = System.currentTimeMillis();
				s.executeBatch();						    
//手动提交
			    c.commit();
			    System.out.println("开始时间:"+start);
			    System.out.println("结束时间:"+end);
				System.out.println("执行时间"+(end-start));	
				
				
				
				
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
//关闭
				try {
					if(s != null) {
						s.close();
						}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				try {
					if(c != null) {
						c.close();						
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值