logger.info("向数据库表中插入数据。");
String connId = "SampleDataSource";
//JDBCTransaction transaction = (JDBCTransaction)resManager.getResource(JDBCTransaction.class.getName());
JDBCTransaction transaction = (JDBCTransaction)((TransactionFactory)resManager.getResource("SampleTransaction")).create();
/* 返回SQLExecutor工厂,并创建SQLExecutor对象 */
SQLExecutor sqlExecutor = transaction.getSQLExecutorFactory(connId).create();
/* 返回SQLBuilder工厂,并创建SQLBuilder对象 */
SQLBuilder sqlBuilder = transaction.getSQLBuilderFactory(connId).create("");
try{
/* 开始事务 */
transaction.begin();
//transaction.setAutoCommit(connId, true);
System.out.println("transaction.getAutoCommit(connId):"+transaction.getAutoCommit(connId));
//transaction.setTransactionLevel(connId, level);
// SQL sql = sqlBuilder.constructSQLForInsert("Messages", messagesHM);
// if(logger.isDebugEnabled()){
// logger.debug("通过SQLBuilder构建的SQL: " + sql.getSQLString()+" "+messagesHM);
// }
String[] sqls = new String[messagesMaps.length];
for(int i=0;i<messagesMaps.length;i++){
String sqlstr =
"INSERT INTO Messages (Sender,Email,Subject,Contact,IP,SendTime,ID,Content) " +
"VALUES ('"+messagesMaps[i].get("Sender")+
"','"+messagesMaps[i].get("Email")+
"','"+messagesMaps[i].get("Subject")+
"','"+messagesMaps[i].get("Contact")+
"','"+messagesMaps[i].get("IP")+
"','"+DateTimeHelper.formatDateTimetoString(DateTimeHelper.getSystemDate())+
"','"+(generateID(sqlExecutor).intValue()+i)+
"','"+messagesMaps[i].get("Content")+"')";
sqls[i]=sqlstr;
if(logger.isDebugEnabled()){
logger.debug("通过SQLBuilder构建的SQL: " + sqlstr+" ");
}
}
/* 执行SQ语句 */
int[] rs = sqlExecutor.executeBatch(sqls);
System.out.println(rs[0]+","+rs[1]);
/* 提交事务 */
transaction.commit();
}catch(Exception e){
/* 回退事务 */