hibernate session获取Connection_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > hibernate session获取Connection

hibernate session获取Connection

 2014/10/29 19:28:54  zyazyz008  程序员俱乐部  我要评论(0)
  • 摘要:publicintexecuteSql(String[]sql){intkey=0;ConnectionProvidercp=((SessionFactoryImplementor)getSession().getSessionFactory()).getConnectionProvider();Connectionconn=null;PreparedStatementpps=null;try{conn=cp.getConnection();conn.setAutoCommit(false)
  • 标签:hibernate
class="java">public int executeSql(String[] sql) {
		int key = 0;

		ConnectionProvider cp = ((SessionFactoryImplementor) getSession().getSessionFactory()).getConnectionProvider();
		Connection conn = null;
		PreparedStatement pps = null;
		try {
			conn = cp.getConnection();
			conn.setAutoCommit(false);
			for (int i = 0; i < sql.length; i++) {
				if (sql[i] == null || "".equals(sql[i]))
					break;
				pps = conn.prepareStatement(sql[i]);
				pps.execute();
				pps.close();
				key++;
				if (key % 1000 == 0) {
					conn.commit();					
				}
			}
			conn.commit();
		} catch (Exception e) {
			if (conn != null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return key;
	}
发表评论
用户名: 匿名