当前位置: 首页 > 工具软件 > jujube-jdbc > 使用案例 >

JDBC..

焦阎宝
2023-12-01
package TEST_BYME.day01.tets01;

import TEST_BYME.day01.entity.StoreDetails;
import org.junit.jupiter.api.Test;

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

/**
 * @Description:
 * @author:myh
 * @date: 2021/6/23  10:43
 */
public class TestPrepareStatement {
    public List<StoreDetails> getAll(){
        List<StoreDetails>list=new ArrayList<>();
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/retail1_db", "root", "123456");
            String sql="select * from store_details";
            ps=connection.prepareStatement(sql);

            //
            rs=ps.executeQuery();

            while (rs.next()) {
                String storeId = rs.getString("store_id");
                String storeName = rs.getString("store_name");
                String employeeNumber = rs.getString("employee_number");

                StoreDetails storeDetails = new StoreDetails(storeId, storeName, employeeNumber);

                list.add(storeDetails);
            }




        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null) {
                    rs.close();
                }

                if(ps != null) {
                    ps.close();
                }

                if(connection != null) {
                    connection.close();
                }
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
        }


        return list;
    }

    public List<StoreDetails> getAll(Object ... params) {

        List<StoreDetails> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        //1.获取连接
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/retail1_db", "root", "root");

            //2.获取PS
            String sql = "select * from store_details where 1=1 and store_name like ?";
            ps = connection.prepareStatement(sql);

            //3.填充占位符
            if(params != null && params.length > 0) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i+1,params[i]);
                }
            }

            //4.执行查询
            rs =  ps.executeQuery();

            //5.处理rs
            while (rs.next()) {
                String storeId = rs.getString("store_id");
                String storeName = rs.getString("store_name");
                String employeeNumber = rs.getString("employee_number");

                StoreDetails storeDetails = new StoreDetails(storeId, storeName, employeeNumber);

                list.add(storeDetails);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null) {
                    rs.close();
                }

                if(ps != null) {
                    ps.close();
                }

                if(connection != null) {
                    connection.close();
                }
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
        }
        return list;
    }


    @Test
    public void testQueryAll() {
        List<StoreDetails> list = getAll();

        //list的几种遍历方式????
        //1.增强for  2.经典for  3.迭代器  4.foreach  5.Stream ---》 处理里面的数据
        list.forEach(System.out::println);
        //  list.forEach(t -> System.out.println(t));

        System.out.println("===================================");


        List<StoreDetails> queryByConditions = getAll("%Food%");
        queryByConditions.forEach(System.out::println);
    }
}

快捷键   跳出 try catch 语句   选中然后CTRL+ALT+T

 类似资料: