日期:2014-05-18 浏览次数:20601 次
public class Servlet01 extends HttpServlet { protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,java.io.IOException{ String name= req.getParameter("name"); GetJdbc gj=new GetJdbc(); boolean flag=gj.getIn(name); if(flag==true){ RequestDispatcher rd=req.getRequestDispatcher("Yes.jsp");//。forward方式只能跳转到本web应用中的页面上。 req.setAttribute(name, "name"); System.out.print(name); rd.forward(req, resp);//跳转 } } }
package com.cme.cen; import java.sql.*; public class GetJdbc { String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// 将驱动器的包名和类名存储到字符串中 String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=Sales"; // 将数据库StuInfo的路径存储到字符串中 String userName = "sa"; // 默认用户名 String userPwd = "19881212"; // 安装sql server 2005时的密码 Statement sm = null; public static Connection dbConn = null;// 定义打开了的接口对象 ResultSet rs = null; // 构造方法 public GetJdbc() { try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); sm = dbConn.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public boolean getIn(String name){ String sql="select * from Employees"; boolean flag=false; try { rs = sm.executeQuery(sql); while(rs.next()){ if(rs.getString("姓名").trim().equals(name)){ flag=true; } } } catch (SQLException e) { e.printStackTrace(); } return flag; } }