当我尝试关闭MySQL连接时出错

时间:2019-03-13 19:18:14

标签: java mysql database connection

当我尝试关闭MySQL连接时,我的页面显示为空白。我是否将关闭连接的位置正确?用户登录(正在运行),但是在userprofile.jsp中关闭MySQL连接似乎出现了错误。我认为这与连接的关闭有关,因为没有它就可以工作

重定向到用户个人资料页面的登录类

    try (PrintWriter out = response.getWriter()) {

        String name = request.getParameter("name");
        String pass = request.getParameter("pass");
        MyDb1 db = new MyDb1();
      Connection con = db.getCon();
      Statement stmt = con.createStatement();
     ResultSet rs = stmt.executeQuery("select uid,name,pass from lauraccc_fyp.register where email = '"+name+"' and  pass = '"+pass+"'");

     while ((rs.next())) {

        String uid = rs.getString("uid");


             HttpSession session=request.getSession(); 
          session.setAttribute("name",uid);

        RequestDispatcher dispatcher = request.getRequestDispatcher("/userprofile.jsp");
            dispatcher.forward(request, response);
         return;



  } catch (SQLException ex) {
       }




</head>
<body>

<div class="fh5co-loader"></div>

<%

try {

    String session_id =null;
    HttpSession session1=request.getSession(false); 

    if(session1!=null) {  
        session_id=(String)session1.getAttribute("name");  
    }

    Class.forName("com.mysql.jdbc.Driver");

    Connection  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/lauraccc_fyp", "lauraccc", "*");
    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery("select * from register where uid='"+session_id+"'");
    rs.next();

    String name = rs.getString("name");
    con.close();

}catch(Exception e) {
    out.println(e);
}
<h1>Welcome back <%out.print(name);%></h1>
%>

1 个答案:

答案 0 :(得分:0)

您的页面空白吗?我认为这是因为一切都很好。在您的try catch块中,根本没有输出。