1. Create a stored procedure
Proc code
- 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,'Invalidemployee id');
- End if;
- End;
- /
Create or replace procedure changesalary (p_employeeid number, p_newsalary number) isbegin update employees set salary = p_newsalary where employee_id = p_employeeid; if SQL % notfound then raise_application_error (-20100, 'invalidemployee id'); end if; end ;/
2. hibernate Configuration
Xml Code
- <?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">
3. hibernate applications
(1) Connect using JDBC
Java code
- ImportOrg. hibernate. Query;
- ImportOrg. hibernate. Session;
- ImportOrg. hibernate. SessionFactory;
- ImportOrg. hibernate. cfg. Configuration;
- Public ClassCallSP {
- Public Static VoidMain (String [] args)ThrowsException {
- Configuration c =NewConfiguration (). 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.exe cute ();// 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. conne Ction (); // 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.exe cute (); // call stored procedure session. getTransaction (). commit (); session. close (); sf. close ();}}
(2) Use Native SQL
Java code
- ImportOrg. hibernate. Query;
- ImportOrg. hibernate. Session;
- ImportOrg. hibernate. SessionFactory;
- ImportOrg. hibernate. cfg. Configuration;
- Public ClassCallSP {
- Public Static VoidMain (String [] args)ThrowsException {
- Configuration c =NewConfiguration (). 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.exe cuteUpdate ();
- Session. getTransaction (). commit ();
- Session. close ();
- Sf. close ();
- }
- }