用戶註冊數據庫登記

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/liuxiaowei_java96/article/details/78532649

用戶註冊數據庫登記

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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章