1. 程式人生 > >JDBC之——PreparedStatement實現對資料庫的增刪改操作

JDBC之——PreparedStatement實現對資料庫的增刪改操作

一、PreparedStatement介面引入

PreparedStatement是Statement的子介面,屬於預處理操作,與直接使用Statement不同的是,PreparedStatement在操作時,是現在資料表中準備好了一條SQL語句,但是此SQL語句的具體內容暫時不設定,二十之後再進行設定。

(以後開發一部用PreparedStatement,不用Statement)

二、使用PreparedStatement介面實現新增資料操作

//工具
public class DbUtil {
	
	//資料庫地址
	private static String dbUrl="jdbc:mysql://localhost:3306/db_book";
	//使用者名稱
	private static String dbUserName="root";
	//密碼
	private static String dbPassword="root";
	//驅動名稱
	private static String jdbcName="com.mysql.jdbc.Driver";
	
	/**
	 * 獲取資料庫連線
	 * 	1.載入資料庫驅動
	 *  2.獲取資料庫連線
	 */
	public Connection getCon() throws Exception {
		
		Class.forName(jdbcName);//載入資料庫驅動
		
		Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
		
		return con;
	}
	
	/**
	 * 關閉連線
	 */
	public void close(Statement stmt,Connection con) throws Exception {
		if(stmt!=null) {
			stmt.close();
		}
		if(con!=null) {
			con.close();
		}
	}
}


//book物件
public class Book {
	private int id;
	private String bookName;
	private float price;
	private String author;
	private int bookTypeId;
	
	public Book(String bookName, float price, String author, int bookTypeId) {
		super();
		this.bookName = bookName;
		this.price = price;
		this.author = author;
		this.bookTypeId = bookTypeId;
	}
	
	public Book(int id, String bookName, float price, String author, int bookTypeId) {
		super();
		this.id = id;
		this.bookName = bookName;
		this.price = price;
		this.author = author;
		this.bookTypeId = bookTypeId;
	}
}

//測試demo
public class Demo01{
	private static DbUtil dbUtil = new DbUtil();
	
	private static int addBook(Book book)throws Exception{
		Connection con = dbUtil.getCon();
		String sql = "insert into t_book values(null,?,?,?,?)";
		
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());//給第一個坑設值
		pstmt.setFloat(2, book.getPrice());
		pstmt.setString(3, book.getAuthor());
		pstmt.setInt(4, book.getBookTypeId());
		int result = pstmt.executeUpdate();
		dbUtil.close(pstmt, con);
		return result;
	}
	public static void main(String[] args) throws Exception {
		Book book = new Book("java測試", 99, "測試", 1);
		int result = addBook(book);
		if(result ==1) {
			System.out.println("新增成功");
		}else {
			System.out.println("新增失敗");
		}
	}
}

三、使用PreparedStatement介面實現更新資料操作

public class demo02 {
	private static DbUtil dbUtil = new DbUtil();
	
	private static int updateBook(Book book)throws Exception{
		Connection con = dbUtil.getCon();
		String sql = "update t_book set bookName=?,price=?,author=?,bookTypeId=? where id=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, book.getBookName());
		pstmt.setFloat(2, book.getPrice());
		pstmt.setString(3, book.getAuthor());
		pstmt.setInt(4, book.getBookTypeId());
		pstmt.setInt(5, book.getId());
		int result = pstmt.executeUpdate();
		dbUtil.close(pstmt, con);
		return result;
	}
	public static void main(String[] args) throws Exception {
		Book book = new Book(1, "book1", 222, "book作者", 3);
		int result = updateBook(book);
		if(result ==1) {
			System.out.println("更新資料成功");
		}else {
			System.out.println("更新資料失敗");
		}
	}
}

四、使用PreparedStatement介面實現刪除資料操作

public class Demo3 {
	private static DbUtil dbUtil = new DbUtil();
	
	private static int deleteBook(int id) throws Exception{
		Connection con = dbUtil.getCon();
		String sql = "delete from t_book where id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, id);
		int result = pstmt.executeUpdate();
		dbUtil.close(pstmt, con);
		return result;
	}
	public static void main(String[] args) throws Exception {
		int result = deleteBook(6);
		if(result ==1 ) {
			System.out.println("刪除成功");
		}else {
			System.out.println("刪除失敗");
		}
	}
}