---
title: "Hiberante批量处理(Hibernate API 转为 JDBC API 执行)"
author: "w2c2y2"
date: "2009-09-24T20:10:17Z"
tags: []
slug: "hiberante-hibernate-api-jdbc-api"
draft: false
lastmod: "2009-09-25T20:04:12Z"
id: 476705
url: "https://w2c2y2.iteye.com/blog/476705"
---

今天使用Excel导入到数据库
但是要进行批量插入， 使用的是JDBC的方式进行的，可是就是在事务处理的时候没有成功，
我在网上找了一些资料，这次又是使用Hibernate的回调机制。成功了！

分享一下~

Hibernate中的Dao层的代码:
package teach.dao.adminpart;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class StudentCourseBatchDaoImp extends HibernateDaoSupport implements
		StudentCourseBatchDao {
	/*
	 * 
	 * JDBC API 方式处理
	 * 
	 * 
	 */

	public void addTeacherCourseByBatch(final String excelPath,
			final String courseid, final Integer grade, final Integer term)
			throws SQLException, FileNotFoundException, IOException {
		/*
		 * 
		 * 批量添加学生成绩的语句
		 * 
		 * 绕过Hibernate的API来处理
		 * 
		 * 采用JDBC的API 来处理
		 * 
		 * 1、修改方法的前缀，使方法绕过Spring的声明式事务( 经过测试，这里Spring的事务没有作用了 )
		 * 2、通过session获取事务，不过该事务无法成功启动。
		 * 
		 * 3、不如手启动JDBC的事务。
		 */

		this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				HSSFWorkbook workBook;
				int rowsLength = 0;
				HSSFSheet sheet = null;
				
				try {
					workBook = new HSSFWorkbook(new FileInputStream(excelPath));
					sheet = workBook.getSheet("Sheet1");// 取Excel的第一页就可以了。
					rowsLength = sheet.getPhysicalNumberOfRows(); // 获取行数.
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				String studentid;
				int mark;

				java.sql.Date year = new java.sql.Date(new java.util.Date()
						.getTime()); // 使用那个java.sql.Date

				Connection conn = session.connection();

				String sql = "insert into studentcourse(studentid,courseid,mark,remark,stage,grade,term,year)"
						+ " values(?,?,?,?,?,?,?,?)";
				PreparedStatement stmt = conn.prepareStatement(sql);

				for (int i = 1; i < rowsLength; i++) {
					HSSFRow row = sheet.getRow(i);

					if (null != row) {

						HSSFCell cell_0 = row.getCell((short) 0);
						studentid = cell_0.getStringCellValue();

						HSSFCell cell_2 = row.getCell((short) 2);
						mark = (int) cell_2.getNumericCellValue();

						stmt.setString(1, studentid);
						stmt.setString(2, courseid);
						stmt.setInt(3, mark);
						stmt.setInt(4, 0);
						stmt.setInt(5, 2);
						stmt.setInt(6, grade);
						stmt.setInt(7, term);
						stmt.setDate(8, year);
						stmt.addBatch();
					}
					
					stmt.executeBatch();

				}
				return null;
			}
		});
	}
}



要看到它有点难度啊，因为我加入了Apache项目下的POI 读取Excel文件，这样大家应该清楚一些了吧！！