qiaohe-1 2016-08-07 02:38 采纳率: 50%
浏览 1289

初学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条回答 默认 最新

  • bhtj27 2016-08-07 02:45
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决