数据库的增删查改

public List findAll() {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List users = new ArrayList<>();
Connection connection = null;
try {
connection = DBUtil.getConnection(DBUtil.qudong, DBUtil.url);
//3.写sql
String sql = “select * from tb_user”;
//4.得到statement对象
preparedStatement = connection.prepareStatement(sql);
//5.执行sql
resultSet = preparedStatement.executeQuery();
//6.处理结果集
while (resultSet.next()) {
TbUser user = new TbUser();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
users.add(user);
}
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.CloseAll(resultSet, preparedStatement, connection);
}
return users;
}

public boolean doLogin(String username,String password){
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    Connection connection = null;
    int id;
    try {
        connection = DBUtil.getConnection(DBUtil.qudong, DBUtil.url);
        //3.写sql
        String sql = "select * from tb_user where username=? and password=?";
        //4.得到statement对象
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);
        //5.执行sql
        resultSet = preparedStatement.executeQuery();
        //6.处理结果集
        if(resultSet.next()){
            return true;
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return false;
}

public static void insert(String username1,String password1){
    PreparedStatement preparedStatement=null;
    int resultSet;
    Connection connection=null;
    try {
        connection=DBUtil.getConnection(DBUtil.qudong,DBUtil.url);
        String sql="insert into tb_user(username,password) values ('"+username1+"','"+password1+"')";
        preparedStatement=connection.prepareStatement(sql);
        //5.执 行sql
        resultSet=preparedStatement.executeUpdate();
        //6.处理结果集
        //7.关闭资源
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        DBUtil.CloseAll(null,preparedStatement,connection);
    }
}
public static void delete(int id){      //delete(int id1){
    PreparedStatement preparedStatement=null;
    Connection connection=null;
    int resultSet;
    try {
        connection=DBUtil.getConnection(DBUtil.qudong,DBUtil.url);
        String sql="delete from tb_user where id=?";   //where id="+id1;
        preparedStatement=connection.prepareStatement(sql);
        //5.执行sql
        preparedStatement.setInt(1,id);
        resultSet=preparedStatement.executeUpdate();
        //7.关闭资源
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        DBUtil.CloseAll(null,preparedStatement,connection);
    }
}
public static void modify(int id,String username,String password){
    PreparedStatement preparedStatement=null;
    int resultSet;
    Connection connection=null;
    try {
        connection=DBUtil.getConnection(DBUtil.qudong,DBUtil.url);
        String sql="update tb_user set username=? , password=? where id=?";
        preparedStatement=connection.prepareStatement(sql);
        //5.执行sql
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);
        preparedStatement.setInt(3,id);
        resultSet=preparedStatement.executeUpdate();
        //6.处理结果集
        //7.关闭资源
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        DBUtil.CloseAll(null,preparedStatement,connection);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值