日期:2014-05-16  浏览次数:20462 次

jdbctemp 事务
<%@ page import="java.net.URLEncoder"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/tlds/wall.tld" prefix="wall"%>
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", 0);
	response.flushBuffer();
%>
<%@page import="org.springframework.jdbc.core.PreparedStatementCallback"%><html>
	<head>
		<title></title>
	</head>
	<body>
	
<%
			javax.sql.DataSource dataSource = (javax.sql.DataSource)WebApplicationContextUtils.getWebApplicationContext(
					pageContext.getServletContext()).getBean("luxe_dataSource");

			org.springframework.jdbc.core.JdbcTemplate  jdbcTemp= new org.springframework.jdbc.core.JdbcTemplate(dataSource);
	
			
		//	System.out.println("  ==== dataSource :  " + dataSource);
		//	System.out.println("  ==== jdbcTemplare :  " + jdbcTemp);
			
			
			
			
String sql = "select id,name,order_index from luxe_poi_cat where book_id=? and parent_id=0 and is_delete=0 order by order_index;";
String childrenSql = "select id,name,order_index from luxe_poi_cat where book_id=? and parent_id=? and is_delete=0 order by order_index;";
String updateOrderSql ="update luxe_poi_cat set order_index=? where id=?";

	String sBookId=request.getParameter("book_id");
	
	int iBookId =0;
	if(sBookId!=null && sBookId.trim().length()>0){
		try{
			iBookId = Integer.parseInt(sBookId);
			System.out.println(" book_id==== ["+sBookId+"]");
			out.println(" book_id==== ["+sBookId+"]");
		}catch(NumberFormatException ne){ 
			System.out.println("  ==== paramter [book_id]  just accept integer type ");
			out.println("  ==== paramter [book_id] just accept integer type ");
			ne.printStackTrace();
			System.exit(1);	
		}
	}else{
		System.out.println("  ==== paramter [book_id] must not be empty ");
		out.println("  ==== paramter [book_id] must not be empty ");
		
	}
			
	  org.springframework.transaction.support.DefaultTransactionDefinition def = new org.springframework.transaction.support.DefaultTransactionDefinition();
	  org.springframework.transaction.PlatformTransactionManager transactionManager = 
		   (org.springframework.transaction.PlatformTransactionManager)
		   WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext()).getBean("transactionManager");
	  org.springframework.transaction.TransactionStatus status = transactionManager.getTransaction(def); 
	  

   try{
   List list=jdbcTemp.queryForList(sql,new Object[]{iBookId});
  // System.out.println(" >>> list : " + list);
if(list!=null && list.size()>1){
for(int i=0;i< list.size();i++){
Map  recordMap=(Map)list.get(i);
System.out.println(" ================= Record ===== " + recordMap);
int pkKey= Integer.parseInt(recordMap.get("id").toString());

//update   chirldren order with parent id
List childrenList=jdbcTemp.queryForList(childrenSql,new Object[]{iBookId,pkKey});

if(childrenList!=null){
for(int j=0;j< childrenList.size();j++){
Map  childrenRecordMap=(Map)list.get(j);

System.out.println(" =================children  Record ===== " + childrenRecordMap);
int childrenPKKey= Integer.parseInt(childrenRecordMap.get("id").toString());
jdbcTemp.update(updateOrderSql,new Object[]{j+2,childrenPKKey});
}
}
//update parent order id.