日期:2014-05-20  浏览次数:20779 次

为什么会报错?

package cn.jbit.classobject;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import org.apache.log4j.Logger;

public class Test6 {
private static Logger logger = Logger.getLogger(Test6.class.getName());

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
System.out.println("请输入姓名");
String name = input.next();
System.out.println("请输入密码");
int pwd = input.nextInt();
// 1.加载驱动
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO: handle exception
logger.error(e);
}
try {
// 2、建立连接
conn = DriverManager.getConnection(
"jdbc:sqlserver//:localhost:1433;DatabaseName=stuDB", "sa",
"ok");
// 判断宠物主人是否登陆成功
stmt = conn.createStatement();
String sql = "select * from dog where name='" + name + "'and love="
+ pwd;
System.out.println(sql);
rs = stmt.executeQuery(sql);
if (rs.next()) {
System.out.println("登陆成功,欢迎您!");
} else {
System.out.println("登录失败,请重新输入!");
}
} catch (Exception e) {
// TODO: handle exception
logger.error(e);
} finally {
if (rs != null) {
rs.close();//Unhandled exception type SQLException
}
if (stmt != null) {
stmt.close();//Unhandled exception type SQLException
}
if (conn != null) {
conn.close();//Unhandled exception type SQLException
}
}
}
}


最后三句关闭的话,为什么会报注释中的错误,为什么必须加到try...catch才不会报错?

------解决方案--------------------
楼上正解 Connection conn = null; Statement stmt = null; ResultSet rs = null;这三个操作都有可能抛异常出来,所以也得捕获。
------解决方案--------------------
void close()
           throws SQLException
Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released. 
Calling the method close on a Connection object that is already closed is a no-op. 

It is strongly recommended that an application explicitly commits or rolls back an active transaction prior to calling the close method. If the close method is called and there is an active transaction, the results are implementation-defined. 



Throws: 
SQLException - SQLException if a database access error occurs


以上是api
------解决方案--------------------
关闭对象也要捕获异常,try一下
------解决方案--------------------
肯定要放在try{}catch(){}里面!
理由,请看API:

API文档中文:
-----------------------------
close
void close()
    throws SQLException立即释放此 Connection 对象的数据库和 JDBC 资源,而不是等待它们被自动释放。 
    在已经关闭的 Connection 对象上调用 close 方法无操作 (no-op)。 
建议最好在调用 close 方法之前,应用程序显式提交或回滚一个活动事务。如果调用 close 方法并且有一个活动事务,那么结果将由实现定义。 
抛出: 
SQLException - 如果发生数据库访问错误

------解决方案--------------------
这三个一般都需要处理

------解决方案--------------------
注意到我发给你的api文档?
注意到里面最后一局没有?
api文档写道:
   抛出: 
   SQLException - 如果发生数据库访问错误
根据api这句“抛出......”,
所以,要抛异常:
try{
   conn.close();
}catch(SQLException err){err.printStackTrace();}
-----------------------
这就是原因!结贴吧
------解决方案--------------------
引用:
楼上正解 Connection conn = null; Statement stmt = null; ResultSet rs = null;这三个操作都有可能抛异常出来,所以也得捕获。