jsp+javabean+MySQL登陆校验[实例]

请大家给出意见或建议。谢谢!

jdk版本:1.4.2

tomcat版本:5.0

MySQL版本:5.0

 

一共三个文件:index.jsp,login.jsp,LinkBean.java

数据库名:ziyuan

表名:zyd_user

字段:usercode,username,password

 

 

 

<!-- [if gte mso 9]><![endif]--><!-- [if gte mso 9]><![endif]--><!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;mso-font-charset:0;mso-generic-font-family:roman;mso-font-pitch:variable;mso-font-signature:-1610611985 1107304683 0 0 159 0;}@font-face{font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;mso-font-charset:0;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-1610611985 1073750139 0 0 159 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-fareast-font-family:宋体;mso-bidi-font-family:"Times New Roman";mso-font-kerning:1.0pt;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;font-size:10.0pt;mso-ansi-font-size:10.0pt;mso-bidi-font-size:10.0pt;mso-ascii-font-family:Calibri;mso-fareast-font-family:宋体;mso-hansi-font-family:Calibri;mso-font-kerning:0pt;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:36.0pt;mso-footer-margin:36.0pt;mso-paper-source:0;}div.Section1{page:Section1;}--><!-- [if gte mso 10]><![endif]-->

Index.jsp

<% 

 response.sendRedirect("login.jsp"); 

%>


Login.jsp

<%@page contentType="text/html;charset=gb2312"import="java.sql.*" language="java"%>

<%@page import="util.login.logon.LinkBean"%>

 

<html>

    <head>

        <title>登陆!</title>

    </head>

    <body>

        <form method="post">

            <table border="0"width="898">

                <tr>

                    <tdwidth="890" align="left" colspan="2">

                        <b> <fontsize="5">开始登陆!!! </font> </b>

                        <hr>

                    </td>

                </tr>

                <tr>

                    <td width="75"align="center">

                        <palign="right">

                            账号:

                        </p>

                    </td>

                    <td width="809"align="center">

                        <palign="left">

                            <inputtype="text" name="memberid" size="20">

                    </td>

                </tr>

                <tr>

                    <td width="75"align="center">

                        <palign="right">

                            密码:

                        </p>

                    </td>

                    <tdwidth="809" align="center">

                        <palign="left">

                            <inputtype="password" name="memberpwd" size="20">

                            <inputtype="submit" value="登陆"name="loginButton">

                    </td>

                </tr>

            </table>

 

            <jsp:useBean id="Link"scope="page" class="util.login.logon.LinkBean" />

            <%

                String code =request.getParameter("memberid");

                String pass =request.getParameter("memberpwd");

                ResultSet rs = null;

                Connection conn = null;

                boolean flag=false;       

 

                if (code != null &&pass != null) {

                    String sql = "select *from zyd_user where usercode='" + code + "' and password='" +pass + "'";

                    conn = Link.connectdb();

                    rs =Link.executeQuery(sql);

 

                    if (rs.next()) {

                        flag = true;

                    }              

               

                    if (flag)

                    {

                    //合法用户

                       response.sendRedirect("main.jsp");

                    }

                    else

                    {

                    //非法用户

                       response.sendRedirect("login_failure.jsp");           

                    }            

                }

            %>

 

        </form>

    </body>

</html>


LinkBean.java

packageutil.login.logon;

 

importjava.sql.*;

importjava.lang.*;

 

publicclass LinkBean

{

  StringsDBDriver="org.gjt.mm.mysql.Driver";

  private Stringurl="jdbc:mysql://localhost:3306/ziyuan";

  String user="root";   //root为用户名称,12345为密码

  String password="12345";

  private Connection conn=null;

  private Statement stmt=null;

  ResultSet rs=null;

 

  public LinkBean()

          {

          }

          

  public Connection connectdb()

              {

                   try

                     {

 //                        Class.forName(sDBDriver).newInstance();

                         Class.forName(sDBDriver);

                     }

                    catch(java.lang.ClassNotFoundException e)

//                  catch (Exception e)

                     {

                    System.err.println("conectdb(): " + e.getMessage());

//                     e.printStackTrace();

//                      System.out.println(e.toString());

                    }

              return conn;

              }

             

   public ResultSet executeQuery(String sqls)

          {

              rs=null;

                  try

                  {

                     conn=DriverManager.getConnection(url,user,password); 

                     stmt=conn.createStatement();

                     rs=stmt.executeQuery(sqls);

                   }

                  catch(Exception ex)

                   {

                  System.err.println("aq.executeQuery: " + ex.getMessage());

//                   e.printStackTrace();

//                       System.err.println("aq.executeQuery: " + ex.getMessage());

                   }

              return rs;

           }

 

       public void executeUpdate(String sql)

               {

                   stmt=null;

                   rs=null;

                   try

                     {

                           conn=DriverManager.getConnection(url,user,password);

                           stmt=conn.createStatement();

                            stmt.executeUpdate(sql);

                            stmt.close();

                            conn.close();

                      }

                    catch(SQLException ex)

                      {

                            System.err.println("aq.executeQuery: " + ex.getMessage());

                      }

                }

 

      public void closeStmt()

              {

                     try

                       {

                             stmt.close();

                       }

                     catch(SQLException e)

                       {

                            e.printStackTrace();

                       }

               }

 

      public void closeConn()

              {

                     try

                       {

                              conn.close();

                       }

                     catch(SQLException e)

                       {

                           e.printStackTrace();

                       }

              }

 

 

   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值