• java web用户登录界面


    做这次实验,主要用到了mysql  java web 的 内容

    实验代码:

    IUserDao.java

    package com.jaovo.msg.dao;

    import java.util.List;

    import com.jaovo.msg.model.User;

    public interface IUserDao {
    public void add(User user);
    public void delete(int id);
    public void update(User user);
    public User load(int id);
    public User load(String username);
    public List<User> load();

    }

     UserDaoImpl.java

    package com.jaovo.msg.dao;

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


    import com.jaovo.msg.Util.DBUtil;
    import com.jaovo.msg.Util.UserException;
    import com.jaovo.msg.model.User;

    import sun.net.www.content.text.plain;

    public class UserDaoImpl implements IUserDao{

    @Override
    public void add(User user) {
    //获得链接对象
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "select * from example where username = ?";
    //创建语句传输对象
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
    preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setString(1, user.getUsername());
    //接收结果集
    resultSet = preparedStatement.executeQuery();
    //遍历结果集
    while(resultSet.next()) {
    if (resultSet.getInt(1) > 0) {
    throw new UserException("用户已存在") ;
    }
    }

    sql = "insert into example(username,password) value (?,?)";
    preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setString(1, user.getUsername());
    preparedStatement.setString(2, user.getPassword());
    preparedStatement.executeUpdate();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally {
    //关闭资源
    DBUtil.close(resultSet);
    DBUtil.close(preparedStatement);
    DBUtil.close(connection);
    }

    }

    @Override
    public void delete(int id){
    Connection connection = DBUtil.getConnection();
    String sql = "delete from example where id = ?";
    PreparedStatement preparedStatement = null;

    try {
    preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setInt(1, id);
    preparedStatement.executeUpdate();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally {
    DBUtil.close(preparedStatement);
    DBUtil.close(connection);
    }

    }

    @Override
    public void update(User user) {
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "update example set password = ? , nickname=? where id = ?";
    //创建语句传输对象
    PreparedStatement preparedStatement = null;
    try {
    preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setString(1, user.getPassword());
    preparedStatement.setInt(3, user.getId());
    preparedStatement.executeUpdate();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally {
    DBUtil.close(preparedStatement);
    DBUtil.close(connection);
    }
    }

    public User load1(int id) {
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "select * from example where id = ?";
    //创建语句传输对象
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    User user = null;
    try {
    preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setInt(1, id);
    resultSet = preparedStatement.executeQuery();
    while(resultSet.next()) {
    user = new User();
    user.setId(id);
    user.setUsername(resultSet.getString("username"));
    user.setPassword(resultSet.getString("password"));
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally {
    DBUtil.close(resultSet);
    DBUtil.close(preparedStatement);
    DBUtil.close(connection);
    }
    return user;
    }

    public User load1(String username){
    // TODO Auto-generated method stub
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "select * from example where username = ?";
    //创建语句传输对象
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    User user = null;
    try {
    preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setString(1, username);
    resultSet = preparedStatement.executeQuery();
    while(resultSet.next()) {
    user = new User();
    user.setUsername(username);
    user.setId(resultSet.getInt("username"));
    user.setPassword(resultSet.getString("password"));
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally {
    DBUtil.close(resultSet);
    DBUtil.close(preparedStatement);
    DBUtil.close(connection);
    }
    return user;

    }

    public List<User> load(){
    Connection connection = DBUtil.getConnection();
    //准备sql语句
    String sql = "select * from example ";
    //创建语句传输对象
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    //集合中只能放入user对象
    List<User> users = new ArrayList<User>();
    User user = null;
    try {
    preparedStatement = connection.prepareStatement(sql);
    resultSet = preparedStatement.executeQuery();
    while(resultSet.next()) {
    user = new User();
    user.setId(resultSet.getInt("id"));
    user.setUsername(resultSet.getString("username"));
    user.setPassword(resultSet.getString("password"));
    users.add(user);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally {
    DBUtil.close(resultSet);
    DBUtil.close(preparedStatement);
    DBUtil.close(connection);
    }
    return users;
    }

    @Override
    public void add(User user) {
    // TODO Auto-generated method stub

    }

    @Override
    public void update(User user) {
    // TODO Auto-generated method stub

    }

    @Override
    public User load(int id) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public User load(String username) {
    // TODO Auto-generated method stub
    return null;
    }

    }

    User.java

    package com.jaovo.msg.model;

    public class User {

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

    }

    DBUtil.java

    package com.jaovo.msg.Util;

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

    public class DBUtil {

    public static Connection getConnection() {
    try {
    //1 加载驱动
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    String user = "root";
    String password = "582607kou";
    String url = "jdbc:mysql://localhost:33060/mysql";
    Connection connection = null;
    try {
    //2 创建链接对象connection
    connection = DriverManager.getConnection(url,user,password);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return connection;
    }

    //关闭资源的方法
    public static void close(Connection connection ) {
    try {
    if (connection != null) {
    connection.close();
    }

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public static void close(PreparedStatement preparedStatement ) {
    try {
    if (preparedStatement != null) {
    preparedStatement.close();
    }

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public static void close(ResultSet resultSet ) {
    try {
    if (resultSet != null) {
    resultSet.close();
    }

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

    UserException.java

    package com.jaovo.msg.Util;

    public class UserException extends RuntimeException{

    public UserException() {
    super();
    // TODO Auto-generated constructor stub
    }

    public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
    super(message, cause, enableSuppression, writableStackTrace);
    // TODO Auto-generated constructor stub
    }

    public UserException(String message, Throwable cause) {
    super(message, cause);
    // TODO Auto-generated constructor stub
    }

    public UserException(String message) {
    super(message);
    // TODO Auto-generated constructor stub
    }

    public UserException(Throwable cause) {
    super(cause);
    // TODO Auto-generated constructor stub
    }

    }

    indexcheck.java

    <%@ page import="com.jaovo.msg.Util.DBUtil" language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ page import="com.jaovo.msg.model.User"%>>
    <%@ page import="com.jaovo.msg.dao.UserDaoImpl"%>>
    <!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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    request.setCharacterEncoding("UTF-8");
    String username=request.getParameter("username");
    String password=request.getParameter("password");//取出login.jsp的值
    UserDaoImpl a=new UserDaoImpl();
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    User user=a.load(username);
    String password1=user.getPassword();
    if(password.equals(password1)){
    response.sendRedirect("loginsuccess.jsp");
    }
    else{
    out.print("<script language='javaScript'> alert('密码错误');</script>");
    response.setHeader("refresh", "0;url=login.jsp");
    }
    %>
    </body>
    </html>

    login.java


    <%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>登录界面</title>
    </head>
    <body>
    <center>
    <h1 style="color:black">登录</h1>
    <form id="indexform" name="indexForm" action="indexcheck.jsp" method="post">
    <table border="0">
    <tr>
    <td>账号:</td>
    <td><input type="text" name="username"></td>
    </tr>
    <tr>
    <td>密码:</td>
    <td><input type="password" name="password">
    </td>
    </tr>
    </table>
    <br>
    <input type="submit" value="登录" style="color:#BC8F8F">
    </form>
    <form action="zhuce.jsp">
    <input type="submit" value="注册" style="color:#BC8F8F">
    </form>
    </center>
    </body>
    </html>

    loginsuccess.java

    <%@ page import="java.sql.*" 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>登陆成功</h1>
    </body>
    </html>

    实验结果截图:

    对自己空闲时间计划的太少,平时不能利用空闲时间来学习java web的内容,以后打算每天用4个小时的时间来学习,编程,周六日时间再加强!

  • 相关阅读:
    Elasticsearch常用插件集合(转)
    istio 安装与bookinfo示例运行(转)
    kubernetes发布tomcat服务,通过deployment,service布署(转)
    记一次ceph集群的严重故障 (转)
    Linux centos 7 安装NFS服务
    Centos7下使用Ceph-deploy快速部署Ceph分布式存储-操作记录(转)
    docker 安装 jmeter
    idea 2018.1破解激活方法,有效期至2099年
    MySQL和Oracle的区别
    SpringCloud面试题
  • 原文地址:https://www.cnblogs.com/ggrm/p/7884408.html
Copyright © 2020-2023  润新知