存储过程
数据库定义语句:
即:
delimiter &&
create procedure proced123()
begin
update dept set dname="*********" where deptno =30;
select * from dept where deptno >20;
end
&&
delimiter ;
*java 调用:
*
即:private static void execProc() {
try {
Connection conn = MySQLUtil.getConnection();// 2、建立连接
CallableStatement stmt = conn.prepareCall("{call proced123()}");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(“deptno”) + " " + rs.getString(“dname”)+
" " + rs.getString(“loc”));
}
MySQLUtil.free(rs, stmt, conn);// 释放资源
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
execProc();
}
java 事件
同时同步增加多条数据
DAO层代码
@Override
public int insert2Test(Demo test1, Demo test2) throws SQLException {
Connection conn = MySQLUtil.getConnection();
conn.setAutoCommit(false);
String sql=“insert into tb_test values(?,?)”;
PreparedStatement ps=null;
PreparedStatement ps2=null;
int res=0;
try {
ps = conn.prepareStatement(sql);
// System.out.println(3/0);
ps.setString(1, test1.getId());
ps.setString(2, test2.getName());
res=ps.executeUpdate();
ps2=conn.prepareStatement(sql);
ps2.setString(1, test2.getId());
ps2.setString(2, test2.getName());
res=res+ps2.executeUpdate();
System.out.println(res);
} catch (Exception e) {
conn.rollback();
e.printStackTrace();
MySQLUtil.free(null, ps, conn);
MySQLUtil.free(null, ps2, conn);
}
return res;
}