问题描述
最近在自学jsp的内容,遇到了一个问题,希望各位大虾帮忙解决一下。importjava.io.*;importjava.sql.*;importjava.util.*;publicclasssqlBean{publicConnectionconn=null;publicResultSetrs=null;privateStringdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";privateStringurl="jdbc:sqlserver://localhost:1433;DatabaseName=工资管理系统";publicvoidsetDriver(Stringdriver){this.driver=driver;}publicStringgetDriver(){return(this.driver);}publicvoidsetUrl(Stringurl){this.url=url;}publicStringgetUrl(){return(this.url);}publicvoidsqlBean(){try{Class.forName(driver);}catch(java.lang.ClassNotFoundExceptione){System.err.println("加载驱动器有误:"+e.getMessage());System.out.print("执行插入有错误:"+e.getMessage());}}publicintexecuteInsert(Stringsql){intnum=0;try{conn=DriverManager.getConnection(url,"sa","111111");Statementstmt=conn.createStatement();num=stmt.executeUpdate(sql);}catch(SQLExceptione){System.err.println("执行插入有错误:"+e.getMessage());System.out.print("执行插入有错误:"+e.getMessage());}closeDataBase();returnnum;}publicResultSetexecuteQuery(Stringsql){rs=null;try{conn=DriverManager.getConnection(url,"sa","111111");Statementstmt=conn.createStatement();rs=stmt.executeQuery(sql);}catch(SQLExceptione){System.err.println("执行查询有错误:"+e.getMessage());System.out.print("执行查询有错误:"+e.getMessage());}closeDataBase();returnrs;}publicintexecuteDelete(Stringsql){intnum=0;try{conn=DriverManager.getConnection(url,"sa","111111");Statementstmt=conn.createStatement();num=stmt.executeUpdate(sql);}catch(SQLExceptione){System.err.println("执行删除有错误:"+e.getMessage());System.out.print("执行删除有错误:"+e.getMessage());}closeDataBase();returnnum;}publicvoidcloseDataBase(){try{conn.close();}catch(Exceptionend){System.err.println("执行关闭Connection对象有错误"+end.getMessage());System.out.print("执行关闭Connection对象有错误"+end.getMessage());}}}importjava.io.*;importjava.sql.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclasslogin_checkextendsHttpServlet{publicvoiddoPost(HttpServletRequestreq,HttpServletResponseres)throwsServletException,IOException{Stringmessage=null;Stringid=null;id=(String)req.getParameter("id");HttpSessionsession=req.getSession(true);session.setAttribute("id",String.valueOf(id));Stringpassword=null;password=(String)req.getParameter("password");Stringkind=null;kind=(String)req.getParameter("kind");Stringtemp=getPassword(req,res,id,kind);if(password.equals(temp))goo(req,res,kind);else{message="密码或用户名有误!!!";doError(req,res,message);}}publicvoidgoo(HttpServletRequestreq,HttpServletResponseres,Stringkind)throwsServletException,IOException{if(kind.equals("职工")){RequestDispatcherrd=getServletContext().getRequestDispatcher("/worker.jsp");rd.forward(req,res);}if(kind.equals("管理员")){RequestDispatcherrd=getServletContext().getRequestDispatcher("/admin.jsp");rd.forward(req,res);}}publicStringgetPassword(HttpServletRequestreq,HttpServletResponseres,Stringid,Stringkind)throwsServletException,IOException{sqlBeandb=newsqlBean();Stringpw="";Stringsql="selectpasswordfrom"+kind+"whereid='"+id+"'";try{ResultSetrs=db.executeQuery(sql);if(rs.next()){pw=rs.getString("password");}}catch(Exceptione){System.out.print(e.toString());}returnpw;}publicvoiddoError(HttpServletRequestreq,HttpServletResponseres,Stringstr)throwsServletException,IOException{req.setAttribute("problem",str);RequestDispatcherrd=getServletContext().getRequestDispatcher("/errorpage.jsp");rd.forward(req,res);}publicvoiddoGet(HttpServletRequestreq,HttpServletResponseres)throwsServletException,IOException{/*Stringaction=req.getParameter("action");if("logout".equalsIgnoreCase("action")){HttpSessionsession=req.getSession(true);session.invalidate();RequesDispatcherrd=getServletContext().getRequestDispatcher("/ongin.jsp");rd.forward(req,res);}*/doPost(req,res);}}运行的时候总是跳转到errorpage.jsp,不能跳转到worker.jspandadmin.jsp
解决方案
解决方案二:
忘了说了,kind是一个radioz中的数据
解决方案三:
你的URL写错了URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";