简体   繁体   English

ejb3中准备好的语句

[英]prepared Statement in ejb3

i want to use prepared statement with ejb3 to insert data in oracle. 我想使用带有ejb3的预处理语句在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 还是有其他方法可以在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 . 另请参阅JDBC教程

If you're using EJB, the idiom is to use entity beans for database interaction. 如果您使用的是EJB,则习惯用法是使用实​​体bean进行数据库交互。 If you're using EJB3, you should be creating an object and using annotations to deal with the database. 如果使用的是EJB3,则应该创建一个对象并使用批注来处理数据库。 SQL is generated for you using JPA. 使用JPA为您生成SQL。

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? 因此,如果EJB提供了所有这些抽象来帮助您,为什么您觉得需要回到较低的层次并编写PreparedStatement? Maybe the real answer is to rethink your object model and see how the query could fit into an entity bean abstraction. 也许真正的答案是重新考虑您的对象模型,并查看查询如何适合实体bean抽象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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