JSP连接数据库实现注册登录

1.使用单例来连接数据库     //数据库的连接使用单例来连接一次即可,执行完sql语句后如果没有结束就不能关流 不然后边就不能再去执行sql语句

public class JDBCUtils1 {
    // 连接数据库的参数
    private String driver= "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/xinzhi666?useUnicode=true&characterEncoding=utf-8";
    private String username = "root";
    private String password = "root";

    private static Connection conn;

    private JDBCUtils1(){
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static Connection getConnection(){
        if(conn!=null){
            new JDBCUtils1();
        }
        return conn;
    }

    public void relase(ResultSet resultSet, Statement statement,Connection connection){
        try {
            if(resultSet!=null){
                resultSet.close();
            }
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
 

2.写注册的接口

public interface IUserInfoDao {
    /**
     *  注册  数据库中添加用户
     * @param ui  用户实体类
     * @return
     */
    int addUser(UserInfo ui);
}

3.写注册的实现类

public class UserInfoDaoImpl implements IUserInfoDao {

    @Override
    public int addUser(UserInfo ui) {
        int count = -1;
        PreparedStatement pstat = null;
        try {
            String sql = "insert into user (username,password,age,phone) values (?,?,?,?)";
            pstat = JDBCUtils.getConnection().prepareStatement(sql);
            pstat.setString(1, ui.getUsername());
            pstat.setString(2, ui.getPassword());
            pstat.setInt(3, ui.getAge());
            pstat.setString(4, ui.getPhone());
            
            count = pstat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            JDBCUtils.relase(null, pstat, null);
        }
        return count;
}

2.注册的界面

<form action="regService.jsp" method="post">
    用户名:<input type="text" name="username" /><br>
    密码:<input type="text" name="password" /><br>
    年龄:<input type="text" name="age" /><br>
    手机号:<input type="text" name="phone" /><br>
    <input  type="submit" value="注册">
</form>

3.注册的后台逻辑

    <%
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String age = request.getParameter("age");
        String phone = request.getParameter("phone");

        UserInfo ui = new UserInfo();
        ui.setAge(Integer.parseInt(age));
        ui.setUsername(username);
        ui.setPassword(password);
        ui.setPhone(phone);
        
        UserInfoDaoImpl uidi = new UserInfoDaoImpl();
        int count = uidi.addUser(ui);
        if(count > 0){
            response.sendRedirect("login.jsp");
        }else{
            request.setAttribute("info", "注册失败");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
    %>

登录功能

1.写登录的接口

/**
 *
 * @param username   用户名
 * @param password    密码
 * @return   查询的数据
 */
User queryByUsernameAndPassword(String username,String password); 

根据输入的账号密码来进入数据库查询

2.写登录的实现类

@Override
public User queryByUsernameAndPassword(String username, String password) {
    // 通过工具类获取连接对象 connection
    Connection connection = BaseDao.getConnection();
    // 插入的sql语句
    String sql = "select id,username,password,age from user where username=? and password=?";
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    User user = null;

    try {
        //获取statement对象
        statement = connection.prepareStatement(sql);
        statement.setString(1,username);
        statement.setString(2,password);
        resultSet = statement.executeQuery();
        if (resultSet.next()){
            int id = resultSet.getInt(1);
            String uname = resultSet.getString(2);
            String pw = resultSet.getString(3);
            int age = resultSet.getInt(4);
            user = new User(id, uname, pw, age);
        }
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
    return user;
}

3.写登录的界面

    <form action="loginService.jsp" method="post">
        用户名:<input type="text" name="username" />
        密码:<input type="text" name="password" />
        <input  type="submit" value="登陆">
    </form>

4.写登录的后台逻辑

<%
        //设置页面请求编码格式
        request.setCharacterEncoding("UTF-8");
        //获取页面传过来的用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        // 登录逻辑
        UserInfoDaoImpl uidi = new UserInfoDaoImpl();
        User user = uidi.login(username, password);
        
        // 获取年龄
        ShowInfoDaoImpl showInfoDao = new ShowInfoDaoImpl();
        Set<Integer> ages = showInfoDao.getAge();
    
        if(user != null){
            // 登陆成功
            ShowInfoDaoImpl sidi = new ShowInfoDaoImpl();
            List<User> uis = sidi.getUserList(-1);
            // 域对象中设置属性(request域对象的特点)  
            request.setAttribute("uis", uis);
            request.setAttribute("ages",ages );
            request.getRequestDispatcher("show.jsp").forward(request, response);
        }else{
            // 登陆失败
            request.setAttribute("info", "登陆失败");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    %>
 

  • 3
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙龙爱喝奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值