1. 程式人生 > >通過jdbc操作資料庫之新增資料

通過jdbc操作資料庫之新增資料

1、在mysql資料庫中建立圖書資訊表books。

create table books(
    id integer primary key not null auto_increment,
    bookname varchar(45) not null default 'zero',
    price double not null default 0.0,
    bookCount integer unsigned not null default 0,
    author varchar(45) not null default 'zero');
2、建立名為Book的JavaBean來封裝圖書資訊。
package com.Bean;

public class Book {
	private int id;
	private String name;
	private double price;
	private int bookCount;
	private String auth;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getBookCount() {
		return bookCount;
	}
	public void setBookCount(int bookCount) {
		this.bookCount = bookCount;
	}
	public String getAuth() {
		return auth;
	}
	public void setAuth(String auth) {
		this.auth = auth;
	}
	
 }
3、建立Add.jsp頁面,用於放置新增圖書資訊的表單,而此頁面得到的資訊將被提交到AddBook.jsp頁面中進行處理。
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增圖書資訊</title>
<script type="text/javascript">
	function check(form){
		with(form){
			if(name.value==""){
				alert("姓名不能為空");
				return false;
			}
			if(price.value==""){
				alert("價格不能為空");
				return false;
			}
			if(bookCount.value==""){
				alert("數量不能為空");
				return false;
			}
			if(author.value=""){
				alert("作者不能為空");
				return false
			}
		}
	} 
</script>
</head>
<body>
<form method="get" action="AddBook.jsp" onsubmit="return check(this);">
	<h1>新增圖書資訊</h1><br>
	<p>圖書名稱:<input type="text" name="name"/></p>
	<p>價        格:<input type="text" name="price"/></p>
	<p>數        量:<input type="text" name="bookCount"/></p>
	<p>作        者:<input type="text" name="auth"/></p>
	<p><input type="submit" value="添   加"></p>
</form>

</body>
</html>
4、AddBook.jsp頁面,處理首頁提交過來的圖書資訊,將圖書資訊新增到資料庫中。
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增到資料庫</title>
</head>
<body>
<%request.setCharacterEncoding("utf-8");%>
<jsp:useBean id="book" class="com.Bean.Book"/>
<jsp:setProperty property="*" name="book"/>
<%
	/**
	*將add.jsp提交過來的資訊新增到mysql資料庫中
	*/
	try{
		Class.forName("com.mysql.jdbc.Driver");//載入資料庫驅動,註冊到驅動管理器
		String url = "jdbc:mysql://localhost:3306/test";//資料庫連線字串
		String username = "root";//資料庫使用者名稱
		String password = "12345";//資料庫密碼
		Connection connect = DriverManager.getConnection(url, username, password);//建立Connection連線
		
		String sql = "insert into books(bookname,price,bookCount,author) values(?,?,?,?)";//新增圖書資訊的sql語句
		PreparedStatement ppstmt = connect.prepareStatement(sql);//返回執行sql語句後生成結果的物件
		ppstmt.setString(1, book.getName());//對sql語句中第1個引數賦值
		ppstmt.setDouble(2, book.getPrice());//對sql語句中第2個引數賦值
		ppstmt.setInt(3, book.getBookCount());//對sql語句中第3個引數賦值
		ppstmt.setString(4, book.getAuth());//對sql語句中第4個引數賦值
		
		int row = ppstmt.executeUpdate();//執行更新操作,返回所影響的行數
		if(row>0){
			out.print("成功添加了"+row+"條資料!");
		}
		ppstmt.close();//關閉PreparedStatement,釋放資源
		connect.close();//關閉Connection,釋放資源。
	}catch(Exception e){
		out.print("圖書資訊新增失敗了!");
		e.printStackTrace();
	}
%>
<a href="add.jsp">返回</a>
</body>
</html>