对数据库进行增删改查:PreparedStatement

对数据库进行增删改查:PreparedStatement

package com.dbsy.login;

import com.dbsy.bean.Users;
import com.dbsy.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Demo03PreparedStatement {
    public static void main(String[] args) throws SQLException {
//        insert();
//        update();
//        delete();
        select();
    }

    private static void select() throws SQLException {
        Connection conn = JDBCUtils.getConnection();
        String sql="select * from users";
        PreparedStatement pst = conn.prepareStatement(sql);

        ResultSet rs = pst.executeQuery();

        ArrayList<Users> list = new ArrayList<>();

        while (rs.next()){
            int uid = rs.getInt("uid");
            String username = rs.getString("username");
            String password = rs.getString("password");

            Users users = new Users(uid, username, password);
            
            list.add(users);
        }
        for (Users users : list) {
            System.out.println(users);
        }
        JDBCUtils.close(rs,pst,conn);
    }

    private static void delete() throws SQLException {
        Connection conn = JDBCUtils.getConnection();
        String sql="delete from users where uid in (?,?);";
        PreparedStatement pst = conn.prepareStatement(sql);
        
        pst.setObject(1,5);
        pst.setObject(2,4);

        int row = pst.executeUpdate();
        System.out.println(row+"行数据删除成功!");

        JDBCUtils.close(pst,conn);
    }

    private static void update() throws SQLException {
        Connection conn = JDBCUtils.getConnection();
        String sql="update users set password=? where username=?";
        PreparedStatement pst = conn.prepareStatement(sql);

        pst.setObject(1,"jialing");
        pst.setObject(2,"贾玲");

        int row = pst.executeUpdate();
        System.out.println(row+"行数据更改成功!");

        JDBCUtils.close(pst,conn);
    }

    private static void insert() throws SQLException {
        Connection conn = JDBCUtils.getConnection();
        String sql="insert into users(username,password) values(?,?),(?,?),(?,?);";
        PreparedStatement pst = conn.prepareStatement(sql);

        pst.setObject(1,"鹿晗");
        pst.setObject(2,"8888");
        pst.setObject(3,"王一博");
        pst.setObject(4,"6666");
        pst.setObject(5,"贾玲");
        pst.setObject(6,"9999");

        int row = pst.executeUpdate();
        System.out.println(row+"行数据添加成功!");

        JDBCUtils.close(pst,conn);
    }
}
package com.dbsy.bean;

public class Users {
    private int uid;
    private String username;
    private String password;

    public Users() {
    }

    public Users(int uid, String username, String password) {
        this.uid = uid;
        this.username = username;
        this.password = password;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Users{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值