使用JDBC实现用户注册

1、在MySQL数据库中先新建一个数据库

表结构如下:
在这里插入图片描述

2、在IDEA中连接数据库

在连接数据库之前,先将mysql-connector-java-8.0.19.jar导入
在这里插入图片描述
但是有时候这种导入方式不成功,会报错(java.lang.ClassNotFoundException: com. mysql.cj.jdbc.Driver),这时直接将jar包放进tomcat的离别文件夹中即可
在这里插入图片描述

3、创建数据库处理工具类DbPreparedUtil.java

package com.example.jdbc_res;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DbPreparedUtil {
    private Connection connection;
    private PreparedStatement statement;

    /**
     * 初始化和获取connection
     * @return
     * @throws Exception
     */
    public Connection getConnection() throws Exception{
        if (this.connection==null){//赋值
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("成功执行第一步");
            //第二步,获取连接
            this.connection=
                    DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC",
                            "root",
                            "123456");
            System.out.println("成功执行第二步");
        }
        return this.connection;
    }

    /**
     * 初始化和获取statement
     * @return
     * @throws Exception
     */
    public PreparedStatement getStatement(String sql)
            throws Exception{

        this.closeStatement();
        if(this.statement==null){
            this.statement=
                    this.getConnection().prepareStatement(sql);
        }
        return this.statement;
    }

    /**
     * 执行查询的方法
     * @param sql
     * @return
     * @throws Exception
     */
    public List executeQuery(String sql, Object...parameters) throws Exception{
        PreparedStatement statement=this.getStatement(sql);
        int index=1;
        for(Object param:parameters){
            statement.setObject(index,param);
            index++;
        }
        ResultSet rs=statement.executeQuery();

        List res=this.getResult(rs);
        rs.close();
        this.closeStatement();
        System.out.println("成功执行第三步");
        return res;
    }

    /**
     * 执行更新,插入,删除的方法
     * @param sql
     * @return
     * @throws Exception
     */
    public int executeUpdate(String sql,Object...parameters) throws Exception{

        PreparedStatement statement=this.getStatement(sql);
        int index=1;
        for(Object param:parameters){
            statement.setObject(index,param);
            index++;
        }
        System.out.println("成功执行第四步");
        return statement.executeUpdate();
    }

    /**
     * 关闭statement
     */
    public void closeStatement() throws Exception{
        if(this.statement!=null){
            this.statement.close();
            this.statement=null;
        }
    }

    /**
     * 关闭连接
     */
    public void close() throws Exception{
        try {
            this.closeStatement();
        }catch (Exception e){
            e.printStackTrace();
        }

        if(this.connection!=null){
            this.connection.close();
        }
        System.out.println("成功执行第五步");
    }

    /**
     * resultset转化为list
     * @param rs
     * @return
     */
    public List getResult(ResultSet rs)throws Exception{
        ResultSetMetaData metaData=rs.getMetaData();
        List trs=new ArrayList();
        while (rs.next()){
            Map temp=new HashMap();
            for(int i=1;i<=metaData.getColumnCount();i++){
                String key=metaData.getColumnName(i);
                Object val=rs.getObject(i);
                temp.put(key,val);
            }
            trs.add(temp);
        }
        return trs;
    }
}

4、创建注册JSP页面register.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>register</title>
</head>
<body>
<form action="result.jsp" method="post">
    用户名:<input type="text" name="username"/><br>&emsp;码:<input type="password" name="password"/><br>&emsp;别:
    <input type="radio"  name="sex" value="男" checked ="checked" ><input type="radio"  name="sex" value="女"><br>&emsp;明:<input type="text" name="note"/><br>
    <input type="submit" name="submit"/>
</form>
</body>
</html>

5、利用数据工具类实现注册信息入库

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>result</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");

String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String note = request.getParameter("note");
System.out.println(username);
System.out.println(password);
System.out.println(sex);
System.out.println(note);

String sex_new;
    if(sex.equals("男")){
        sex_new = "1";
    }else{
        sex_new = "0";
    }

    DbPreparedUtil dbPreparedUtil = new DbPreparedUtil();
    String sql = "insert into userinfo values(?,?,?,?)";

    int result = dbPreparedUtil.executeUpdate(sql , new Object[]{username,password,sex_new,note});
    if(result>0){
%>
注册成功
<%}else{%>
注册不成功
<%}%>

</body>
</html>

6、效果图

在这里插入图片描述

  • 7
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值