17 soft work 2 shifts Ningyi Jian JDBC Book JAVA fourth class jobs

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/xixihahah574/article/details/102483774

1. Use your own words express MVC.
MVC is a design pattern, we have learned in the javaweb, M refers to the MVC model corresponding JavaBean entity class, Dao, V refers to a value corresponding to a front end view of the HTML page, and the like, C is the Servlet corresponding to the controller means . MVC pattern to make a project code-behind, their duties and ease of development and maintenance. Let's look at the code and was only able to clearly know the role of each code.
2. Master the basic operation of JDBC.
Data book tables were CRUD;
book table: the above mentioned id int, bookName VARCHAR, int. Price, the Description VARCHAR
Book categories: id int, bookName String, price int, description String
Tip: Create a database connection tools DBUtil class.

(1) First, as shown in Table first build
Here Insert Picture Description
Here is the code:

package com.nyj.JDBC4;

public class Book {
	private int id;
	private String bookName;
	private int price;
	private String description;
	public Book(){
		
	}
	public Book(int id, String bookName, int price, String description) {
		this.id = id;
		this.bookName = bookName;
		this.price = price;
		this.description = description;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	@Override
	public String toString() {
		return "Book [id=" + id + ", bookName=" + bookName + ", price=" + price + ", description=" + description + "]";
	}
	
	

}

package com.nyj.JDBC4;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection(){
		String sql="jdbc:mysql://localhost:3306/javahomework?useUnicode=true&characterEncoding=UTF-8";
		Connection conn;
		try {
			conn = DriverManager.getConnection(sql, "root", "123456");
			return conn;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	public static void close(Connection conn){
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement ps){
		try {
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(ResultSet rs){
		try {
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

package com.nyj.JDBC4;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 * 书本的增删改查功能
 * @author 宁一健
 *
 */

public class BookTest {

	public static void main(String[] args) {
//		Book book = new Book();
//		book.setId(1004);
//		book.setBookName("java");
//		book.setPrice(50);
//		book.setDescription("好书");
//		saveBook(book);
		//deleteBookById(1003);
//		updateBook(book);
		//findBookById(1003);
		fidBookByPrice(500);

	}

	/**
	 * 添加书本的方法
	 */
	public static void saveBook(Book book) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("insert into book(id,bookName,price,description)values(?,?,?,?)");
			pstmt.setInt(1, book.getId());
			pstmt.setString(2, book.getBookName());
			pstmt.setInt(3, book.getPrice());
			pstmt.setString(4, book.getDescription());
			int rs = pstmt.executeUpdate();
			System.out.println(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt!=null)
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 删除书本的方法
	 */
	public static void deleteBookById(int id) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("delete from book where id=?");
			pstmt.setInt(1, id);
			int rs = pstmt.executeUpdate();
			System.out.println(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt!=null)
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}
	/**
	 * 修改书本的方法   id
	 * 
	 */
	public static void updateBook(Book book){
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("update book set bookName=?,price=?,description=? where id=?");
			pstmt.setString(1, book.getBookName());
			pstmt.setInt(2, book.getPrice());
			pstmt.setString(3, book.getDescription());
			pstmt.setInt(4, book.getId());
			int rs = pstmt.executeUpdate();
			System.out.println(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt!=null)
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	/**
	 * 查询书本的方法   id
	 */

	public static Book findBookById(Integer id) {
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		Book book = new Book();
		try {
			pstmt = conn.prepareStatement("select * from book where id=?");
			pstmt.setInt(1, id);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				book.setId(rs.getInt("id"));
				book.setBookName(rs.getString("bookName"));
				book.setPrice(rs.getInt("price"));
				book.setDescription(rs.getString("description"));
				System.out.println(book);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (pstmt!=null)
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return book;
	}
	/**
	 * 查询书本的方法 price
	 */
	public static List<Book> fidBookByPrice(int price){
		Connection conn = DBUtil.getConnection();
		Book book = new Book();
		List<Book> list = new ArrayList<>();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("select * from book where price =?");
			pstmt.setInt(1, price);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				book.setId(rs.getInt("id"));
				book.setBookName(rs.getString("bookName"));
				book.setPrice(rs.getInt("price"));
				book.setDescription(rs.getString("description"));
				list.add(book);
				for(Book list2:list)
				System.out.println(list2);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(pstmt!=null)
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;

	}

}

Guess you like

Origin blog.csdn.net/xixihahah574/article/details/102483774