java与mysql数据库---注册登录功能(增删改查4功能)初级入门教学

需要的软件:IntelliJ IDEA或其他(版本越新越好)Navicat或sqlyog(版本随意)

前提需要:需要你已经把java和mysql数据库成功连接

如果你没学会怎么连接:(先收藏,点下面链接你连接好数据库再来)

eclipse 连接sql:http://t.csdn.cn/lrIgc
IntelliJ IDEA连接sql :http://t.csdn.cn/1Rv2N

以下内容就是java+mysql的注册登录功能(注册、登录、修改密码、注销)

1.数据库设计:(使用Navicat或sqlyog来做快捷方便,或者你用传统代码来创建也行)

在这里插入图片描述
数据库名:jdbc,表名:teacherinfo
字段(id:主键,不为空,varchar,账号),password(不为空,varchar,密码)
在这里插入图片描述
插入几条记录备用

java代码:

package cn.student.test;

import java.sql.*;
import java.util.Scanner;

        /*
        注册登录模块
        1.注册
            1.1新增用户数据到数据库
            1.2注册新用户时需要检测数据库中是否存在同名用户
        2.登录
            2.1在数据库中检验账号和密码是否准确
        3.修改密码
            3.1根据用户名和密码进行匹配,更新密码
        4.注销
            4.1根据用户信息在数据库中删除记录

        数据库设计
        1.字段(id:主键,不为空,varchar,账号),password(不为空,varchar,密码)
         */

public class test1 {
    //主函数
    public static void main(String[] args) throws Exception{

        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("连接数据库成功!");

        String url = "jdbc:mysql://localhost:3306/jdbc"; // 提示:(jdbc:mysql://localhost:3306/数据库名字)
        String user = "root";                           //提示:sql用户名
        String dbpassword = "123456";                   //提示:sql密码

        Connection conn= DriverManager.getConnection(url,user,dbpassword);
        System.out.println("成功加载数据库"+conn);
        System.out.println("---------------------------------------------");
        System.out.println("********       欢迎进入教师管理系统       ********");
        System.out.println("注册请输入1,登录请输入2,修改密码请输入3,注销请输入4");
        System.out.println("请输入功能序号:");
        Scanner sc=new Scanner(System.in);
        int flag=sc.nextInt();
        System.out.println("----------------------------------------------");
        if (flag==1){
            //注册
            System.out.println("请输入账号:");
            String id = sc.next();
            System.out.println("请输入密码:");
            String password = sc.next();
            signup(conn,id,password);
        }
        else if (flag==2){
            //登录
            System.out.println("请输入账号:");
            String id = sc.next();
            System.out.println("请输入密码:");
            String password = sc.next();
            login(conn,id,password);
        }
        else if (flag==3){
            //修改密码
            System.out.println("请输入账号:");
            String id = sc.next();
            System.out.println("请输入旧密码:");
            String oldpassword = sc.next();
            System.out.println("请输入新密码:");
            String newpassword = sc.next();
            changePassword(conn,id,oldpassword,newpassword);
        }
        else if (flag==4){
            //注销
            System.out.println("请输入账号:");
            String id = sc.next();
            System.out.println("请输入密码:");
            String password = sc.next();
            deleteUser(conn,id,password);
        }
        else {
            System.out.println("输入有误,请重新输入");
        }
    }


    //注册模块(增加)
    public static void signup(Connection conn,String id,String password)throws Exception{

        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("加载成功");
        Statement statement = conn.createStatement();
        String sql1 = "select id from teacherinfo where id=?";
        //select 列名 from 表名 where 列名=?
        PreparedStatement PS = conn.prepareStatement(sql1);
        PS.setString(1,id);//给sql语句的第1个问号赋值
        ResultSet res = PS.executeQuery();
        if (res.next()){
            System.out.println("该账号已存在,请重新输入!");
        }else{
            String sql2="insert into teacherinfo (id,password) values(?,?);";
            //insert into 表名 (列名1,列名2) values(?,?);
            PS=conn.prepareStatement(sql2);
            PS.setString(1,id);//给第1个问号赋值
            PS.setString(2,password);//给第2个问号赋值
            PS.executeUpdate();
            System.out.println("注册成功");
        }
        //关闭所有接口
        res.close();
        PS.close();
        statement.close();
        conn.close();
    }

    //登录模块(查询)
    public static void login(Connection conn,String id,String password) throws Exception{
        String sql1 = " select id from teacherinfo where id=? ";
        PreparedStatement PS = conn.prepareStatement(sql1);
        PS.setString(1,id);
        ResultSet res = PS.executeQuery();
        if(res.next()){
            String sql2 = "select * from teacherinfo where id= ? and password= ?";
            PreparedStatement PS1 = conn.prepareStatement(sql2);
            PS1.setString(1,id);
            PS1.setString(2,password);
            ResultSet res1 = PS1.executeQuery();
            if (res1.next()){
                System.out.println("登录成功!");
            } else {
                System.out.println();
                System.out.println(res1.next());
                System.out.println("密码错误");
            }
        }else {
            System.out.println("此账号还未注册,请先注册再登录!");
        }
        conn.close();
        PS.close();
        res.close();
    }


    //修改密码(修改)
    public static void changePassword(Connection conn,String id,String oldpassword,String newpassword) throws Exception{
        String sql1 = " select id from teacherinfo where id=? ";
        PreparedStatement PS = conn.prepareStatement(sql1);
        PS.setString(1,id);
        ResultSet res = PS.executeQuery();
        if(res.next()){
            String sql2 = " select * from teacherinfo where id= ? and password= ?";
            PreparedStatement PS1 = conn.prepareStatement(sql2);
            PS1.setString(1,id);
            PS1.setString(2,oldpassword);
            ResultSet res1 = PS1.executeQuery();
            if (res1.next()){
                String updatesql1 = "update teacherinfo set password = ? where id = ?";//定义要执行的sql语句
                PreparedStatement PS2=conn.prepareStatement(updatesql1);//在sql里执行语句
                PS2.setString(1,newpassword);
                PS2.setString(2,id);
                PS2.executeUpdate();//执行更新
                System.out.println("修改成功!");
            } else {
                System.out.println();
                System.out.println("密码错误");
            }
        }else {
            System.out.println("此用户名还未注册,请先注册再登录!");
        }
        conn.close();
        PS.close();
        res.close();
    }

    //注销(删除)
    public static void deleteUser(Connection conn,String id,String password) throws Exception{
        String sql1 = " select id from teacherinfo where id=? ";
        PreparedStatement PS = conn.prepareStatement(sql1);
        PS.setString(1,id);
        ResultSet res = PS.executeQuery();
        if(res.next()){
            String sql2 = " select * from teacherinfo where id= ? and password= ?";
            PreparedStatement PS1 = conn.prepareStatement(sql2);
            PS1.setString(1,id);
            PS1.setString(2,password);
            ResultSet res1 = PS1.executeQuery();
            if (res1.next()){
                System.out.println("登录成功!");
                String deletesql = "delete from teacherinfo where id = ?";
                PreparedStatement PS2=conn.prepareStatement(deletesql);//在sql里执行语句
                PS2.setString(1,id);
                PS2.executeUpdate();
                System.out.println("删除成功!");
                PS2.close();
                res1.next();
            } else {
                System.out.println();
                System.out.println(res1.next());
                System.out.println("密码错误");
            }
        }else {
            System.out.println("此账号还未注册,请先注册再登录!");
        }
        conn.close();
        PS.close();
        res.close();
    }



}
  • 13
    点赞
  • 126
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值