zoukankan      html  css  js  c++  java
  • PreparedStatement用法【mysql jdbc List<Book>】

    package com.java1234.jdbc.chap05.sec02;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.java1234.jdbc.model.Book;
    import com.java1234.jdbc.util.DbUtil;
    
    public class Demo1 {
    
        private static DbUtil dbUtil = new DbUtil();
    
        //查找所有图书
        private static void listBook() throws Exception {
            Connection con = dbUtil.getCon(); 
            String sql = "select * from t_book";
            PreparedStatement pstmt = con.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery(); 
            while (rs.next()) {
                int id = rs.getInt(1); 
                String bookName = rs.getString(2); 
                float price = rs.getFloat(3); 
                String author = rs.getString(4); 
                int bookTypeId = rs.getInt(5);
                System.out.println("图书编号" + id + "书名" + bookName + "价格"
                        + price + "作者" + author + "图书类别编号" + bookTypeId);
                System.out
                        .println("=======================================================================");
    
            }
        }
        
        private static void listBook2() throws Exception {
            Connection con = dbUtil.getCon(); 
            String sql = "select * from t_book";
            PreparedStatement pstmt = con.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery(); 
            while (rs.next()) {
                int id = rs.getInt("id"); 
                String bookName = rs.getString("bookName");
                float price = rs.getFloat("price");
                String author = rs.getString("author"); 
                int bookTypeId = rs.getInt("bookTypeId"); 
                System.out.println("图书编号" + id + "书名" + bookName + "价格"
                        + price + "作者" + author + "图书类别编号" + bookTypeId);
                System.out
                        .println("=======================================================================");
    
            }
        }
        
      //获取所有图书,保存到 List<Book> 中
    private static List<Book> listBook3()throws Exception{ List<Book> bookList=new ArrayList<Book>(); Connection con = dbUtil.getCon(); String sql = "select * from t_book"; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String bookName = rs.getString("bookName"); float price = rs.getFloat("price"); String author = rs.getString("author"); int bookTypeId = rs.getInt("bookTypeId"); Book book=new Book(id, bookName, price, author, bookTypeId); bookList.add(book); } return bookList; } public static void main(String[] args) throws Exception { // listBook(); // listBook2(); List<Book> bookList=listBook3(); for (Book book : bookList) { System.out.println(book); } } }
  • 相关阅读:
    如何判定某个类的职责是否够"单一"?
    Guava中的Cache简易源码分析
    为什么尽量少用继承?
    聊聊抽象类和接口
    WINDOW 搭建 ELK 2.4.0
    摇摇棒,理工男的择偶权(上)
    C++值多态:传统多态与类型擦除之间
    C++值元编程
    STM32学习笔记——printf
    C++98/11/17表达式类别
  • 原文地址:https://www.cnblogs.com/CPU-Easy/p/12386680.html
Copyright © 2011-2022 走看看