问题描述
小弟现在很郁闷,做的那么多还是不行!哪位大哥帮忙下啊!就是一个登陆页面获取输入的用户名密码再查看数据库看有没有匹配的,如果有就跳到一个新页面!简单的很,可我老是出现问题,老是jdbc什么。。。sa用户登录失败的!换了好多方法都不行,我配置了jdktomcat安装了sqlsp4的补丁sql2000的jdbc驱动哪位给了例子就最好啦!!!获取输入的用户名密码再查看数据库看有没有匹配的,如果有就跳到一个新页面!就是这个功能的小例子!我的代码我全删了搞的头大!
解决方案
解决方案二:
自己先顶下!!!
解决方案三:
logindbpackagedb;importjava.sql.ResultSet;importjava.sql.SQLException;importmql.Login;publicclassLoginDB{privateMyDBmydb=null;publicLoginDB(){this.mydb=newMyDB();}publicbooleanselectlogin(Loginlogin){booleanflag=false;Stringsql;sql="select*fromUserLoginwherename='"+login.getName()+"'and'"+login.getPassword()+"'";mydb.openConnection();ResultSetrs=mydb.executeQuery(sql);try{while(rs.next()){flag=true;}}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnflag;}}login.javapackagemql;publicclassLogin{Stringname;Stringpassword;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}}数据库操作packagedb;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassMyDB{//privateStringdriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//sqlserver.jar//privateStringdriver="net.sourceforge.jtds.jdbc.Driver";//privateStringurl="jdbc:microsoft:sqlserver://localhost:1433;databaseName=UserLogin";privateStringdriver="com.mysql.jdbc.Driver";privateStringurl="jdbc:mysql://localhost:3306/userLogin";privateStringuser="root";privateStringpwd="123456";privateConnectionconn=null;privateStatementstmt=null;/***打开数据库*/publicvoidopenConnection(){try{Class.forName(driver);conn=DriverManager.getConnection(url,user,pwd);//stmt.getConnection();stmt=conn.createStatement();}catch(SQLExceptione){//TODOAuto-generatedcatchblockSystem.out.println("数据库打开失败!!!");e.printStackTrace();}catch(ClassNotFoundExceptione){//TODOAuto-generatedcatchblockSystem.out.println("数据库打开失败!!!");e.printStackTrace();}}/***关闭数据库*/publicvoidcloseConnection(){try{if(stmt!=null)stmt.close();if(conn!=null)conn.close();}catch(SQLExceptione){//TODOAuto-generatedcatchblockSystem.out.println("数据库关闭失败!!!");e.printStackTrace();}}/***insert,update,delete*/publicintexecuteUpdate(Stringsql){try{returnstmt.executeUpdate(sql);}catch(SQLExceptione){//TODOAuto-generatedcatchblockSystem.out.println("数据库操作失败!!!");e.printStackTrace();}return0;}/***select*/publicResultSetexecuteQuery(Stringsql){ResultSetrs=null;try{rs=stmt.executeQuery(sql);}catch(SQLExceptione){//TODOAuto-generatedcatchblockSystem.out.println("数据库查询失败!!!");e.printStackTrace();}returnrs;}}servletpackageservlet;importjava.io.IOException;importjava.sql.SQLException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importmql.Login;importdb.LoginDB;publicclassLoginServletextendsHttpServlet{/****/privatestaticfinallongserialVersionUID=1L;/***ThedoGetmethodoftheservlet.<br>**Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.**@paramrequesttherequestsendbytheclienttotheserver*@paramresponsetheresponsesendbytheservertotheclient*@throwsServletExceptionifanerroroccurred*@throwsIOExceptionifanerroroccurred*/publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}/***ThedoPostmethodoftheservlet.<br>**Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.**@paramrequesttherequestsendbytheclienttotheserver*@paramresponsetheresponsesendbytheservertotheclient*@throwsServletExceptionifanerroroccurred*@throwsIOExceptionifanerroroccurred*@throwsSQLException*/publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{LoginDBlg=newLoginDB();Loginlog=newLogin();log.setName(request.getParameter("userName"));log.setPassword(request.getParameter("password"));booleanok=lg.selectlogin(log);if(ok){request.getRequestDispatcher("success.jsp").forward(request,response);}else{request.setAttribute("errormess","login.form.error");request.getRequestDispatcher("index.jsp").forward(request,response);}}/***Initializationoftheservlet.<br>**@throwsServletExceptionifanerroroccurs*/}
应该可以吧你试试
解决方案四:
可能是你的jdbc写的试试2楼的代码吧
解决方案五:
sa用户登录失败,当然是你登录数据库的用户名或者密码错了啊和代码没问题的,要么新建一个用户,要么改下sa的登录密码
解决方案六:
用HIBERNATE吧!
解决方案七:
我是新手刚学的额上面都不怎么明白!!!我的代码:<%@pagecontentType="text/html;charset=gb2312"language="java"import="java.sql.*,com.tools.*"errorPage=""%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><scriptlanguage="JavaScript"type="text/Javascript"><!--functionisok(theform){if(theform.name.value==""){alert("请输入您的用户名");return(false);}if(theform.password.value==""){alert("请输入您的密码");return(false);}varpassword=theform.password.value;if((password.length>6)||(password.length<6)){alert("请输入六位密码");return(false);}}--></script><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/><title>登陆</title><styletype="text/css"><!--.style11{font-size:large;color:#660099;font-weight:bold;}.style12{color:#660099}.style13{color:#660099;font-weight:bold;}.style16{font-family:Georgia,"TimesNewRoman",Times,serif;font-size:xx-large;color:#660099;font-weight:bold;}.style17{color:#0066CC;font-size:large;}body{background-image:url(imges/987321.jpg);}.STYLE18{font-size:x-large}.STYLE19{font-size:x-large;color:#660099;font-weight:bold;}.STYLE20{font-size:36px;font-weight:bold;}--></style></head><%Stringusername=request.getParameter("username");Stringpassword=request.getParameter("password");session.setAttribute("username",username);session.setAttribute("password",password);if(username!=null&&password!=null){response.sendRedirect("check.jsp");}%><body><formmethod="post"action="check.jsp"onsubmit="returnisok(this)"><tablebgcolor=#CCCC99align="center"width="35%"height="181"border="1"cellpadding="7"><tr><tdheight="46"colspan="2"><divalign="center"class="style16"><imgsrc="imges/20085504328297_2.jpg"width="59"height="43"align="left"/>管理员登录</div></td></tr><tr><tdwidth="33%"height="38"><divalign="center"class="style11STYLE18">用户名:</div></td><tdwidth="67%"><divalign="center"class="style12"><inputname="username"type="text"class="style12"id="username"size="20"/></div></td></tr><tr><tdwidth="33%"height="51"><divalign="center"class="STYLE19">密码:</div></td><tdwidth="67%"><divalign="center"class="style13"><inputname="password"type="text"size="20"/></div></td></tr></table><palign="center"><inputname="b1"type="submit"class="style11"value="登录后台"/><inputname="b2"type="reset"class="style11"value="重填"></p><palign="center"class="STYLE20"><ahref="frist.jsp"class="STYLE19">退出</a></p></form></body>check.jsp<%@pagecontentType="text/html;charset=gb2312"language="java"errorPage=""%><%@pageimport="java.sql.*"%><html><head><title>认证码验证页面</title><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"><METAHTTP-EQUIV="Pragma"CONTENT="no-cache"><METAHTTP-EQUIV="Cache-Control"CONTENT="no-cache"><METAHTTP-EQUIV="Expires"CONTENT="0"></head><body><%Stringusername=(String)session.getAttribute("username");Stringpassword=(String)session.getAttribute("password");%><%StringSqlString="select*fromuserinfowheread_usename='"+username+"'andad_password='"+password+"'";StringdriverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";StringuserName="sa";StringuserPassWord="123";StringdbName="db_tao";Stringurl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;Connectionconn;Statementstmt=null;//PreparedStatementpstmt;try{//加载驱动程序Class.forName(driverName);//建立同数据库的连接对象conn=DriverManager.getConnection(url,userName,userPassWord);stmt=conn.createStatement();ResultSetrs=stmt.executeQuery(SqlString);if(rs.next()){out.print("这是在check.jsp页面");response.sendRedirect("zhuye.jsp");}}catch(Exceptioncfe){System.out.println(cfe.getMessage());}%></body></html>链接了后到check.jsp页面什么也没显示跳转不了zhuye.jsp
解决方案八:
解决方案九:
先看看你数据库可以正常使用嘛?
解决方案十:
有错误信息还是什么也没有啊输入的值能在数据库里得到结果集吗?
解决方案十一:
我想在你连接数据库时就错了,你应该先测试数据库看看连得上不。还有就是你加了sql2000的那个驱动包没有,你先试试吧!
解决方案十二:
我晕倒不是吧就一个登录呢至于这么复杂嘛
解决方案十三:
等着马上给你写一个
解决方案十四:
没提示出现错误。应该是你数据库没内容把。。是没有找到RS.NEXT()所以都没跳转!
解决方案十五:
if(rs.next()){out.print("这是在check.jsp页面"); 估计if语句就没有执行
解决方案:
privateStringdriver="com.mysql.jdbc.Driver";privateStringurl="jdbc:mysql://localhost:3306/userLogin";你是照着书写的吧!有很多东西都要改的,这个端口3306是你自己本机的端口吗?还是抄书上的。这个是连接mysql数据库的,你要连接sqlServer的话要用privateStringdriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";privateStringurl="jdbc:microsoft:sqlserver://localhost:你自己的端口号/userLogin";
解决方案:
飘过..