ddbdzs.cn ddbdzs.cn

欢迎光临
我们一直在努力
顶部
域名
云服务器48/月

java语言MySQL批处理_MySQL-mysql教程-

本质来讲就是使用statement和prestatement的addbatch()方法

代码

import java.sql.*;

public class GetConnection{
	public static void main(String[] args){
		Access2Database adb=new Access2Database();
		Connection conn=adb.getConn();	
		
		//transaction dealing
		PreparedStatement pstam=null;
		try{
			conn.setAutoCommit(false);
			String sql="insert into student(name,major,score) values(?,?,?);";
			pstam=conn.prepareStatement(sql);
			pstam.setString(1, "f");
			pstam.setString(2,"History");
			pstam.setInt(3, 67);
			pstam.addBatch();
			pstam.setString(1, "h");
			pstam.setString(2, "Biology");
			pstam.setInt(3, 85);
			pstam.addBatch();
			pstam.executeBatch();
			conn.commit();
		}catch(SQLException e){
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
		
		//release the resource of the program
		try{
			pstam.close();
			conn.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}
登录后复制

【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。
发布内容
-六神源码网 网站出售带数据-六神源码网 网站出售带数据-六神源码网 网站出售带数据-六神源码网