0

我是玩框架的新手。我想将 java 控制器中的数组变量传递给 scala 模板。

try {
            String userName = "data";

            String password = "data";

            String url = "jdbc:mysql://localhost/playdb";

            // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(url, userName, password);
            Statement stmt = con.createStatement();
            System.out.println("Connected database successfully...");
            String strSelect = "select * from computer";

            //statement.setString(1, name);
            ResultSet rset = stmt.executeQuery(strSelect);

            while(rset.next()) {   // Move the cursor to the next row
                String name = rset.getString("name");

                int    id   = rset.getInt("id");
                System.out.println( name + ", " + id);
                // ++rowCount;
            }


        }
        catch(SQLException e) {
            e.printStackTrace();
            System.out.println("cant Connected database successfully...");
        }
        Form<Computer> computerForm = form(Computer.class);
      return ok(
        createForm.render(computerForm,rset)
    );

和 scala Templete

    @(computerForm: Form[Computer],createFormRset: String)

我得到了错误

cannot find symbol [symbol: variable rset] [location: class controllers.Application]

我需要将rset值传递给 scala template 。但我不知道如何请帮助我

4

2 回答 2

1

您需要rset在 try-block 之外声明:

ResultSet rset = null;
try {
            String userName = "data";

            String password = "data";

            String url = "jdbc:mysql://localhost/playdb";

            // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(url, userName, password);
            Statement stmt = con.createStatement();
            System.out.println("Connected database successfully...");
            String strSelect = "select * from computer";

            //statement.setString(1, name);
            rset = stmt.executeQuery(strSelect);

            while(rset.next()) {   // Move the cursor to the next row
                String name = rset.getString("name");

                int    id   = rset.getInt("id");
                System.out.println( name + ", " + id);
                // ++rowCount;
            }


        }
        catch(SQLException e) {
            e.printStackTrace();
            System.out.println("cant Connected database successfully...");
        }
        Form<Computer> computerForm = form(Computer.class);
      return ok(
        createForm.render(computerForm,rset)
    );

这个解决方案不是很漂亮,因为如果发生 SQL 异常,它将为 null,并且您将在模板 ( )rset中遇到问题。NullPinterException您可能需要考虑将 return 语句移到 try 块的末尾,并将另一个语句添加到 catch 块中以进行错误处理。

于 2013-10-30T07:56:04.003 回答
0

基本上,您可以将任何 java 对象传递给模板。Play 框架对视图进行类型检查,因此您必须声明 rset。如果您查看 Play 附带的计算机数据库示例,您会看到它传递了一个 Page 对象和三个字符串:

@(currentPage: com.avaje.ebean.Page[Computer], currentSortBy: String, currentOrder: String, currentFilter: String)

但是,您可能会发现将值从 rset 复制到您的 computerForm 对象或另一个 POJO 中,并将其传递给模板更容易。

于 2013-10-25T14:04:10.697 回答