Servlet调用SQL Server2000存储过程实例
2006-11-23 11:36
295 查看
数据库中的存储过程:
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){}
a187
}
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;
}
}
}
相关文章推荐
- asp怎样调用sql server2000无参数存储过程
- 调用存储过程返回list实例
- Ajax调用存储过程简单实例
- 项目实例 声明包 创建存储过程,调用
- 存储过程从入门到熟练(多个存储过程完整实例及调用方法)_AX
- Tomcat请求处理(七) - Servlet实例的调用
- 存储过程从入门到熟练(多个存储过程完整实例及调用方法)
- 存储过程从入门到熟练(多个存储过程完整实例及调用方法)_AX -转载
- 实例:java调用存储过程
- EJB3.0实例教程如何调用存储过程
- oracle中job定时调用存储过程的实例
- SQL:多个存储过程完整实例及调用方法
- 存储过程从入门到熟练(多个存储过程完整实例及调用方法)_AX
- Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例
- servlet 调用 EJB 经典实例!
- mybatis 调用存储过程 返回游标 实例
- hibernate 调用存储过程 返回游标 实例
- php存储过程调用实例代码
- MySQL 存储过程实例 与 ibatis/mybatis/hibernate/jdbc 如何调用存储过程
- Web Services调用存储过程简单实例