繁体   English   中英

如何将SQL数组值从Java控制器传递到Scala模板

[英]how to pass sql array values from java controller to scala template

我是玩框架的新手。 我想将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)
    );

和斯卡拉神庙

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

我得到了错误

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

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

您需要在try块之外声明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将为null,并且您将在模板中遇到麻烦( NullPinterException )。 您可能要考虑将return语句移到try块的末尾,然后将另一条语句添加到catch块中以进行错误处理。

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

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

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

暂无
暂无

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

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