2

我使用 Java 作为编程语言,使用 Play (2.4) 作为我的框架

    1 package controllers;
    2
    3 import java.sql.Connection;
    4 import play.db.*;
    5
    6 public class JDBCUtilities {
    7   
    8   
    9   Connection connection = DB.getConnection();

第 4 行突出显示为错误。在我的 Eclipse IDE 中,它似乎没有显示错误。

4

1 回答 1

1

在 Play 2.4.x 中,您应该要求它为您注入数据库。例如:

import play.db.Database;
import javax.inject.*;

public class MyController extends Controller {

    @Inject Database db;

    public Result index() {
        Connection conn = db.getConnection();
        // do something
        return ok("Hi");
    }

}

还要确保在 build.sbt 中有 jdbc 依赖项(注意这不包括 JDBC 驱动程序...):

libraryDependencies ++= Seq(
  // ...
  javaJdbc,
  // ...
)

更多信息:访问 SQL 数据库 - Play 2.4.x doc

于 2015-06-27T18:23:14.777 回答