首页 > 其他分享 >5.22总结

5.22总结

时间:2023-05-22 16:56:10浏览次数:37  
标签:总结 String brand Connection 5.22 SQL id pstmt

package com.mf.jdbc.exmaple;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mf.jdbc.Brand;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**

  • 品牌数据的增删改查操作
    */
    public class BrandTest {

    /**

    • 查询所有
      1. SQL:select * from tb_brand;
      1. 参数:不需要
      1. 结果:List
        */

    @Test
    public void testSelectAll() throws Exception {
    //1. 获取Connection
    //3. 加载配置文件
    Properties prop = new Properties();
    prop.load(new FileInputStream("./src/druid.properties"));
    //4. 获取连接池对象
    DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

     //5. 获取数据库连接 Connection
     Connection conn = dataSource.getConnection();
    
     //2. 定义SQL
     String sql = "select * from tb_brand;";
    
     //3. 获取pstmt对象
     PreparedStatement pstmt = conn.prepareStatement(sql);
    
     //4. 设置参数
    
     //5. 执行SQL
     ResultSet rs = pstmt.executeQuery();
    
     //6. 处理结果 List<Brand> 封装Brand对象,装载List集合
     Brand brand = null;
     List<Brand> brands = new ArrayList<>();
     while (rs.next()){
         //获取数据
         int id = rs.getInt("id");
         String brandName = rs.getString("brand_name");
         String companyName = rs.getString("company_name");
         int ordered = rs.getInt("ordered");
         String description = rs.getString("description");
         int status = rs.getInt("status");
         //封装Brand对象
         brand = new Brand();
         brand.setId(id);
         brand.setBrandName(brandName);
         brand.setCompanyName(companyName);
         brand.setOrdered(ordered);
         brand.setDescription(description);
         brand.setStatus(status);
    
         //装载集合
         brands.add(brand);
    
     }
     System.out.println(brands);
     //7. 释放资源
     rs.close();
     pstmt.close();
     conn.close();
    

    }

    /**

    • 添加
      1. SQL:insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);
      1. 参数:需要,除了id之外的所有参数信息
      1. 结果:boolean
        */

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

     //1. 获取Connection
     //3. 加载配置文件
     Properties prop = new Properties();
     prop.load(new FileInputStream("./src/druid.properties"));
     //4. 获取连接池对象
     DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    
     //5. 获取数据库连接 Connection
     Connection conn = dataSource.getConnection();
    
     //2. 定义SQL
     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();
    

    }

    /**

    • 修改
      1. SQL:

    update tb_brand
    set brand_name = ?,
    company_name= ?,
    ordered = ?,
    description = ?,
    status = ?
    where id = ?

      1. 参数:需要,所有数据
      1. 结果:boolean
        */

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

     //1. 获取Connection
     //3. 加载配置文件
     Properties prop = new Properties();
     prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
     //4. 获取连接池对象
     DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    
     //5. 获取数据库连接 Connection
     Connection conn = dataSource.getConnection();
    
     //2. 定义SQL
     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();
    

    }

    /**

    • 删除
      1. SQL:

    delete from tb_brand where id = ?

      1. 参数:需要,id
      1. 结果:boolean
        */

    @Test
    public void testDeleteById() throws Exception {
    // 接收页面提交的参数

     int id = 4;
    
    
     //1. 获取Connection
     //3. 加载配置文件
     Properties prop = new Properties();
     prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
     //4. 获取连接池对象
     DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
    
     //5. 获取数据库连接 Connection
     Connection conn = dataSource.getConnection();
    
     //2. 定义SQL
     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();
    

    }
    }

标签:总结,String,brand,Connection,5.22,SQL,id,pstmt
From: https://www.cnblogs.com/XiMenXve/p/17421052.html

相关文章

  • 计算题总结
    目录计算题1.给定一个长度为n的整数数组nums和一个目标值target,请你从nums中选出三个整数,使得它们的和与target最接近。2.以下是一个函数的示例代码,它接受一个32位有符号整数x,并返回其数字部分反转后的结果。如果反转后的整数超出了32位有符号整数的范围[-2^31,2^31-1]......
  • irq中断相关(2023.5.22)
    //irq29:nobodycared(trybootingwiththe"irqpoll"option) //aer_irqthreaded   aer_isrDIsablingIRQ#105 https://blog.csdn.net/Guet_Kite/article/details/106689126note_interrupt(){if(unlikely(desc->irqs_unhandled>99900)){ ......
  • Redhat7.3linux系统防火墙命令总结
    在Linux系统部署皕杰报表后,需要关闭防火墙或者开放报表工具使用的端口,才能通过浏览器访问报表。在linux中如何关闭防火墙或开启端口呢?基本上是基于命令操作。通过几天的实践,总结了有关防火墙的操作命令,现记录如下。systemctlstatusfirewalld:查看防火墙状态systemctlstartfirewa......
  • 2023江西省赛赛后总结
    大一acmer的第二场线下赛(第一场是天梯赛。去年省赛是线上赛,结果我还因为时间冲突没有去,最后只有我的两个队友去了),比赛前一天晚上睡不着,早上坐车去比赛的时候就一直很困,比赛开始后却立马精神了。最后只过了四题,拿了个三等奖,我好菜啊。。。。。。别人都是fake,只有我是真菜。。。......
  • 【杂文随笔】2017年总结 送自己一个字
    .......
  • 【杂文随笔】2019年总结 送自己一个字
    ......
  • 5.22打卡
      3.程序流程图 4.代码实现#include<bits/stdc++.h>usingnamespacestd;inta[14];main(){inti,j=1,n;printf("ħÊõʦÊÖÖеÄÅÆԭʼ³ÌÐòÊÇ£º\n");for(i=1;i<=13;i++){n=1;do{......
  • 信息收集学习笔记总结
    1.域名信息(来自csdn)在渗透测试过程中,一般在目标的主站很少有发现漏洞点的存在,这时候我们就要从从主站之外的接口进行渗透测试,这时我们可以从域名出发收集信息。(1)端口一个网站可能会开放多个不同的端口,而我们可以通过同一网站的不同端口进行测试,扫描开放端口的......
  • 办公位2.0,用SVG实现Chrome浏览器图标,文末有近半年经验总结
    功能拆解很多图形的实现并不困难,我之前的文章也实现过各式各样的图形。基本是CSS里的样式约熟悉,图形实现的越快速、越相似。还有一些需要SVG或Canvas实现的图形,这就需要这两项技术的基本功扎实了。简单图形设计整个画面中有很多图形,有些图形比如画框、太阳、桌腿、便签,无论是形状还......
  • c语言程序设计知识点总结03
    c语言程序设计知识点总结03地址(Address):计算机的内存由若干个字节内存单元构成,每个字节内存单元都有一个唯一的地址用于区分和存取单元中的数据。形式上,地址是一个无符号整数,从0开始,依次递增,在表达和交流时,通常把地址写成十六进制数。指针(Pointer):一个变量,它存有另外一......