开发者社区> 问答> 正文

java 怎么多表同时插入到数据库啊?报错

"

public class Add { public void add(String fid,String title,String content){ Connection connection = null; Statement statement = null; Statement statement1 = null; String sql = null; String sql1 = null;

    try{
        connection = ConnectionUtils.getConnection();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String dateValue = simpleDateFormat.format(now);
        sql = "insert into news_base(fid,title,date,author) values("+fid+",'"+title+"','"+dateValue+"','Admin')";
        sql1 = "insert into news_content (cid,content) values("+fid+",'"+content+"')";
        statement1 = connection.createStatement();
        statement1.executeUpdate(sql1);
        statement = connection.createStatement();
        statement.executeUpdate(sql);
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(statement1 != null){
            try{
                statement1.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        if(statement != null){
            try{
                statement.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

}

部分代码

sql = "insert into news_base(fid,title,date,author) values("+fid+",'"+title+"','"+dateValue+"','Admin')";
            sql1 = "insert into news_content (cid,content) values("+fid+",'"+content+"')";
            statement1 = connection.createStatement();
            statement1.executeUpdate(sql1);
            statement = connection.createStatement();
            statement.executeUpdate(sql);

这样写的话,如果有10个插入 那不是要写10行重复的代码?看起来代码太臃肿了
有什么方法只写一个executeUpdate和一个createStatement就能把多表插入到数据库啊?

"

展开
收起
因为相信,所以看见。 2020-05-27 10:02:53 1000 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    "

    JDBC本身支持批量更新,具体API如下:

    addBatch(String sql):Statement类的方法, 可以将多条sql语句添加Statement对象的SQL语句列表中
    addBatch():PreparedStatement类的方法,可以将多条预编译的sql语句添加到PreparedStatement对象的SQL语句列表中
    executeBatch():把Statement对象或PreparedStatement对象语句列表中的所有SQL语句发送给数据库进行处理
    clearBatch():清空当前SQL语句列表

    使用批量更新API,我将你的代码调整如下:(注:如果SQL列表包含过多的待处理SQL语句, 可能会产生OutOfMemory错误。所以需要及时处理SQL语句列表。)

    public class AddBatchSql {
    
        public void add(String fid,String title,String content){
            Connection connection = null;
            Statement stmt = null;
            String sql1 = null;
            String sql2 = null;
    
            try{
                connection = ConnectionUtils.getConnection();
                stmt = connection.createStatement();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateValue = simpleDateFormat.format(new Date());
                sql1 = "insert into news_base(fid,title,date,author) values("+fid+",'"+title+"','"+dateValue+"','Admin')";
                sql2 = "insert into news_content (cid,content) values("+fid+",'"+content+"')";
                List<String> sqlList = new ArrayList<String>();
                sqlList.add(sql1);
                sqlList.add(sql2);
                for (int i = 0; i < sqlList.size(); i++) {
                    String sql = sqlList.get(i);
                    stmt.addBatch(sql);
                    if (i==500) {
                        stmt.executeBatch();//为避免出现OutOfMemory错误,及时处理
                        stmt.clearBatch();//清空列表
                    }
                }
                //最后一次列表不足500条,处理
                stmt.executeBatch();
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(stmt != null){
                    try{
                        stmt.close();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
        
    }
    ######

    多条相关联的记录用这种方法肯定是要一行一行写的,你可以自己封装一下,看起来应该好不了多少,还是用的orm框架吧,可读性好些。

    ######

    可以用addBatch()批量处理语句

    ######

    框架的好处就体现出来了,会帮你做重复的操作

    "
    2020-05-27 17:37:05
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载
超大型金融机构国产数据库全面迁移成功实践 立即下载