我使用 Play Framework 1.2.5 编写了以下 JDBC 代码,用于从数据库 Oracle 10g 中获取一些数据:
Connection conn = DB.getConnection();
PreparedStatement stmt = null;
System.out.println(conn);
try {
stmt = conn.prepareStatement("select dept_id from emp where emp_id = 11");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println("Dept Id: " + rs.getInt("dept_id"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这种方法有效,但我有一个困惑:
如果我注释整个代码块并运行应用程序,那么我可以在控制台中看到消息,说明已与数据库建立连接。因此:
1) 上述代码块是从 Oracle DB 获取数据的正确方法还是存在比这更好的方法?
2)在整个应用程序生命周期中,与数据库的连接是否会持续存在?
我是这方面的新手,因此很挣扎:(
请让我知道如何继续此操作。
问候
最佳答案
您需要阅读play JPA文档:http://www.playframework.org/documentation/1.2.5/jpa#finding
您的查询应如下所示:
Post.find("byTitle", "My first post").fetch();
Post.find("byTitleLike", "%hello%").fetch();
Post.find("byAuthorIsNull").fetch();
Post.find("byTitleLikeAndAuthor", "%hello%", connectedUser).fetch();
如果需要进行完整查询,可以在find中添加JPA代码:
Post.find(
"select p from Post p, Comment c " +
"where c.post = p and c.subject like ?", "%hop%"
);
关于java - 如何在 Play 框架中执行查询(使用 Oracle 10g),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514559/