zoukankan      html  css  js  c++  java
  • JDBC--批处理操作数据库

    一次处理多条mysql语句

    package com.machuang.jdbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Demo14 {
    
        public static void main(String[] args) {
            
            Connection conn = null;
            Statement st = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                
                // 建立连接
                conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/testjdbc", "root", "333666");
                
                conn.setAutoCommit(false);        // 设置手动递交
                st = conn.createStatement();
                
                for (int i = 0; i < 2000; i++) {
                    st.addBatch("insert into t_usr (usrName, pwd, regTime) value ('Cappuccino"+i+"', '64648', now())");
                }
                st.executeBatch();
                
                conn.commit(); //递交
                
                
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(null != st) {
                        st.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    if(null != conn) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            
    
        }
    
    }
  • 相关阅读:
    整理牙刷
    color 圆盘染色
    数论の一波流[长期更新]
    生成树
    一维黑白棋
    Factorials
    平面分割问题
    poj1183 反正切函数
    烽火传递
    校门外的树
  • 原文地址:https://www.cnblogs.com/cappuccinom/p/8877045.html
Copyright © 2011-2022 走看看