PreparedStatement用法【mysql jdbc List<Book>】

好久不见. 提交于 2020-02-29 23:44:56

 

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);
        }
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!