为什么下面finally里面的东西有问题呢
package Student;import java.sql.*;public class Student {
public static void main(String[] args){
Connection con=null;
PreparedStatement pStatement=null;
ResultSet rs=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException se){
System.out.println("找不到数据库驱动");
}
try{
con=DriverManager.getConnection("jdbc:odbc:Temp");
String sql="select * from student";
pStatement=con.prepareStatement(sql);
rs=pStatement.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
}
}catch(SQLException se){
se.printStackTrace();
}finally{
rs.close();
pStatement.close();
con.close(); }
}
}
package Student;import java.sql.*;public class Student {
public static void main(String[] args){
Connection con=null;
PreparedStatement pStatement=null;
ResultSet rs=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException se){
System.out.println("找不到数据库驱动");
}
try{
con=DriverManager.getConnection("jdbc:odbc:Temp");
String sql="select * from student";
pStatement=con.prepareStatement(sql);
rs=pStatement.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
}
}catch(SQLException se){
se.printStackTrace();
}finally{
rs.close();
pStatement.close();
con.close(); }
}
}
解决方案 »
- String s1 = new String("abc");String s2 = new String("abc");产生了几个对象
- 关于JAVA静态数组问题 急 在线等
- Java源程序读不出来了???急救!!新手
- 我是JAVA新手,我安装了JDK1.7系统变量也配置好了,但是为什么我在运行窗口里输入javac却显示'javac'不是内部或外部命令,也不是可运行的程序或批处理文件。
- 关于String对象问题,请教!
- 数组中的元素读出来全部是0?
- 求救!!关于鼠标响应事件的问题
- eclipse生成jar文件,想要把配置文件单独放在外面,试了好久没成功
- 一个很菜的问题,请大家进来看看。
- JAVA IN CHINA邀请函(无须请柬即可参加)
- 关于字符串反转???
- 关于使用java gui实现一种界面效果的方案???
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
} if(....) // 照上面这样关闭另外两个。
}
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class JdbcUtil { public static void close(Connection con, Statement stat, ResultSet rs) {
close(rs);
close(stat);
close(con);
} public static void close(Connection con, Statement stat) {
close(con, stat, null);
} public static void close(Connection con, ResultSet rs) {
close(con, null, rs);
} public static void close(Connection con) {
if(con != null) {
try {
con.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
} public static void close(Statement stat) {
if(stat != null) {
try {
stat.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
} public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}finally 中改成:} finally {
JdbcUtil.close(con, pStatement, rs);
}