zoukankan      html  css  js  c++  java
  • java语言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(); } } }


  • 相关阅读:
    Word Break
    Binary Tree Right Side View
    41. First Missing Positive
    2 Sum ,3 Sum, 3 Sum close
    216. Combination Sum III
    190. Reverse Bits
    143. Reorder List
    142. Linked List Cycle II
    Single Number i,ii,iii
    62. Unique Paths i & ii
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6978060.html
Copyright © 2011-2022 走看看