Home > Database > Mysql Tutorial > body text

jdbc-mysql的JDBC封装类如何释放资源?

WBOY
Release: 2016-06-06 09:37:01
Original
1109 people have browsed it

mysqljdbcjava

在这个类中返回出一个Resultset结果,然后在另一个类中循环出来,有什么办法可以在这个类中直接释放资源?如果直接在后面加finally{...}会报错,初学求解

public ResultSet SQL_ExecuteQuery(String sql,Object [] p)
{
Connection con=openConnection();//打开数据库
PreparedStatement pst=null;//定义一个 执行对象
ResultSet rst=null;
try {
pst=con.prepareStatement(sql);//初始化执行对象
//循环设置参数
if(p!=null && p.length>0)
{
for (int i = 0; i pst.setObject((i+1), p[i]);//设置参数
}
}
rst=pst.executeQuery();
//最后执行命令 并返回结果
return rst;//执行命令
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

<code>            return null;        }</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!