2019年10月21日课堂测试

一、题目

石家庄铁道大学2019年秋季

  2018 级课堂测试试卷(六)(10分)

课程名称: JAVA语言程序设计  任课教师王建民        考试时间: 150 分钟 

一、   考试要求:

 

1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)

2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)

3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)

4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)

5姓名:输入自己的姓名;

5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(1分)

6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)

7可以演示连接上数据库。(2分)

二、源代码

 

package Dao;

import java.sql.Connection;
import java.sql.Statement;

import DBUtil.DBUtil;

import Entity.User;

public class Dao {

    public boolean add(User user) {
        // TODO Auto-generated method stub
        String sql = "insert into user1(username,password,sex,name,num,email,xueyuan,xi,banji,ruxue,where,beizhu) values('"+ user.getUsername() + "','"+ user.getPassword() +"','"+user.getSex() +"','"+  user.getName() +"','" + user.getNum() +"','"+ user.getEmail()+ "','"+ user.getXueyuan() + "','"+ user.getXi() + "','"+ user.getBanji() + "','"+ user.getRuxue() + "','"+ user.getWhere() + "','"+ user.getBeizhu()+"')";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;

        try {
            state = conn.createStatement();
            a=state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            
            DBUtil.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;

}
}
DAO
package DBUtil;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * ���ݿ����ӹ���
 * @author Hu
 *
 */
public class DBUtil {
    
    public static String db_url = "jdbc:mysql://localhost:3306/user";
    public static String db_user = "root";
    public static String db_pass = "hao6116119";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.jdbc.Driver");//��������
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }
    
    public static void close (Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close (ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql ="select * from users";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if(rs.next()){
            System.out.println("空");
        }else{
            System.out.println("不空");
        }
    }
}
DBUtil
package Entity;

public class User {
    
    private String username;
    private String password;
    private String name;
    private String sex;
    private String num;
    private String email;
    private String xueyuan;
    private String xi;
    private String banji;
    private String ruxue;
    private String where;
    private String beizhu;
    
    

    public User() {}
    
public User(String username,String password,String sex,String name,String num,String email,String xueyuan,String xi,String banji,String ruxue,String where,String beizhu) {
        
        
        this.username=username;
        this.password=password;
        this.sex=sex;
        this.name=name;
        this.num=num;
        this.email=email;
        this.xueyuan=xueyuan;
        this.xi=xi;
        this.banji=banji;
        this.ruxue=ruxue;
        this.where=where;
        this.beizhu=beizhu;
        
    }
public String getXueyuan() {
    return xueyuan;
}

public void setXueyuan(String xueyuan) {
    this.xueyuan = xueyuan;
}

public String getXi() {
    return xi;
}

public void setXi(String xi) {
    this.xi = xi;
}

public String getBanji() {
    return banji;
}

public void setBanji(String banji) {
    this.banji = banji;
}

public String getRuxue() {
    return ruxue;
}

public void setRuxue(String ruxue) {
    this.ruxue = ruxue;
}

public String getWhere() {
    return where;
}

public void setWhere(String where) {
    this.where = where;
}

public String getBeizhu() {
    return beizhu;
}

public void setBeizhu(String beizhu) {
    this.beizhu = beizhu;
}
public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public String getNum() {
    return num;
}

public void setNum(String num) {
    this.num = num;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}
    

}
Entity
package Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.Dao;
import DBUtil.DBUtil;
import Entity.User;




@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    
    public Servlet() {
        super();
        
    }
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("add".equals(method)) {
            add(req, resp);
        } 
    }
    
    
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String name = req.getParameter("name");
        String num = req.getParameter("num");
        String email = req.getParameter("email");
        String xueyuan = req.getParameter("xueyuan");
        String xi = req.getParameter("xi");
        String banji = req.getParameter("banji");
        String ruxue = req.getParameter("ruxue");
        String where = req.getParameter("where");
        String beizhu = req.getParameter("beizhu");
        User user = new User(username,password,sex,name,num,email,xueyuan,xi,banji,ruxue,where,beizhu);
        
        Dao dao =new Dao();
        boolean f=dao.add(user);
        
        
        if(f) {
            req.setAttribute("message", "注册成功!");
            req.getRequestDispatcher("user.jsp").forward(req,resp);
        } else {
            req.setAttribute("message", "注册失败!");
            req.getRequestDispatcher("user.jsp").forward(req,resp);
        }
    }
}
Servlet
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
    
    <body>
    <%
        Object message = request.getAttribute("message");
        if (message != null && !"".equals(message)) {
    %>
    <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
    </script>
    <%
        }
    %>
    
    
        <table border="1px" cellpadding="10px" cellspacing="0px"
                style="width: 30%;margin:auto;background:rgb(183,133,195)"  bordercolor="red" >
                    <form action="Servlet?method=add" method="post"
            onsubmit="return check()">
            <caption>注册用户</caption>
          
             
                <tr>
                    <th>用户名:</th>
                    <td><input type="text" name="username"></th>
                </tr>
                <tr>
                    <th>密码:</th>
                    <td><input type="password" name="password"></td>
                </tr>
                <th>性别:</th>
                    <td>
                        <select name="sex">
                            <option value="男">男</option>
                            <option value="女">女</option>
                        </select>
                <tr>        
                    </td>
                    <th>姓名:</th>
                    <td><input type="text" name="name"></td>
                </tr>
                 <tr>        
                    </td>
                    <th>学号:</th>
                    <td><input type="text" name="num"></td>
                </tr>
                 <tr>
                    <th>电子邮件:</th>
                    <td><input type="text" name="email"></td></tr>
                 <tr>
                    <th>所在学院:</th>
                    <td><input type="text" name="xueyuan"></td>
                </tr>
                 <tr>
                    <th>所在系:</th>
                    <td><input type="text" name="xi"></td>
                </tr>
                 <tr>
                    <th>所在班级:</th>
                    <td><input type="text" name="banji"></td>
                </tr>
                <th>入学年份(届):</th>
                    <td>
                        <select name="ruxue">
                            <option value="1998">1998</option>
                            <option value="1999">1999</option>
                            <option value="2001">2001</option>
                            <option value="2002">2002</option>
                            <option value="2003">2003</option>
                            <option value="2004">2004</option>
                            <option value="2005">2005</option>
                            <option value="2006">2006</option>
                            <option value="2007">2007</option>
                            <option value="2008">2008</option>
                            <option value="2009">2009</option>
                        </select><tr>      
                 <tr>
                    <th>生源地:</th>
                    <td><input type="text" name="where"></td>
                </tr> 
                 <tr>
                    <th>备注:</th>
                    <td><input type="text" name="beizhu"></td>
                </tr> 
                <tr>
                    <th colspan="4">
                        <input type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp;
                        <%--<input type="reset" value="重置"> --%>
                    </th>
                </tr>
            </form>
        </table>
    </body>
</html>
JSP

三、问题

1.今天编程中遇到的主要问题是数据库连不上,从而阻断了我之后的编程,询问过学长之后发现是环境出了问题,直到现在还没有解决,电脑里面的环境跟别人好像不太一样,今天暂时就到这里,等到日后定会补上完好的代码!

posted @ 2019-10-21 23:27  酸奶面包  阅读(136)  评论(0编辑  收藏  举报