zoukankan      html  css  js  c++  java
  • sql批量入库

    public void saveBatch(List<Asset> entitys) throws ClassNotFoundException, SQLException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            StringBuffer sql = new StringBuffer("INSERT IGNORE INTO 表名 (AssetId,Category,SubCategory,VideoUrl"
                    + ",Anchor,AssetDesc,Keyword,Title,UploadTime,ImgUrl,PosterUrl,Duration,CreateTime,VideoStatus,ImgStatus,UpdateTime) "
                    + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");
            
            String className = PropertyManager.getProperty("jdbc.driverClassName");
            String url = PropertyManager.getProperty("jdbc.url")+"&useServerPrepStmts=false&rewriteBatchedStatements=true";
            String userName = PropertyManager.getProperty("jdbc.username");
            String password = PropertyManager.getProperty("jdbc.password");            
            
            Class.forName(className);
            Connection connection = DriverManager.getConnection(url,userName,password);
            //设置手动提交
            connection.setAutoCommit(false);  
            PreparedStatement ps = connection.prepareStatement( sql.toString());
            for (Asset entity : entitys) {
                ps.setString(1,entity.getAssetId());
                ps.setString(2,entity.getCategory());
                ps.setString(3,entity.getSubCategory());
                ps.setString(4,entity.getVideoUrl());
                ps.setString(5,entity.getAnchor());
                ps.setString(6,entity.getAssetDesc());
                ps.setString(7,entity.getKeyword());
                ps.setString(8,entity.getTitle());
                ps.setString(9,entity.getUploadTime()+"");
                ps.setString(10,entity.getImgUrl());
                ps.setString(11,entity.getPosterUrl());
                ps.setInt(12,entity.getDuration());
                ps.setString(13,sdf.format(new Date()));
                ps.setInt(14,0);
                ps.setInt(15,0);
                ps.setString(16,sdf.format(new Date()));
                ps.addBatch();
            }
            ps.executeBatch();
            ps.clearBatch();
            //提交批处理
            connection.commit();
            //执行
            connection.close();
        }

    INSERT IGNORE INTO  库中没有的入库,有的忽略,注意设置唯一索引,一般除了id只有一个。

    REPLACE INTO 库中存在的直接更新。

    INSERT INTO 表名(id,age,name)values ( id ,age,name)   on duplicate key update age=? , name =?

  • 相关阅读:
    RealView MDK在链接时提示空间不够的解决方案总结
    不同的LCD之间程序移植时配置参考
    S3c2440ALCD控制器配置实例
    mini2440裸机之PWM
    对增益大于等于10时保持稳定的放大器进行补偿以在较低增益下工作
    ASP.NET HttpContext的时间戳属性
    C#验证邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP类.. (转)
    sqlhelper 实现回滚事务
    JQuery全选和反选
    js 获取前天、昨天、今天、明天、后天的时间 (转)
  • 原文地址:https://www.cnblogs.com/onroad2019/p/11327164.html
Copyright © 2011-2022 走看看