结果为null,程序没有输入if语句

时间:2015-03-07 15:10:41

标签: java servlets

// dopost方法

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String password = null;
       response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String parm;
        parm=request.getParameter("user_name");
        String User_name=parm;
        parm=request.getParameter("National");
        int National_ID=Integer.parseInt(parm);
        try{
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=My_Project;integratedSecurity=true;applicationName=WebApplication2;");
    String sqlquery="select * from Users where First_Name='" + User_name + "'and National_ID='" +National_ID + "'";
        PreparedStatement ps=con.prepareStatement(sqlquery);
    rs=ps.executeQuery();
        if(rs.next()){

            password=rs.getString(9);

         }
    }catch(SQLException e){} catch (ClassNotFoundException ex) {
        Logger.getLogger(Forget.class.getName()).log(Level.SEVERE, null, ex);
    }
             //ds.ss(User_name, National_ID);
             //String password=ds.password;
            //  String password=new DataBase().(User_name,National_ID);
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Forget</title>");            
            out.println("</head>");
            out.println("<body>");
           out.println("<h1>Your Password  " + password + "</h1>");
            out.println("</body>");
            out.println("</html>");

    }

//结果仍为空

1 个答案:

答案 0 :(得分:0)

首先我想告诉你,请在编写代码时坚持使用正确的java约定。当您发布时,请以适当的格式发布。使用异常处理和适当的异常消息或堆栈跟踪来识别问题。

我刚刚更改了你的帖子方法。使用它,看它是否有效。注意:我假设您的JDBC连接运行良好,没有任何问题:

&#13;
&#13;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
			
	response.setContentType("text/html;charset=UTF-8");
	PrintWriter out = null;

	String password = null;
	String userName = request.getParameter("user_name");
	String nationalVal = request.getParameter("National");
	int national=0;
	  if(national != null && !national.isEmpty()){
		 national = Integer.parseInt(nationalVal);
	  }
  
    ResultSet rs= null;
    Statement st=null;	
    Connection con = null;
	
    try{
	    out = response.getWriter();
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=My_Project;integratedSecurity=true;applicationName=WebApplication2;");
		
		String sqlQuery="select password from Users where First_Name='"+ User_name +"' and National_ID=" +National_ID ;
        st = con.createStatement();
		rs = st.executeQuery(sqlQuery);
		
        if(rs.next()){
			password = rs.getString(9);
        }
		
    }catch(SQLException e){
	  e.printStackTrace();
    }
	
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Servlet Forget</title>");            
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Your Password  " + password + "</h1>");
		out.println("</body>");
		out.println("</html>");

    }
&#13;
&#13;
&#13;

为您重新发布的问题重新发布答案。