@Override
public boolean insertBook(BookBean book) {
boolean b=false;
String sql="insert into book_info values(?,?,?,?,?,?,?,?,?,?)";
openDB();
try {
conn.setAutoCommit(false);
//插入书籍信息sql
pstat=conn.prepareStatement(sql);
pstat.setString(1, book.getISBN());
pstat.setString(2, book.getTitle());
pstat.setString(3, book.getPublisher());
pstat.setString(4, book.getAuthor());
pstat.setInt(5, book.getStock());
pstat.setDouble(6, book.getCprice());
pstat.setDouble(7, book.getPrice());
pstat.setInt(8, book.getSales());
pstat.setString(9, book.getRemark());
pstat.setString(10, book.getImgurl());
pstat.addBatch();
pstat.executeBatch();
//插入类别信息sql
sql="insert into book_info_type values(?,?)";
pstat=conn.prepareStatement(sql);
for(int i=0; i<book.getTypeId().length; i++) {
pstat.setString(1, book.getISBN());
pstat.setInt(2, Integer.parseInt(book.getTypeId()[i]));
pstat.addBatch();
}
pstat.executeBatch();
conn.commit();
b=true;
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
} finally {
closeDB();
}
return b;
}
大概就是一个书籍信息表A,一个类别表B,一个书籍类别详情表C(关联前面两表),A插入数据时,会同时给C表做插入操作。
java预操作多行sql语句
最新推荐文章于 2022-02-08 21:29:57 发布