java Hibernate 调用存储过程

实例:

package com.test;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.util.HibernateSessionFactory;
/**
 * 
 * @author yushen
 *
 */
public class StoredProcedureTest {
	
	// 调用函数FUN_GET();
	public static void main(String[] args) {
		System.out.println(getOracle());
	}

	/**
	 * create or replace procedure get_username(v_id in number,v_username out
	 * varchar2) as begin select username into v_username from tab_user where id
	 * = v_id; --变量赋值 exception when no_data_found then
	 * raise_application_error(-20001,'ID不存在!'); end get_username;
	 * 
	 * @return
	 */
	public static List getOracle() {

		Session session = HibernateSessionFactory.getSession();
		SQLQuery query = session.createSQLQuery("{Call get_usernames(?,?)}"); // 这里调用存储过程
		query.setString(0, "ddd");
		List list = query.list();
		session.close();
		return list;
	}

	public void saveUpde() {
		Session session = HibernateSessionFactory.getSession();
		Connection conn = session.connection();
		ResultSet rs = null;
		CallableStatement call;
		try {
			call = conn.prepareCall("{Call proc(?)}");
			call.setString(1, " 参数");
			rs = call.executeQuery();
			rs.close();
			session.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

猜你喜欢

转载自blog.csdn.net/weixin_42749765/article/details/81778860