score:1

Accepted answer

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.

score: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.


Related Query

More Query from same tag