• 初涉JSP+JDBC 基于SQL2008的登陆验证程序


    简单的以代码的形式纪念一下,因为现在还没有解决SQL2008驱动的问题,并且有好多东西要学,所以日后会有更新~

    所安装的软件有:SQL2008,eclipse,tomcat,JDK,涉及环境配置、等等,安装数据库感受最深,虽然网上有教程,但是实际操作起来,真心感觉这才是真正的安装软件。

    设计关键:

       关键在于验证页面,在该页面中必须:数据库的连接操作,数据库记录的查询操作

    提交界面代码

    <%@ page language="java"  pageEncoding="GB2312"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户提交页面</title>
    </head>
    <body>
    <form action ="ch07_7_yanzheng.jsp"method="post">
    用户名:<input type="text"name="username">
    用户密码:<input type="password"name="pass"><br></br>
    <input type="submit"value="登录">
    </form>>
    </body>
    </html>

    截图:

    验证界面代码:

    <%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%>

    <html> <head>

    <title>登录验证界面</title>

     </head> <body>

    <% Connection conn =null;

    PreparedStatement pstmt =null;

    ResultSet rs=null;

    try{    String driverName ="com.sql.jdbc.Driver";  

    String dbName="user";  

    String url1="jdbc:sql://localhost/"+dbName;  

    String url2="?user=sa&password=lx13784429739";

     String url3="&Unicode=true&characterEncoding=GB2312";  

    String url=url1+url2+url3;  Class.forName(driverName);  

    conn=DriverManager.getConnection(url);  

    request.setCharacterEncoding("GB2312");  

    String name=request.getParameter("username");

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

    String sql="select * from user_b where(uname=?andupassword=?) ";    

    pstmt =conn.prepareStatement(sql);  

    pstmt.setString(1,name);  

    pstmt.setString(2,pw);  

    rs=pstmt.executeQuery();

     if(rs.next()){  %><%=name%>:登陆成功!<br><%    

    }  else{%>

     <%=name %>:登录失败!<br><%}  

    }catch(Exception e)   

       {%>                出现异常错误!<br><%=e.getMessage()%>  <%}

    finally{    if(rs!=null){rs.close();}   

     if(pstmt!=null){pstmt.close();}  

         if(conn!=null){conn.close();}    

       }%>

      </body> </html>

    数据库代码过于简单就不给出了。

    在编写验证界面期间,遇到了些问题,所以在CSDN、博客园、jsp吧上提了一下问题,得到了热心网友的帮助,在这里表示感谢~

    http://tieba.baidu.com/p/3702849992

    http://ask.csdn.net/questions/175550

    遇到的问题:

    即 statement  result  connection  的定义 应该放在try的外面  否则在finally里面不能调用,其实提问之前已经意识到是变量定义的范围的问题,就是没能解决,这个值得自己考虑。

    还需解决的问题:

    数据库连接的问题

    计划:

    吃透JSP+JDBC的开发模式,学习javabeen,servlet,近期进行JSP、JAVABEEN、SERVLET、JDBC间的各种组合开发。

  • 相关阅读:
    Docker安装以及运行第一个HelloWorld
    logstash-配置文件详解
    oh my zsh 常用插件
    Linux之Shell基本命令
    Linux的基本命令
    Vue
    rest_framwork之认证组件,权限组件,频率组件
    rest_framwork之序列化组件
    rest_framwork之APIView
    中间件
  • 原文地址:https://www.cnblogs.com/revenge/p/4430521.html
Copyright © 2020-2023  润新知