学习java第八天

音乐管理系统
界面

package com.zhongruan.service;

import com.sun.deploy.panel.DeleteFilesDialog;
import com.zhongruan.bean.User;
import com.zhongruan.dao.UserDao;
import com.zhongruan.util.DBUtil;
import javafx.scene.input.InputEvent;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Scanner;

public class MusicService {

    public static void jiemian() throws Exception {
        Scanner input= new Scanner(System.in);
        System.out.println("请输入用户名");
        String username =input.next() ;
        System.out.println("请输入密码");
        String password =input.next() ;

        UserDao userDao =new UserDao();
        boolean flag = userDao.login(username,password);
    if(flag){
        System.out.println("-----------登录成功----------");
        System.out.println("-----------欢迎来到音乐管理系统--------");
        System.out.println("\t1.音乐管理\t2.用户管理\t3.退出系统");
    }

   if(!flag){
            System.out.println("登录失败");
            System.out.println("请先注册");
            System.out.println("请输入注册名");
            String zhuce= input.next();
            System.out.println("请输入注册密码");
            String zcmm =input.next();
            System.out.println("请确认密码");
            String zcmm2 =input.next();
            if(zcmm.equals(zcmm2)){
                userDao.zhuce(username,password);
                System.out.println("注册成功");
                System.out.println("是否登录,还是退出Y/N");
                String i=input.next();
                if(i.equals("N")){
                    System.out.println("输错了");
                    System.exit(0);
                }
                else if(i.equals("Y")){
                    System.out.println("欢迎您");
                    System.out.println("-----------登录成功----------");
                    System.out.println("-----------欢迎来到音乐管理系统--------");
                    System.out.println("\t1.音乐管理\t2.用户管理\t3.退出系统");
                }
                else{
                    System.out.println("密码有误,请重新输入");
                }
            }
            else{
                System.out.println("密码有误,请重新注册");
            }

        }
    }


       public static void sing() throws Exception {

           Scanner input = new Scanner(System.in);


               System.out.println("-----------欢迎来到音乐管理系统--------");
               System.out.println("\t1.查询音乐\t2.添加音乐\t3.修改音乐\t4.删除音乐");
               int i = input.nextInt();
               switch (i) {
                   case 1:
                       Select.select();
                       break;
                   case 2:
                       Add.add();

                       break;
                   case 3:
                       Upade.upade();
                       break;
                   case 4:
                       System.out.println("请输入你要删除的歌");
                       int id = input.nextInt();
                       Delete.delete(id);
                       break;
               }
            }
        public static void user() throws SQLException {
            System.out.println("-----------欢迎来到用户管理系统--------");
            System.out.println("\t1.查询用户\t2.添加用户\t3.修改用户\t4.删除用户");
            Scanner input = new Scanner(System.in);
            int k =input.nextInt();
            switch (k){
                case 1:UserDao.select();
                    break;
                case 2:UserDao.add();

                    break;
                case 3:UserDao.upade();
                    break;
                case 4:UserDao.delete();

                    break;
            }
        }




    public static void main(String[] arge) throws Exception {
        Connection connection= DBUtil.getConnection();
        PreparedStatement statement =null;
        Scanner input  = new Scanner (System.in);
        jiemian();
        int j=input.nextInt();

        if(j==1){
            sing();
        }
        else if(j==2){
            user();
        }



    }

}

用户的增删改查

ckage com.zhongruan.dao;

import com.sun.xml.internal.bind.v2.model.core.ID;
import com.zhongruan.bean.User;
import com.zhongruan.service.Select;
import com.zhongruan.util.DBUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.sql.*;
import java.util.Collections;

import static com.zhongruan.service.MusicService.user;

public class UserDao {


    public boolean login(String username, String password) throws SQLException {
        Connection connection = DBUtil.getConnection();
        ResultSet resultSet = null;
        PreparedStatement statement = null;


            String sql = "select *from tb_user where username =? and password=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return  true;
            }
            else {
                return  false;
            }




    }

    public boolean zhuce(String username, String password) throws SQLException {
        Connection connection = DBUtil.getConnection();
        String sql = "insert into tb_user (username, password) values(?, ?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,username);
        statement.setString(2,password);
        int i = statement.executeUpdate();
        if(i != 0){
            return true;
        }else{
            return false;
        }
    }
    public static void select() throws SQLException {
        List<User>users =new ArrayList<>();
        ResultSet resultSet =null;
        Connection connection=null;
        PreparedStatement statement =null;
        connection =DBUtil.getConnection();
        try {
            statement=connection.prepareStatement("select *from tb_user");
            resultSet = statement.executeQuery();
            while (resultSet.next()){

                int id  =resultSet.getInt(1);
                String  username =resultSet.getString(2);
                String password =resultSet.getString(3);
                User user = new User();
                user.setId(id);
                user.setUsername(username);
                user.setPossword(password);
                users.add(user);
        }
            System.out.println(users);
            user();

        }catch (SQLException e){
            e.printStackTrace();
        }


    }
    public static void add() throws SQLException{
        Connection connection =null;
        PreparedStatement statement =null;
        connection = DBUtil.getConnection();
        Scanner input =new Scanner(System.in);
        try {
            statement = connection.prepareStatement("insert  int  tb_user (username,password) values (?,?)");
            System.out.println("请输入用户名");
            statement.setString(1,input.next());
            System.out.println("请输入密码");
            statement.setString(2,input.next());
            int i = statement.executeUpdate();
            if(i!=0){
                System.out.println("添加成功");
                user();
            }
            else{
                System.out.println("no");
            }

        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    public static void delete(){
        Connection connection =null;
        PreparedStatement statement =null;
        connection = DBUtil.getConnection();
        Scanner input =new Scanner(System.in);
        try {
            statement = connection.prepareStatement("delete from  tb_user where id=?");
            System.out.println("请输入你要删除的用户");
            int id = input.nextInt();
            statement.setString(1, String.valueOf(id));
            int  i  =statement.executeUpdate();
            if(i!=0){
                System.out.println("删除成功");
                user();
            }
            else{
                System.out.println("no");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    public static void upade(){
        Connection connection =null;
        PreparedStatement statement =null;
        connection = DBUtil.getConnection();
        Scanner input =new Scanner(System.in);
        try {
            statement=connection.prepareStatement("update tb_user  set username=?  where password=?");
            System.out.println("请输入用户名");
            String username = input.next();
            System.out.println("请输入原密码");
            String password = input.next();
            statement.setString(1,username);
            statement.setString(2,password);
            int i =statement.executeUpdate();
            if(i!=0){
                System.out.println("修改成功");
                user();
            }
            else{
                System.out.println("no");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



}

工具类

ackage com.zhongruan.util;

import java.io.IOException;
import java.sql.*;

public class DBUtil {
  public static Connection getConnection(){
      Connection connection =null;
      try {
          //连接
          Class.forName("com.mysql.jdbc.Driver");
          connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nbcj?" +
                  "useSSL=true&characterEncoding=utf-8&user=root&password=123456");
      }catch (Exception e){
          e.printStackTrace();
      }
  return  connection;
  }
    public static void closeAll(ResultSet resultSet, PreparedStatement statement,Connection connection){
  if(resultSet!=null){
      try {
          resultSet.close();
      }catch (SQLException e){
          e.printStackTrace();
      }
  }
        if(statement!=null){
            try {
                statement.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值