音乐管理系统搭建3

音乐管理系统搭建3

package com.app.dao;

import com.app.bean.User;
import com.app.util.DBUtil;

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

public class UserDao {

    public boolean register(String username, String password, int type) throws SQLException {
        Connection connection = DBUtil.getconnection();
        String sql = "INSERT INTO tb_user (username,password,type) VALUES (?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, username);
        statement.setString(2, password);
        statement.setInt(3, type);
        int i = statement.executeUpdate();
        if (i != 0) {
            return true;
        } else {
            return false;
        }
    }

    public int login(String username, String password) throws SQLException {
        int type=0;
        Connection connection = DBUtil.getconnection();
        String sql = "select * from tb_user where username=? and password=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, username);
        statement.setString(2, password);
        ResultSet resultSet = statement.executeQuery();
        if(resultSet.next()){
            type=resultSet.getInt(4);
        }
        return type;

        /*if (resultSet.next()) {
            return true;
        } else {
            return false;
        }*/
    }

    public List<User> select() throws SQLException {
        List<User> user = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        String username, password;
        int id, type;
        System.out.println("请输入id");
        id = input.nextInt();
        Connection connection = DBUtil.getconnection();
        PreparedStatement statement = connection.prepareStatement("select * from tb_user where id=?");
        statement.setInt(1, id);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            id = resultSet.getInt(1);
            username = resultSet.getString(2);
            password = resultSet.getString(3);
            type = resultSet.getInt(4);
            User user1 = new User();
            user1.setId(id);
            user1.setUsername(username);
            user1.setPassword(password);
            user.add(user1);
        }
        return user;

    }

    public boolean modify() throws SQLException {
        Connection connection = DBUtil.getconnection();
        Scanner input = new Scanner(System.in);
        int id;
        id = input.nextInt();
        PreparedStatement statement = connection.prepareStatement("UPDATE tb_user SET username=\"bbb\" WHERE id =?");
        statement.setInt(1,id);
        int result=0;
        result = statement.executeUpdate();
        if (result != 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean del() throws SQLException {
        Connection connection=DBUtil.getconnection();
        Scanner input=new Scanner(System.in);
        int id;
        System.out.println("输入id:");
        id=input.nextInt();
        PreparedStatement statement=connection.prepareStatement("DELETE FROM tb_USER WHERE id=?");
        statement.setInt(1,id);
        int result=0;
        result=statement.executeUpdate();
        if(result!=0){
            return  true;
        }else{
            return false;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值