天天看點

java web入門到精通java調用mysql存儲過程執行個體

1、效果圖:

java web入門到精通java調用mysql存儲過程執行個體

2、目錄圖:

java web入門到精通java調用mysql存儲過程執行個體

3、Book.java

package hh.proc;

public class Book {
	private int id;
	private String name;
	private double price;
	private int bookCount;
	private String author;
	
	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 getAuthor(){
		return author;
	}
	public void setAuthor(String author){
		this.author=author;
	}
}
           

4、FindBook

package hh.proc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;



public class FindBook {
	
//	public static void main(String[] args) {
//		findAll();
//	}
	
	
	public Connection getConnection(){
		Connection conn=null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/hh";
	        String username = "root";
	        String password = "root";
	        conn=DriverManager.getConnection(url,username,password);
	        
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch(SQLException e){
			e.printStackTrace();
		}
		return conn;
	}
	
	public List<Book> findAll(){
		List<Book> list=new ArrayList<Book>();
		Connection conn=getConnection();
		try{
			CallableStatement cs=conn.prepareCall("{call findBook()}");
			ResultSet rs=cs.executeQuery();
			while(rs.next()){
				Book book =new Book();
				book.setId(rs.getInt("id"));
				book.setName(rs.getString("name"));
				book.setPrice(rs.getDouble("price"));
				book.setBookCount(rs.getInt("bookCount"));
				book.setAuthor(rs.getString("author"));
				list.add(book);
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
}
           

5、index.asp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="hh.proc.Book"%>
<%@page import="hh.proc.FindBook"%>
<%@page import="java.util.List"%>

<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<jsp:useBean id="findBook" class="hh.proc.FindBook"></jsp:useBean>
	<table align="center" width="450" >
		<tr>
			<td align="center" colspan="5">
				<h2>所有圖書資訊</h2>
			</td>
		</tr>
		<tr align="center">
			<td><b>ID</b></td>
			<td><b>圖書名稱</b></td>
			<td><b>價格</b></td>
			<td><b>數量</b></td>	
			<td><b>作者</b></td>	
		</tr>
		<%
			List<Book> list=findBook.findAll();
			if(list==null || list.size()<1){
				out.print("沒有資料");
			}
			else{
				for(Book book:list){
					%>
					<tr align="center">
						<td><%=book.getId() %></td>
						<td><%=book.getName() %></td>
						<td><%=book.getPrice() %></td>
						<td><%=book.getBookCount() %></td>
						<td><%=book.getAuthor() %>
					</tr>
					<%
				}
			}
		%>
	</table>
</body>
</html>
           

6、總結:注意引用mysql庫時。

java web入門到精通java調用mysql存儲過程執行個體

8、源碼下載下傳:

http://pan.baidu.com/s/1boVWPwN