登录注册功能的简单实现

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了登录注册功能的简单实现脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

由于我在上学期的Java中挂了所有这周老师让我去给学弟们讲讲数据库连接和登入注册,顺便在写一篇博客。

先从建立数据库开始,我用native cat建立一个数据库

登录注册功能的简单实现

 然后就是连接数据库 具体的数据库名和表名根据自己的更改

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

public class DBUtil { private static String jdbcName = "com.mysql.cj.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/user?serverTimezone=GMT&useSSL=false"; private static String user = "root"; private static String password = "Akko"; private Connection con=null; public static Connection getConnection() { Connection con=null; try { Class.forName(jdbcName); con=DriverManager.getConnection(url, user, password); //System.out.println("数据库连接成功"); } catch (Exception e) { // TODO Auto-generated catch block //System.out.println("数据库连接失败"); e.printStackTrace(); } try { con = DriverManager.getConnection(url,user,password); System.out.println("连接成功");

} catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } return con; } public static void main(String[] args)throws SQLException { Connection conn = getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from user"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); System.out.println(getConnection()); while(rs.next()){ System.out.println("成功"); }

}

// return con; public static void close(Connection con) { if(con!=null) try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 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(); } } }

}

建立bean类

public class Bean { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } 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 Bean(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } public String toString() { return "User{" + "id=" + id + ", username='" + username + ''' + ", password='" + password + ''' + '}'; } }

然后是建立一个到类,里面存放数据库的登陆注册方法

//注册public boolean insert(Bean bean) {//插入数据的方法 boolean f=false; String sql="insert into user(id,username,password) values('"+bean.getId()+"','"+bean.getUsername()+"','"+bean.getPassword()+"')"; Connection conn=DBUtil.getConnection();//数据库连接,加载驱动 Statement state=null; try { state=conn.createStatement();//实例化Statement对象,方便对sql语句进行操作 System.out.println(conn); state.executeUpdate(sql); f=true; //执行数据库更新操作用于执行INSERT、UPDATE或DELETE语句以及SQLDDL(数据定义语言)语句, //例如CREATETABLE和DROPTABLE,(创建表和删除表) }catch(Exception e)//当try语句中s出现异常时,会执行catch中的语句 { e.printStackTrace();//捕获异常的语句 } finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。 { DBUtil.close(conn); } return f; }//用户登入public int check(String username,String password) throws SQLException { DBUtil db=new DBUtil(); String sql="select * from user where username ="+username+" and password ="+password; Connection con=db.getConnection(); PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); int s=0; if(rs.next()) { s=1; } return s; }

然后写登入注册页面 

login

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>用户登入</title></head><body><div align="center"> <h1>用户登入</h1> <form action="LoginServlet" method="post"> <table id="addTable" class="table table-bordered "> <tr class="text-center row"> <tr> <tr class="text-center row"> <td class="col-sm-2"> 用户名 </td> <td class="col-sm-4"> <input type="text" class="form-control" name="username" id="username" > </td></tr> <tr> <td class="col-sm-2"> 密码 </td> <td class="col-sm-4"> <input type="password" class="form-control" name="password" id="password" > </td> </tr> <tr align="center" style="height: 20%"> <td style="padding-left: 10px"><input type="submit" name="sub_name" value="用户登录"></td> <td style="padding-right: 40px;padding-left: 20px"><input type="submit" name="sub_name" value="管理员登录" ></td> </tr> </table> <input type="button" value="注册"onclick="javascrtpt:window.location.href='register.jsp'" /> </form> </div></body></html>

register

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><title>注册</title></head><body><% Object message = request.getAttribute("message"); if (message != null && !"".equals(message)) { %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); //弹出对话框 </script> <% } %> <div align="center"> <h1>增加</h1> <a href="login.jsp">返回登入页</a> <form action="servlet?method=insert" method="post"> <table id="addTable" class="table table-bordered "> <tr class="text-center row"> <tr> <td class="col-sm-2"> id </td> <td class="col-sm-4"> <input type="text" class="form-control" name="id" id="id" > </td> <tr class="text-center row"> <td class="col-sm-2"> 用户名 </td> <td class="col-sm-4"> <input type="text" class="form-control" name="username" id="username" > </td></tr> <tr> <td class="col-sm-2"> 性别 </td> <td class="col-sm-4"> <input type="text" class="form-control" name="password" id="password" > </td> </tr>

</table> <input type="submit" value="注册" onclick= "return check()" /> </form> </div>

</body></html>

最后是写servlet,通过它来连接前端和后端

注册

private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); int id = Integer.parseInt(request.getParameter("id")); String username = request.getParameter("username"); String password = request.getParameter("password"); Bean bean=new Bean(id,username,password); if(dao.insert(bean)) { request.setAttribute("message", "注册成功"); request.getRequestDispatcher("register.jsp").forward(request, response); } }

登入

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String sub_name = request.getParameter("sub_name"); HttpSession a = request.getSession(); Dao dao = new Dao(); int s=0; if (sub_name.equals("用户登录")) { try { s=dao.check(username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(s==0) { response.setHeader("refresh","0.1;url=failLogin.jsp"); } else { a.setAttribute("account",username); request.getRequestDispatcher("usermain.jsp").forward(request,response); } } if(s==0) { response.setHeader("refresh","0.1;url=failLogin.jsp"); } else { a.setAttribute("account",username); request.getRequestDispatcher("adminmain.jsp").forward(request,response); } } }

脚本宝典总结

以上是脚本宝典为你收集整理的登录注册功能的简单实现全部内容,希望文章能够帮你解决登录注册功能的简单实现所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:数据库