gpt4 book ai didi

java - 如何将sql数组值从java Controller 传递到scala模板

转载 作者:行者123 更新时间:2023-12-01 13:56:37 25 4
gpt4 key购买 nike

我是刚玩框架的新手。我想将 java Controller 中的数组变量传递给 scala 模板。

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

和 scala 模板

    @(computerForm: Form[Computer],createFormRset: String)

我收到错误

cannot find symbol [symbol: variable rset] [location: class controllers.Application]

我需要将 rset 值传递给 scala template 。但我不知道该怎么办请帮助我

最佳答案

您需要在 try block 之外声明 rset:

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 block 的末尾,并将另一个语句添加到 catch block 中以进行错误处理。

关于java - 如何将sql数组值从java Controller 传递到scala模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19590140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com