如何将sql数组值从java控制器传递给scala模板

时间:2013-10-25 12:39:43

标签: java scala playframework

我是新玩的框架。我想将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模板。但我不知道请帮助我

2 个答案:

答案 0 :(得分:1)

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

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异常,rset将为空,您将在模板(NullPinterException)中遇到麻烦。您可能需要考虑在try-block的末尾移动return语句,并将另一个语句添加到catch块中以进行错误处理。

答案 1 :(得分:0)

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

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

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