Create an EJB connection and execute an SQL statement

Source: Internet
Author: User

1. Under the project source folder, create the persistent. xml file.

Project name

-- | SRC

-- | Persistence. xml

2. Compile the persistent. xml file in the following format.

<? XML version = "1.0" encoding = "UTF-8"?> <Persistence version = "1.0" xmlns = "http://java.sun.com/xml/ns/persistence" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name = "cici_persistence_unit" transaction-type = "resource_local"> <provider> Org. eclipse. persistence. JPA. persistenceprovider </provider> <class> COM. cici. CRM. migration. model. CRM. crmcardinstance </class> <class> COM. cici. CRM. migration. model. CRM. crmapplicationinstance </class> <Properties> <property name = "eclipselink. JDBC. native-SQL "value =" true "/> <property name =" eclipselink. JDBC. batch-writing "value =" JDBC "/> <property name =" eclipselink. logging. level "value =" off "/> <property name =" eclipselink.tar get-Database "value =" oracle "/> <property name =" eclipselink. JDBC. URL "value =" JDBC: oracle: thin: @ localhost: 1521: cicidb "/> <property name =" eclipselink. JDBC. driver "value =" oracle. JDBC. driver. oracledriver "/> <property name =" eclipselink. JDBC. user "value =" admcici "/> <property name =" eclipselink. JDBC. password "value =" admcici "/> <property name =" eclipselink. DDL-generation "value =" NONE "/> </Properties> </persistence-unit> </persistence>

 

3. Create a map containing dB connection Parameters

 
Map <string, string> properties = new hashmap <string, string> (); properties. put ("Your selink. JDBC. user ", configuration. getinstance (). getvalue (configuration. otam_database_username); properties. put
 
("Eclipselink. JDBC. Password", configuration. getinstance (). getvalue (configuration. otam_database_password ));

4. Create entitymanagerfactory

 
EntitymanagerfactoryRcafactory =Persistence. Createentitymanagerfactory (cici_persistence_unit, properties );

5. Create entitymanager

 
EntitymanagerEntitymanager = rcafactory. createentitymanager ();

6. Create entitytransaction and enable connection

 
Entitymanager. GetTransaction(). Begin ();

7. Create a query and execute an SQL statement

 
QueryQuery = entitymanager. createnamedquery (SQL );
 
Query.exe cuteupdate ();

8. Submit a transaction

 

 
Entitymanager. gettransaction (). Commit ();

 

9 Close the connection

 

 
Entitymanger. Close ();
Cicifacloud. Close ();

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.