how to pass sql array values from java controller to scala template

北慕城南 提交于 2020-01-03 04:35:07

问题


I am new to play framework. I want to pass array variables in java controller to scala template.

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 . But I don't know how please help me


回答1:


You need to declare rsetoutside of your 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)
    );

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). 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.




回答2:


Basically you can pass any java object to the template. Play framework has type-checking on views so you would have to declare the rset. If you look at the computer-database sample that comes with Play, you'll see it passes a Page object and three strings:

@(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.



来源:https://stackoverflow.com/questions/19590140/how-to-pass-sql-array-values-from-java-controller-to-scala-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!