简体   繁体   English

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

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

I am new to play framework. 我是玩框架的新手。 I want to pass array variables in 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)
    );

and scala templete 和斯卡拉神庙

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

i got the error 我得到了错误

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

I need to pass rset value to scala template . 我需要将rset值传递给scala template。 But I don't know how please help me 但我不知道该如何帮助我

You need to declare rset outside of your try-block: 您需要在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)
    );

This solution is not really pretty, because if an SQL-Exception occurs, rset will be null and you will run into troubles in your template ( NullPinterException ). 这个解决方案并不是很漂亮,因为如果发生SQL异常,则rset将为null,并且您将在模板中遇到麻烦( NullPinterException )。 You might want to consider to move your return statement at the end of the try-block and add another one into the catch block for error handling. 您可能要考虑将return语句移到try块的末尾,然后将另一条语句添加到catch块中以进行错误处理。

Basically you can pass any java object to the template. 基本上,您可以将任何Java对象传递给模板。 Play framework has type-checking on views so you would have to declare the rset. Play框架对视图进行类型检查,因此您必须声明rset。 If you look at the computer-database sample that comes with Play, you'll see it passes a Page object and three strings: 如果查看Play附带的计算机数据库示例,您会看到它传递了Page对象和三个字符串:

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

However, you may find it easier to copy the values from rset into your computerForm object, or another POJO, and pass that to the template. 但是,您可能会发现将rset中的值复制到您的computerForm对象或另一个POJO中并将其传递到模板更容易。

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

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