views:

167

answers:

2

Hi,

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

A: 

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.

dfa
HI Dfa,Thanks for reply. i want to use prepared statement with EJB's createNativeQuery().. or is there any other way to use parameter query (as we use ? in prepared statement) in ejb3
A: 

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.

duffymo