简体   繁体   中英

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.

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

You need to declare rset outside 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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