初学java web 关于JDBC连接对象关闭问题?

package myweb2;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.mysql.jdbc.Connection;
import java.sql.Connection;
import com.mysql.jdbc.PreparedStatement;

public class LoginServlet extends HttpServlet {

/**
 * Constructor of the object.
 */
public LoginServlet() {
    super();
}

/**
 * Destruction of the servlet. <br>
 */
public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
}

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        if(username==null||username.equals("")||password==null||password.equals("")){
            System.out.println("您输入的用户名和密码有误");
            return;
        }
        DBManager dbmanager=null;
        try {
            Connection conn = dbmanager.getConn();
            PreparedStatement pst=(PreparedStatement) conn.prepareStatement("select * from mytable where username=? AND password=?");
            pst.setString(1,username);
            pst.setString(2,password);
            ResultSet rs=pst.executeQuery();
            if (rs.next()){
                System.out.println("访问成功");
            } else { System.out.println("访问失败 ");}
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
        conn.close();
            };

if(username=="woniu"&&password=="123456"){


}
}
 问题:  conn.close(); 老是报错,提示有问题,关不不来,是怎么回事啊,那个大神给看看啊

先关掉rs试试
rs.close();
conn.close();

把 Connection conn = dbmanager.getConn();这句话放在try-catch的外面,因为conn在finnally 下根本和获取不了你那个conn。需要把connection作为一个全局变量。

谢谢,两位!!我把conn放到try-catch外面,然后又把conn.close() try-catch了就可以了
Connection conn = null;
try {
conn = dbmanager.getConn();
PreparedStatement pst=(PreparedStatement) conn.prepareStatement("select * from mytable where username=? AND password=?");
pst.setString(1,username);
pst.setString(2,password);
ResultSet rs=pst.executeQuery();
if (rs.next()){
System.out.println("访问成功");
} else { System.out.println("访问失败 ");}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};

跟连接池不一样,这个得你手动关闭,不然当使用的次数过多后,会被阻塞,因为链接jdbc是有限的。

Connection conn = dbmanager.getConn();
在try{
//声明出:局部变量
}catch
(){}
finally{
//使用,范围超出局部,没找到这个变量
}