zoukankan      html  css  js  c++  java
  • Java(35) _JDBC批量插入数据二

    package MYSQK;
    import java.sql.*;
    
    /**
     * PreparedStatement 对象可以对sql语句进行预编译,预编译的信息会存在存储该对象中,当相同的sql语句再次执行时,程序
     *   会使用PrepareStatement对象中,而不需再次编译去查询数据库,大大提高了数据的访问效率
     */
    
    public class Insert {
        public  static  void main(String[] args) throws SQLException{
            Connection conn=null;
            PreparedStatement pst =null;
    
            try {
                // 1 加载驱动类
                Class.forName("com.mysql.jdbc.Driver");
                // 2 通过DriverManager获取connection对象
                 String url="jdbc:mysql://192.168.64.129:3306/jdbc?" +
                          "user=root&password=815qza&useUnicode=true&characterEncoding=UTF8";
                 conn = DriverManager.getConnection(url);
                 if (!conn.isClosed()){
                     System.out.println("Succeeded connecting to the Database!");
                 }else{
                     System.out.println("Sorry,failed  connecting to the Database");
                 }
                 // 3 获取pre对象
                  String sql = "insert INTO  USERS(name,PASSWORD,email,birthday)  VALUES (?,?,?,?)" ;
                  pst = conn.prepareStatement(sql);
    
                 //4  使用prepare对象执行sql语句
                  System.out.println("开始插入数据");
                  long starttime = System.currentTimeMillis();
                  int count = 0;
                  for(int i=8;i<=100;i++) {
                      pst.setString(1,"bowen"+i);
                      pst.setString(2,"815qza");
                      pst.setString(3,"bowen"+i+"@126.com");
                      pst.setString(4,"1990-01-01");
                      pst.executeUpdate();
                      count++;
                  }
                  long endtime = System.currentTimeMillis();
                  System.out.println("插入结束!耗费时间为"+(endtime-starttime)/1000+"s");
                  System.out.println("共插入"+count+"条数据");
    
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }finally {
                // 6 关闭连接
                 pst.close();
                 conn.close();
            }
        }
    }

  • 相关阅读:
    2015第11周日
    2015第11周六
    素数推断算法(高效率)
    vc中关于 directx的配置,和dxsdk_extras(directshow)
    几种开源分词工具的比較
    javascript实现函数的默认參数值方法
    MyReport报表引擎2.0.0.0新功能
    怎样在小方框上打对号 小方框内打对勾 word 方框打对勾
    Bombing HDU, 4022(QQ糖的消法)
    fullcalendar日历控件知识点集合
  • 原文地址:https://www.cnblogs.com/sunnybowen/p/9880135.html
Copyright © 2011-2022 走看看