用户注册数据库登记

用户注册数据库登记

1、注册页.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>Insert title here</title>
</head>
<body>
    <form action = "regService.jsp" method = "post">
        用户名:<input type = "text" name = "username" value = ""/><br/>
        密码:<input type = "password" name = "password" value = ""/><br/>
        <input type = "submit" value = "注册"/>
    </form>
</body>
</html>

2、注册服务.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>Insert title here</title>
</head>
<body>
<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    boolean usernameCheck = (null != username && username.trim().length() > 6);
    boolean passwordCheck = (null != password && password.trim().length() > 6);

    if(usernameCheck && passwordCheck) {
        //往数据库插入数据
%>
        <jsp:forward page = "regSuccess.jsp">
            <jsp:param value = "<%=username %>" name = "username"/>
            <jsp:param value = "<%=password %>" name = "password"/>
        </jsp:forward>
<%
    } else {
%>
        <jsp:forward page = "regError.jsp"></jsp:forward>
<%      
    }
%>
</body>
</html>

3、注册成功.jsp

<%@page import="hellojsp.AccountGet"%>
<%@page import="hellojsp.Account"%>
<%@page import="java.util.List"%>
<%@page import="hellojsp.RegInsert"%>
<%@ 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>Insert title here</title>
    <style>
        table,table tr th, table tr td { border:1px solid #0094ff; }
        table { width: 200px; min-height: 25px; line-height: 25px; text-align: center; border-collapse: collapse;}   
    </style>
</head>
<body>
    <h1>恭喜您,注册成功!</h1>
    <%  String username = request.getParameter("username"); 
        String password = request.getParameter("password");
        RegInsert.insert(username, password);
    %>
    <h1>显示所有用户注册信息</h1>
    <table>
        <thead>
            <tr>
                <th>userName</th>
                <th>password</th>
            </tr>
        </thead>
        <tbody>     
        <%
        List<Account> accounts = AccountGet.getAccount();

        for(Account account : accounts) {
            out.println("<tr>");
            out.println("<td>" + account.getUsername() + "</td>" + "<td>" + account.getPassword() + "</td>");
            out.println("</tr>");
        }       
        %>
        </tbody>
    </table>

</body>
</html>

4、注册错误.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>Insert title here</title>
</head>
<body>
<h1>用户名或者密码为空或者长度不大于6,请<a href= "reg.jsp">点击重新注册</a></h1>
</body>
</html>

5、用户名和密码插入数据库的方法.java

package hellojsp;

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

public class RegInsert {
    public static void insert(String username, String pword) {
        Connection conn = null;
        PreparedStatement ps = null;
        String url = "jdbc:mysql://localhost:3306/jsp";
        String user = "root";
        String password = "";
        try {
            //1、选择数据库:加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2、连接数据库
            conn = DriverManager.getConnection(url, user, password);
            //3、创建数据库查询
            ps = conn.prepareStatement("INSERT INTO account_table(username,password) VALUES(?,?)");
            ps.setString(1, username);
            ps.setString(2, pword);
            //4、获取查询结果
            int i = ps.executeUpdate();
            System.out.println("一共执行了" + i + "条");
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有找到");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();        
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5、关闭查询和连接
            try {
                if (null != ps) {
                    ps.close();
                }
                if (null != conn) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

6、账号类.java

package hellojsp;

public class Account {
    private String username;
    private String password;

    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;
    }
}

7、查询数据库内所有用户名和密码的方法.java

package hellojsp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AccountGet {
    public static List<Account> getAccount(){
        List<Account> accounts = new ArrayList<Account>();
        Connection conn = null;
        PreparedStatement ps = null;
        String url = "jdbc:mysql://localhost:3306/jsp";
        String user = "root";
        String password = "";
        try {
            //1、选择数据库:加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2、连接数据库
            conn = DriverManager.getConnection(url, user, password);
            //3、创建数据库查询
            ps = conn.prepareStatement("SELECT username,password FROM account_table");  
            //4、获取查询结果
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                Account account = new Account();
                String username = rs.getString("username");
                String pword = rs.getString("password");
                account.setUsername(username);
                account.setPassword(pword);
                accounts.add(account);
            }
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有找到");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();        
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5、关闭查询和连接
            try {
                if (null != ps) {
                    ps.close();
                }
                if (null != conn) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return accounts;
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值