初学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(); 老是报错,提示有问题,关不不来,是怎么回事啊,那个大神给看看啊

5个回答

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

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

tyz_0232
qiaohe-1 正确答案
大约 4 年之前 回复

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

tyz_0232
qiaohe-1 正解。。。。。
大约 4 年之前 回复

谢谢,两位!!我把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是有限的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问