java多表添加,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就能把多表插入到数据库啊?

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()批量处理语句

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值