0
点赞
收藏
分享

微信扫一扫

JDBC的增删改查

i奇异 2022-03-10 阅读 54

JDBC关于数据库的添加操作

public class BrandTest {
        @Test
        public void testAdd() throws Exception{
//          1。  接受页面提交的参数
            String brandName = "香飘飘";
            String companyName = "香飘飘";
            int ordered = 1;
            String description ="地球一圈";
            int status =1;

//            1,获取conneiton
//     3.加载配置文件
            Properties prop =new Properties();
            prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
//     4.获取连接池数据对象
            DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//            5.获取数据库链接
            Connection conn=dataSource.getConnection();
//            2.定义sql语句
//            String sql ="select * from tb_brand";
            String sql ="insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
//            3.获取pstmt对象
            PreparedStatement pstmt = conn.prepareStatement(sql);
//            4。设置参数
            pstmt.setString(1,brandName);
            pstmt.setString(2,companyName);
            pstmt.setInt(3,ordered);
            pstmt.setString(4,description);
            pstmt.setInt(5,status);

//            5.执行sql
            int count=pstmt.executeUpdate();
//            6.处理结果
            System.out.println(count>0);
//        7.释放资源
            pstmt.close();
            conn.close();
    }
}

public class BrandTest {
@Test
public void testAdd() throws Exception{
// 1。 接受页面提交的参数
String brandName = “香飘飘”;
String companyName = “香飘飘”;
int ordered = 1;
String description =“地球一圈”;
int status =1;

// 1,获取conneiton
// 3.加载配置文件
Properties prop =new Properties();
prop.load(new FileInputStream(“C:\Users\lengxin\IdeaProjects\jdbc-demo\lib\druid.properties”));
// 4.获取连接池数据对象
DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
// 5.获取数据库链接
Connection conn=dataSource.getConnection();
// 2.定义sql语句
// String sql =“select * from tb_brand”;
String sql =“insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);”;
// 3.获取pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4。设置参数
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);

// 5.执行sql
int count=pstmt.executeUpdate();
// 6.处理结果
System.out.println(count>0);
// 7.释放资源
pstmt.close();
conn.close();
}
}这里显示为true
这时候刷新数据库 查看数据库的添加操作是否成功
这里可以看见已经添加成功,但发现ID没有修改,下一步做增删改查的修改操作
在这里插入图片描述

*这里在学习过程中犯了一个错误,就是把原来的testAdd增加操作给删除了,导致代码块错误,在这里,修改直接在添加操作下面开始编写代码即可

//修改
//1.update tb_brand
//        set brand_name = ?,
//         company_name = ?,
//         ordered = ?,
//          status  = ?,
//品牌数据的增删改查
public class BrandTest {
    @Test
    public void testAdd() throws Exception{
//          1。  接受页面提交的参数
        String brandName = "香飘飘";
        String companyName = "香飘飘";
        int ordered = 1;
        String description ="地球一圈";
        int status =1;

//            1,获取conneiton
//     3.加载配置文件
        Properties prop =new Properties();
        prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
//     4.获取连接池数据对象
        DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//            5.获取数据库链接
        Connection conn=dataSource.getConnection();
//            2.定义sql语句
//            String sql ="select * from tb_brand";
        String sql ="insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
//            3.获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
//            4。设置参数
        pstmt.setString(1,brandName);
        pstmt.setString(2,companyName);
        pstmt.setInt(3,ordered);
        pstmt.setString(4,description);
        pstmt.setInt(5,status);

//            5.执行sql
        int count=pstmt.executeUpdate();
//            6.处理结果
        System.out.println(count>0);
//        7.释放资源
        pstmt.close();
        conn.close();
    }



    @Test
    public void testUpdate() throws Exception{
//          1。  接受页面提交的参数
        String brandName = "香飘飘";
        String companyName = "香飘飘";
        int ordered = 1000;
        String description ="地球三圈";
        int status =1;
        int id = 4;

//            1,获取conneiton
//     3.加载配置文件
        Properties prop =new Properties();
        prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
//     4.获取连接池数据对象
        DataSource dataSource= DruidDataSourceFactory.createDataSource(prop);
//            5.获取数据库链接
        Connection conn=dataSource.getConnection();
//            2.定义sql语句
//            String sql ="select * from tb_brand";
        String sql ="update tb_brand\n" +
                "set brand_name = ?,\n" +
                "company_name = ?,\n"+
                "ordered = ?,\n"+
                "description = ?,\n"+
                "status = ?\n"+
                "where id = ?";
//            3.获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
//            4。设置参数
        pstmt.setString(1,brandName);
        pstmt.setString(2,companyName);
        pstmt.setInt(3,ordered);
        pstmt.setString(4,description);
        pstmt.setInt(5,status);
        pstmt.setInt(6,id);

//            5.执行sql
        int count=pstmt.executeUpdate();
//            6.处理结果
        System.out.println(count>0);
//        7.释放资源
        pstmt.close();
        conn.close();
    }
}

运行

![显示为TURE](https://img-blog.csdnimg.cn/9209fd27c0544e929bfb9aca0ee6e74f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAd2VpeGluXzUxNDQ4NjE0,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
这里可以查看数据库是否修改成功
![在这里插入图片描述](https://img-blog.csdnimg.cn/3b0b450a592d46fc890799832d54c399.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAd2VpeGluXzUxNDQ4NjE0,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
修改完成

## 下面开始说删除功能
删除是根据id删除,只需要修改sql语句即可
//修改
//1.update tb_brand
//        set brand_name = ?,
//         company_name = ?,
//         ordered = ?,
//          status  = ?,
//品牌数据的增删改查
public class BrandTest {
    @Test
    public void testAdd() throws Exception {
//          1。  接受页面提交的参数
        String brandName = "香飘飘";
        String companyName = "香飘飘";
        int ordered = 1;
        String description = "地球一圈";
        int status = 1;

//            1,获取conneiton
//     3.加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
//     4.获取连接池数据对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//            5.获取数据库链接
        Connection conn = dataSource.getConnection();
//            2.定义sql语句
//            String sql ="select * from tb_brand";
        String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
//            3.获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
//            4。设置参数
        pstmt.setString(1, brandName);
        pstmt.setString(2, companyName);
        pstmt.setInt(3, ordered);
        pstmt.setString(4, description);
        pstmt.setInt(5, status);

//            5.执行sql
        int count = pstmt.executeUpdate();
//            6.处理结果
        System.out.println(count > 0);
//        7.释放资源
        pstmt.close();
        conn.close();
    }


    @Test
    public void testUpdate() throws Exception {
//          1。  接受页面提交的参数
        String brandName = "香飘飘";
        String companyName = "香飘飘";
        int ordered = 1000;
        String description = "地球三圈";
        int status = 1;
        int id = 4;

//            1,获取conneiton
//     3.加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
//     4.获取连接池数据对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//            5.获取数据库链接
        Connection conn = dataSource.getConnection();
//            2.定义sql语句
//            String sql ="select * from tb_brand";
        String sql = "update tb_brand\n" +
                "set brand_name = ?,\n" +
                "company_name = ?,\n" +
                "ordered = ?,\n" +
                "description = ?,\n" +
                "status = ?\n" +
                "where id = ?";
//            3.获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
//            4。设置参数
        pstmt.setString(1, brandName);
        pstmt.setString(2, companyName);
        pstmt.setInt(3, ordered);
        pstmt.setString(4, description);
        pstmt.setInt(5, status);
        pstmt.setInt(6, id);

//            5.执行sql
        int count = pstmt.executeUpdate();
//            6.处理结果
        System.out.println(count > 0);
//        7.释放资源
        pstmt.close();
        conn.close();
    }

    @Test
    public void testDeleteByid() throws Exception {
//          1。  接受页面提交的参数
        int id = 4;

//            1,获取conneiton
//     3.加载配置文件
        Properties prop = new Properties();
        prop.load(new FileInputStream("C:\\Users\\lengxin\\IdeaProjects\\jdbc-demo\\lib\\druid.properties"));
//     4.获取连接池数据对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//            5.获取数据库链接
        Connection conn = dataSource.getConnection();
//            2.定义sql语句
//            String sql ="select * from tb_brand";
        String sql = "delete from tb_brand where id = ?";
//            3.获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
//            4。设置参数

        pstmt.setInt(1, id);

//            5.执行sql
        int count = pstmt.executeUpdate();
//            6.处理结果
        System.out.println(count > 0);
//        7.释放资源
        pstmt.close();
        conn.close();
    }
}

## 总结:增删改查基于3步
1.想要通过sql语句进行什么操作
2.需要用什么参数
3.要得到什么样的结果

举报

相关推荐

0 条评论