0

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

shall i change anything in template file
You should change the type of the second variable to ResultSet.
getting error not found: type ResultSet and my template look like this after i change @(message: Array[ResultSet]) i am only passing rset value to views
In the Template: replace Array[ResultSet] with java.sql.ResultSet. First: You do not pass an array of ResultSet. It is only one ResultSet. Second in the template you do not have the import-statements from the controller. Further: The Play-framework does not work the way you try to use it. Create a model-class which handles the SQL-stuff. In the controller, get a list/array of your model-class-instances which represent the data. Pass them to the view and display them. Have a look at the tutorials and examples provided in the play framework documentation.
0

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.

1 Comment

its easy when using ebean. but i am using core sql query. i am using try and cach method. so kindly help me how to do this. i am struggling more than a week for this. no clarification still now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.