• 软件工程概论课堂测试一


    课堂练习设计思想:先创建一个连接数据库的.Java文件,然后创建登录界面的HTML文件,然后分别在.JSP文件中编写实现登录注册的方法。

    源程序代码:

    login.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>登录</title>
    <meta charset="UTF-8">
    <script language="javascript">
    function turn(){
        window.location.href="register.html";
    }
    </script>
    </head>
    <body>
    
        <center>
            <h1 style="color:orange">登录</h1>
                <form action="logincheck.jsp" method="get">
                    <table border="0">
                        <tr>
                            <td>课程名称:</td>
                            <td><input type="text" name="name" required></td>
                        </tr>
                        <tr>
                            <td>任课教师:</td>
                            <td><input type="text" name="teacher" required>
                            </td>
                        </tr>
                        <tr>
                            <td>上课地点:</td>
                            <td><input type="text" name="address" required>
                            </td>
                    </table>
                <br>
                    <input type="submit" value="登录" style="color:#BC8F8F">
                    <input type="button" value="注册" onclick="turn()" style="color:#BC8F8F">
                </form>
    
        </center>
    </body>
    </html>

    logincheck.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>登录验证</title>
    </head>
    <body>
    <jsp:useBean id="login" class="db.Login"/>
    <%
        request.setCharacterEncoding("utf-8");
        String name=(String)request.getParameter("name");
        String teacher=(String)request.getParameter("teacher");
        String address=(String)request.getParameter("address");
        if(login.ifLogin(name,teacher,address)){
            out.print("成功登陆! 登录名:"+name);
        }
        else{
            out.print("填写错误!");
        }
        login.clean();
    %>
    <center>
        <a href="login.html">返回</a>
    </center>
    </body>
    </html>

    register.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>注册</title>
    </head>
    <body>
        <center>
            <h1 style="color:red">注册</h1>
                <form action="registercheck.jsp" method="post">
                    <table border="0">
                        <tr>
                            <td>课程名称:</td>
                            <td><input type="text" name="name" required></td>
                        </tr>
                        <tr>
                            <td>任课教师:</td>
                            <td><input type="text" name="teacher" required>
                            </td>
                        </tr>
                        <tr>
                            <td>上课地点:</td>
                            <td><input type="text" name="address" required>
                            </td>
                        </tr>
                    </table>
                </form>
                <br>
                    <input type="submit" value="保存"  onclick="confir()" style="color:#BC8F8F">
                    <input type="submit" value="返回"  onclick="back()" style="color:#BC8F8F">
    
        </center>
    <script language="javascript">
    function back(){
        window.history.back();
    }
    function confir(){
        var tea=document.forms[0].teacher.value;
        var add=document.forms[0].address.value;
        var n=document.forms[0].name.value;
        if(tea=="刘立嘉"||tea=="刘丹"||tea=="王建民"||tea=="杨子光"||tea=="王辉"){
            
        }
        else{
            alert("输入不符合要求!");
            break;
        }
        
            if((add.substr(0,2)=="基教")||(add.substr(0,2)=="一教")||(add.substr(0,2)=="二教")||(add.substr(0,2)=="三教"))
            {
                
                
            }
            else{
                alert("输入地点不符合要求!");
                break;
            }
            
                document.forms[0].submit();
            
        
    
    }
    </script>
    </body>
    </html>

    regustercheck.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!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>
    <jsp:useBean id="login" class="db.Login"/>
    <%
    request.setCharacterEncoding("utf-8");
    String name=request.getParameter("name");
    String teacher=request.getParameter("teacher");
    String address=request.getParameter("address");
    
    if(login.ifExitName(name)){
    
        out.print("<script language='javaScript'> alert('已存在该用户!');</script>");
        response.setHeader("refresh", "0;url=register.html");
    }
    else{
    
        login.addUser(name,teacher,address);
        out.print("<script language='javaScript'> alert('注册成功!');</script>");
        response.setHeader("refresh", "0;url=login.html");
    }
    login.clean();
    %>
    </body>
    </html>

    Login.java

    package db;
    
    import java.sql.*;
    
    public class Login{
        private String driverStr = "com.mysql.jdbc.Driver";
        private String connStr = "jdbc:mysql://localhost:3306/test";
        private String dbusername = "root";
        private String dbpassword = "root";
        private Connection conn = null;
        private PreparedStatement pstmt = null;
    
        public Login(){
            try
            {
                Class.forName(driverStr);
                conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
            } 
            catch (Exception ex) {
                System.out.println("Connect Fail!");
            } 
            
        }
        public boolean ifExitName(String name) throws SQLException{
            String sql="select * from lesson where name='"+name+"'";
            pstmt=conn.prepareStatement(sql);
            ResultSet res=pstmt.executeQuery();
            if(res.next()){
                return true;
            }
            return false;
        }
        public boolean ifLogin(String userna,String tea,String add)throws SQLException{
            String sql="select * from lesson where name='"+userna+"' and teacher='"+tea+"' and address='"+add+"'";
            pstmt=conn.prepareStatement(sql);
            ResultSet res=pstmt.executeQuery();
            if(res.next()){
                return true;
            }
            else{
                return false;
            }
        }
        public void addUser(String name,String tea,String add)throws SQLException{
            if(ifExitName(name)){
                return ;
            }
            else {
                String sql = "insert into lesson values('"+name+"','" +tea+"','"+add+"')";
                pstmt=conn.prepareStatement(sql);
                int rst=pstmt.executeUpdate();
                if(rst!=0){
                    System.out.println("Insert Success!");
                }
                else{
                    System.out.println("Insert Fail!");
                }
            }
            
        }
    
        public void clean(){
            try {
                if(conn!=null){
                    conn.close();
                }
                if(pstmt!=null){
                    pstmt.close();
                }
            }
            catch (SQLException e) {
                System.out.println(e.getMessage());;
            }
        }
    }

    运行结果截图:


    项目时间日志:

    日期 开始时间 结束时间 净时间 活动
    2017.11.28 9:10 9:30 20min 编写.Java连接数据库
    2017.11.28 9:35 10:30 55min 编写login.html,logincheck.jsp
    2017.11.28 10:35 11;40 1h55min 编写register.html,logincheck.jsp
    2017.11.28 12:50 13:25 35min 进行优化

    缺陷记录日志:

    日期 编号 描述 修复时间 修复过程
    2017.11.28 1 连续使用“||”倒置程序判断存在bug 25min 将“!=”换为“=”并添加if else语句
    2017.11.28 2 不能够精确判断上课地点 10min 使用substr函数进行
  • 相关阅读:
    LayoutInflater介绍及例子
    Java对象转换为Json对象
    重写Object.equals()方法和Object.hashCode()方法
    初步理解一下:SOA, SOAP, Web Service, WSDL等
    JSP九大内置对象及四个作用域
    Visual Studio的导入和导出设置
    winform三态树的构建和使用
    关于android编程使用android.hardware.Camera 方向不对的问题
    xampp软件包如何重置mysql的 密码
    有关.NET中的config文件的一些问题
  • 原文地址:https://www.cnblogs.com/lwq666/p/7911464.html
Copyright © 2020-2023  润新知