Servlet调用SQL Server2000存储过程实例

 
数据库中的存储过程:
CREATE PROC getuser1
@name         VARCHAR(50),
@pwd           VARCHAR(50),
@count        int OUTPUT             -- 要输出的参数
--@myname VARCHAR(50) OUTPUT,
--@mypwd    VARCHAR(50) OUTPUT
AS
BEGIN
      SELECT @count =count(*) FROM admin WHERE name=@name and pwd=@pwd
   --   SELECT @myname =name , @mypwd=pwd FROM admin WHERE --name=@name and pwd=@pwd
END
GO

 本存储过程模拟用户登陆,输入用户名,密码,返回用户数,用户存在返回1,否则返回0;

 

服务器类:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
 
 
public class CallableStatementExample extends HttpServlet {
 
       /**
        * 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 {
                     doPost(request,response);
       }
 
       /**
        * The doPost method of the servlet. <br>
        *
        * This method is called when a form has its tag value method equals to post.
        *
        * @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 doPost(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException {
 
              Connection conn=null;
              Statement stm = null;
              ResultSet rs = null;
              String name="",pwd="";
              String myname="",mypwd="";
              int count=0,i=0;
             
              name = request.getParameter("name");
              pwd = request.getParameter("pwd");
              /*Enumeration para = request.getParameterNames();
              while(para.hasMoreElements()){
                     String nextpara = (String)para.nextElement();
                     temp[i++] = request.getParameter(nextpara);
              }
              */
              try{
                     conn = new conn().connect();
                     CallableStatement cstmt = conn.prepareCall("{ call getuser1(?,?,?) }");
                     cstmt.setString(1,name);//设置第一个参数(输入)
                     cstmt.setString(2,pwd);//设置第二个参数(输出)
                     cstmt.registerOutParameter(3,Types.INTEGER);//设置第三个参数(输出)
                     //cstmt.registerOutParameter(4,Types.VARCHAR);
                     //cstmt.registerOutParameter(5,Types.VARCHAR);
                     cstmt.execute();
                     count = cstmt.getInt(3);
              //     myname = cstmt.getString(4);
                     //mypwd = cstmt.getString(5);
              /*    stm = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                     String sql = "select * from admin where name='"+name+"' and pwd='"+pwd+"'";
                     rs = stm.executeQuery(sql);
                     while(rs.next()){
                            name = rs.getString("name");
                            pwd =rs.getString("pwd");
                     }
                     rs.last();
                     count = rs.getRow();*/
              }catch(Exception e){}
              finally{
                     try{
                     rs.close();
                     stm.close();
                     conn.close();
                     }catch(Exception e){}
                    
              }
              if(count>0){
                     javax.servlet.http.HttpSession session = request.getSession(false);
                     if(session == null){
                            session = request.getSession(true);
                            session.setAttribute("name",name);
                            session.setAttribute("pwd",pwd);
                     }
                     else{
                            session.setAttribute("name",name);
                            session.setAttribute("pwd",pwd);
                     }
                     //response.sendRedirect("/test/second.jsp?name="+name+"&pwd="+pwd);
                     response.sendRedirect("/test/second.jsp");
                     return;
              }
              else {
                     response.sendRedirect("/test/error.jsp");
                     return;
                     }
              }
       }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值