java 返回 ResultSet 的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11902002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way to return a ResultSet
提问by locrizak
I'm new to java but I'm picking it up pretty quickly. One thing that I keep running into is I end up having one function that is full of queries and just code in general and I would like to break it down into separate functions. Take this for example:
我是 Java 新手,但我很快就学会了。我一直遇到的一件事是我最终拥有一个充满查询和一般代码的函数,我想将其分解为单独的函数。以这个为例:
public ResultSet getApples (){
ResultSet rs;
try{
PreparedStatement stmt = con.prepareStatement("SELECT * FROM fruit WHERE type='apples'");
rs = stmt.executeQuery();
} catch (SQLException e){
e.printStackTrace();
}
return rs;
}
Ideally this would be what I want to do, have all of try's and catches within one function, but this gives me the error: Local variable may not have been initilized
理想情况下,这就是我想要做的,在一个函数中包含所有尝试和捕获,但这给了我错误: Local variable may not have been initilized
I do realize I could do this:
我确实意识到我可以这样做:
public function start(){
try{
ResultSet apples = getApples();
catch (SQLException e){
e.printStackTrace();
}
}
public ResultSet getApples () throws SQLException {
PreparedStatement stmt = con.prepareStatement("SELECT * FROM fruit WHERE type='apples'");
return stmt.executeQuery();
}
But I would really rather have the exceptions handled within the function as well as it return a result.
但我真的宁愿在函数内处理异常并返回结果。
EDITAlright so kinda a modififed answer to whats being provided. My whole goal on this question was to make the main functions of my script as clean as possible. Even the extra if ( _resultSet != null )
was something that I didn't really like. That being said I am pretty happy with this result:
编辑好吧,对所提供的内容有一个修改的答案。我在这个问题上的整个目标是使我的脚本的主要功能尽可能干净。即使是额外的if ( _resultSet != null )
东西也是我不太喜欢的。话虽如此,我对这个结果非常满意:
public ResultSet getApples (){
try{
PreparedStatement stmt = con.prepareStatement("SELECT * FROM fruit WHERE type='apples'");
return stmt.executeQuery();
} catch (SQLException e){
System.out.println("************************");
System.out.println("Class.getApples null");
System.out.println(e.getMessage());
return null;
}
}
Everything is handled within the getApples
function and when _resultSet.next()
is called I get a NullPointerException
and the prints in the getApples exception so I am able to find the error and debug quickly.
一切都在getApples
函数内处理,当_resultSet.next()
被调用时,我得到 aNullPointerException
和 getApples 异常中的打印,因此我能够快速找到错误并进行调试。
回答by evg
Initialize rs to null first.
首先将 rs 初始化为 null。
public ResultSet getApples (){
ResultSet rs = null;
try{
PreparedStatement stmt = con.prepareStatement("SELECT * FROM fruit WHERE type='apples'");
rs = stmt.executeQuery();
} catch (SQLException e){
e.printStackTrace();
}
return rs;
}
回答by John Smith
You can declare your RS like this
你可以像这样声明你的 RS
ResultSet rs = null;
but where you call your function:
但是你在哪里调用你的函数:
ResultSet apples = getApples ()
you have to check:
你必须检查:
if(apples == null)
{
//do something, because your query did not work.
}
回答by Jignesh
Because you are not setting ResultSet rs to anything initial value. and at the end you are returning it. What if any exception occurs and rs value does not have value set in it. In order to solve you need to assign null value to rs when you declare.
因为您没有将 ResultSet rs 设置为任何初始值。最后你要归还它。如果发生任何异常并且 rs 值中没有设置值怎么办。为了解决您需要在声明时为 rs 分配空值。
回答by parsifal
The biggest problem that I see with your first example (other than not initializing rs
) is that you don't properly handle cleanup. You should have a finally
block that closes stmt
.
我在你的第一个例子中看到的最大问题(除了不初始化rs
)是你没有正确处理清理。你应该有一个finally
关闭的块stmt
。
One very good way to make sure that all of this happens is to use Spring's JDBCTemplate(more documentation here). This handles all of the connection management details for you; you simply write your SQL and code to process the ResultSet
. Better, it lets you use Spring's declarative transaction management.
确保所有这些都发生的一个很好的方法是使用 Spring 的JDBCTemplate(更多文档在这里)。这将为您处理所有连接管理细节;您只需编写 SQL 和代码来处理ResultSet
. 更好的是,它允许您使用 Spring 的声明式事务管理。
回答by havexz
You can use CachedRowSet. For detailed answer you can look at my answer here
您可以使用CachedRowSet。有关详细答案,您可以在此处查看我的答案