6.在线考试管理系统ADD

准备工作:
1.建表
create table users(
    userId int primary key auto_increment, #auto_increment,在插入时,如果不给定具体编号,则根据auto_increment值递加
    userName varchar(50),
    password varchar(50),
    sex char(1),
    email varchar(50),
)
2.建表的对应实体类
3.操作数据库工具类util
util:

 

package util;


import java.sql.*;
// JdbcUtil obj = new JdbcUtil();  obj.getCon()
// JdbcUtil obj = new JdbcUtil();  obj.createStatement();
// JdbcUtil.getCon();
public class JdbcUtil {

     final String URL="jdbc:mysql://localhost:3306/test2";
     final String USERNAME="root";
     final String PASSWORD="18081736467xr";
     PreparedStatement ps= null;
     Connection con = null;

    //将jar包中driver实现类加载到JVM中
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //封装连接通道创建细节
    public  Connection   getCon(){
        try {
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    //封装交通工具创建细节
    public  PreparedStatement createStatement(String sql){

        try {
            ps =  getCon().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }
    // ps与con销毁细节 insert,update,delete
    public  void close(){
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

     //select ps,con,rs
    public  void close(ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close();
    }
}
4.导入jdbc实现jar包,并右键jar包 add as Library

  5.画流程图

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
        <!--action="/myWeb/user/add" submit提交之后会重定向到/myWeb/user/add-->
    <form action="/myWeb/user/add" method="get">
        <table border="2">
            <tr>
                <td>用户姓名:</td>
                <td><input type="text" name="userName"></td>
            </tr>

            <tr>
                <td>用户密码:</td>
                <td><input type="password" name="userPassword"></td>
            </tr>

            <tr>
                <td>用户性别:</td>
                <td>
                    <input type="radio" name="sex" value='男'>男
                    <input type="radio" name="sex" value='女'>女
                </td>
            </tr>

            <tr>
                <td>用户邮箱:</td>
                <td><input type="text" name="email"></td>
            </tr>
            <tr>
                <td><input type="submit" value="注册"></td>
                <td><input type="reset" value="重置"></td>
            </tr>
        </table>
    </form>
</body>
</html>

 servlet:

public class UserAddServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        int result =0;
        UserDao userDao = new UserDao();
        //1.调用请求对象读取请求头参数信息,得到用户的信息
        String userName = req.getParameter("userName");
        String password = req.getParameter("userPassword");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        users users = new users(userName,password,sex,email);
        //2.调用UserDAO将用户信息填充到数据库
        try {
            result = userDao.add(users);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //3.调用响应对象 将处理结果以二进制形式写入到响应体
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter ps = resp.getWriter();
        if(result==1)
        {
            ps.print("<font style='color:red; font-size:40'>用户信息注册成功</font>");
        }else
        {
            ps.print("<font style='color:red; font-size:40'>用户信息注册失败</font>");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {

    }
}

 UserDao:

package Dao;

import entity.users;
import util.JdbcUtil;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserDao
{
    JdbcUtil jdbcUtil = new JdbcUtil();
    //用户的注册
    public int add(users users) throws SQLException {
        String addSql = "insert into users(userName,password,sex,email) values(?,?,?,?)";
        PreparedStatement ps = jdbcUtil.createStatement(addSql);
        int result = 0;
        try{
            ps.setString(1,users.getUserName());
            ps.setString(2,users.getPassword());
            ps.setString(3,users.getSex());
            ps.setString(4,users.getEmail());
            result = ps.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        } finally
        {
            jdbcUtil.close();
        }
        return result;
    }
}

util工具类:

package util;


import java.sql.*;
// JdbcUtil obj = new JdbcUtil();  obj.getCon()
// JdbcUtil obj = new JdbcUtil();  obj.createStatement();
// JdbcUtil.getCon();
public class JdbcUtil{
     final String URL="jdbc:mysql://localhost:3306/test2?characterEncoding=UTF-8";
     final String USERNAME="root";
     final String PASSWORD="18081736467xr";
     PreparedStatement ps= null;
     Connection con = null;

    //将jar包中driver实现类加载到JVM中
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //封装连接通道创建细节
    public  Connection  getCon(){
        try {
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    //封装交通工具创建细节
    public  PreparedStatement createStatement(String sql){
        try {
            ps =  getCon().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }
    // ps与con销毁细节 insert,update,delete
    public  void close(){
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

     //select ps,con,rs
    public  void close(ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close();
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值