如何将SQL数组值从Java控制器传递到Scala模板 [英] how to pass sql array values from java controller to scala template
问题描述
我是新手.我想将java控制器中的数组变量传递给scala模板.
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)
);
和斯卡拉神庙
@(computerForm: Form[Computer],createFormRset: String)
我遇到了错误
cannot find symbol [symbol: variable rset] [location: class controllers.Application]
我需要将rset
值传递给scala模板.但是我不知道该如何帮助我
I need to pass rset
value to scala template . But I don't know how please help me
推荐答案
您需要在try-block之外声明rset
:
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)
);
这种解决方案并不是很漂亮,因为如果发生SQL异常,则rset
将为null,并且您的模板(NullPinterException
)会遇到麻烦.您可能需要考虑将return语句移到try块的末尾,然后将另一个语句添加到catch块中以进行错误处理.
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.
这篇关于如何将SQL数组值从Java控制器传递到Scala模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!