1、创建存储过程Proc代码
- create or replace procedure changesalary(p_employeeid number, p_newsalary number) is
- begin
- update employees set salary= p_newsalary
- where employee_id = p_employeeid;
-
- if sql%notfound then
- raise_application_error(-20100,"Invalid Employee Id");
- end if;
-
- end;
- /
create or replace procedure changesalary(p_employeeid number, p_newsalary number)isbeginupdate employees set salary= p_newsalarywhere employee_id = p_employeeid;ifsql%notfound thenraise_application_error(-20100,"Invalid Employee Id");end if;end;/2、hibernate配置Xml代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
- <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
- <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
- <property name="hibernate.connection.username">hr</property>
- <property name="hibernate.connection.password">hr</property>
- <property name="hibernate.hbm2ddl.auto">update</property>
- <property name="hibernate.show_sql">true</property>
- </session-factory>
- </hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property><property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property><property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property><property name="hibernate.connection.username">hr</property><property name="hibernate.connection.password">hr</property><property name="hibernate.hbm2ddl.auto">update</property><property name="hibernate.show_sql">true</property></session-factory></hibernate-configuration> 3、hibernate应用(1)使用JDBC连接Java代码
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
-
- public class CallSP {
-
- public static void main(String[] args) throws Exception {
-
- Configuration c = new Configuration().configure();
- SessionFactory sf = c.buildSessionFactory();
- Session session = sf.openSession();
- session.beginTransaction();
-
- Connection con = session.connection(); // obtain JDBC connection from Session object
- CallableStatement cs = con.prepareCall("{ call changesalary(?,?) }");
- cs.setInt(1,100); // first parameter index start with 1
- cs.setInt(2,6000); // second parameter
- cs.execute(); // call stored procedure
-
- session.getTransaction().commit();
- session.close();
- sf.close();
- }
- }
import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class CallSP {public static void main(String[] args) throws Exception{Configuration c = new Configuration().configure();SessionFactory sf = c.buildSessionFactory();Session session = sf.openSession();session.beginTransaction();Connection con = session.connection();// obtain JDBC connection from Session objectCallableStatement cs = con.prepareCall("{ call changesalary(?,?) }");cs.setInt(1,100);// first parameter index start with 1cs.setInt(2,6000); // second parametercs.execute();// call stored proceduresession.getTransaction().commit();session.close();sf.close();}} (2)使用Native SQLJava代码
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
-
- public class CallSP {
-
- public static void main(String[] args) throws Exception {
-
- Configuration c = new Configuration().configure();
- SessionFactory sf = c.buildSessionFactory();
- Session session = sf.openSession();
- session.beginTransaction();
-
- Query q = session.createSQLQuery(" { call changesalary(?,?) }");
- q.setInteger(0,100); // first parameter, index starts with 0
- q.setInteger(1,4000); // secon parameter
- q.executeUpdate();
- session.getTransaction().commit();
- session.close();
- sf.close();
- }
- }
Oralce预编译Sql语句在JDBC中的处理Hibernate 调用存储过程返回列表相关资讯 hibernate Oracle基础教程
- Hibernate利用@DynamicInsert和@ (今 07:09)
- Hibernate某些版本(4.3)下报错 (04月20日)
- Hibernate 5.1.0 正式版发布下载 (02月12日)
| - Hibernate的get和load的区别 (08月07日)
- Hibernate3.1.2_中文文档PDF (02月17日)
- Hibernate ORM 5.0.6 发布下载 (12/17/2015 17:12:55)
|
本文评论 查看全部评论 (0)