音乐管理系统搭建2

音乐管理系统搭建2

import univ.nbcj.bean.Music;
import univ.nbcj.service.User;
import univ.nbcj.util.DBUtil;

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


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 boolean login(String username, String password, int type) throws SQLException {
        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()) {
            return true;
        } else {
            return false;
        }
    }

    public static List<User> findAll() throws SQLException {
        List<User> users = new ArrayList<>();
        Connection connection = DBUtil.getconnection();
        PreparedStatement statement = connection.prepareStatement("select *from tb_user");
        ResultSet 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.setPassword(password);
            users.add(user);

        }
        return users;
    }
    public static void add(User user) throws SQLException{
        Connection connection=DBUtil.getconnection();
        PreparedStatement statement= connection.prepareStatement
                ("insert into tb_user(username,password,type) value (?,?,?)");
        statement.setString(1,"123");
        statement.setString(2,"123");
        statement.setInt(3,1);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值