servlet吧 关注:1,440贴子:1,219
  • 3回复贴,共1
在servlet中执行sql语句update,程序没有报错,但为什么数据库没有更新呢?!


IP属地:浙江1楼2018-08-24 21:09回复
    也试过加commit了,不过报错说,已经有autocommit了


    IP属地:浙江2楼2018-08-24 21:10
    回复


      IP属地:浙江3楼2018-08-24 21:17
      回复
        代码:
        public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
        Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        String indate=request.getParameter("checkindate");
        String outdate=request.getParameter("checkoutdate" );
        String floor=request.getParameter("floor" );
        String number=request.getParameter("number" );
        String sql="select * from room where floor='"+floor+"'and number='"+number+"'";
        String sql2="update room set is_checkin ='1'and time_out ='"+outdate+"' and time_in ='"+indate+"' where floor='"+floor+"'and number='"+number+"'";
        System.out.println("Connecting to database...");
        try {
        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/room","root","root");
        System.out.println("creating statement...");
        stmt=conn.createStatement();
        rs=stmt.executeQuery(sql);
        System.out.println(rs);
        System.out.println(rs.next());
        rs.previous();
        } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        try {
        if(rs.next())
        {
        stmt.executeUpdate(sql2);
        System.out.println("success");
        }
        else
        {
        System.out.println("error");
        }
        } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        }
        try {
        if(conn!=null)
        conn.close();
        if(rs!=null)
        rs.close();
        if(stmt!=null)
        stmt.close();
        } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();}
        response.setHeader("reflesh","0.1;url:/welcom.jsp");
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println(" <BODY>");
        out.println(" </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
        System.out.println("bye");
        }
        }


        IP属地:浙江4楼2018-08-24 21:18
        回复