java web入门到精通java调用mysql存储过程实例

来源:互联网 发布:java 毫秒转日期 编辑:程序博客网 时间:2024/06/03 19:56

1、效果图:


2、目录图:


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" border="1"><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库时。


8、源码下载:

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

1 0
原创粉丝点击