简体   繁体   中英

prepared Statement in ejb3

i want to use prepared statement with ejb3 to insert data in oracle. is it possible to use.

i try to find some example on net but i could not find any good example.

Please help me to use it. or is there any other way to use parameter query (as we use ? in prepared statement) in ejb3

Thanks and regards

it is very simple:

public class YourEJB {

    @Resource(mappedName="java:/DefaultDataSource") 
    DataSource dataSource;

    // XXX: not handling exceptions       
    public void insertPerson(String name, String surname) {
        Connection connection = dataSource.getConnection();
        PreparedStatement insertPerson = connection.prepareStatement(
           "INSERT INTO PEOPLE VALUES(?,?)");
        insertPerson.setString(1, name);
        insertPerson.setString(2, surname);
        insertPerson.executeUpdate();
    }

Take also a look at JDBC tutorials .

If you're using EJB, the idiom is to use entity beans for database interaction. If you're using EJB3, you should be creating an object and using annotations to deal with the database. SQL is generated for you using JPA.

So if EJB is providing all that abstraction to help you, why do you feel the need to go back to the lower level and write a PreparedStatement? Maybe the real answer is to rethink your object model and see how the query could fit into an entity bean abstraction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM