//
运行结果后显示页面
<% @page import = " cn.java.BookBean " %>
< html >
< head >
< title > jjjj </ title >
</ head >
< body >
< jsp:useBean id = " bookBean " type = " cn.java.BookBean " scope = " request " ></ jsp:useBean >
< ul >
< li > ÊéÃû£º < jsp:getProperty name = " bookBean " property = " title " /></ li >
< li > ÊéºÅ: < jsp:getProperty name = " bookBean " property = " isbn " /></ li >
< li > ×÷Õߣº < jsp:getProperty name = " bookBean " property = " author " />< li >
< li > ³ö°æÉ磺 < jsp:getProperty name = " bookBean " property = " publisher " /></ li >
< li > ¶¨¼Û: < jsp:getProperty name = " bookBean " property = " price " /></ li >
</ ul >
</ body >
</ html >
<% @page import = " cn.java.BookBean " %>
< html >
< head >
< title > jjjj </ title >
</ head >
< body >
< jsp:useBean id = " bookBean " type = " cn.java.BookBean " scope = " request " ></ jsp:useBean >
< ul >
< li > ÊéÃû£º < jsp:getProperty name = " bookBean " property = " title " /></ li >
< li > ÊéºÅ: < jsp:getProperty name = " bookBean " property = " isbn " /></ li >
< li > ×÷Õߣº < jsp:getProperty name = " bookBean " property = " author " />< li >
< li > ³ö°æÉ磺 < jsp:getProperty name = " bookBean " property = " publisher " /></ li >
< li > ¶¨¼Û: < jsp:getProperty name = " bookBean " property = " price " /></ li >
</ ul >
</ body >
</ html >
//
javaBean 程序
package cn.java;
public class BookBean ... {
//ISBN ��
private String isbn;
//����
private String title;
//����
private String author;
//�����
private String publisher;
//�۸�
private int price;
public BookBean()...{
}
public BookBean(String isbn,String title,String autor,String publisher,int price)...{
this.isbn=isbn;
this.title=title;
this.author=author;
this.publisher=publisher;
this.price=price;
}
public String getAuthor() ...{
return author;
}
public void setAuthor(String author) ...{
this.author = author;
}
public String getIsbn() ...{
return isbn;
}
public void setIsbn(String isbn) ...{
this.isbn = isbn;
}
public int getPrice() ...{
return price;
}
public void setPrice(int price) ...{
this.price = price;
}
public String getPublisher() ...{
return publisher;
}
public void setPublisher(String publisher) ...{
this.publisher = publisher;
}
public String getTitle() ...{
return title;
}
public void setTitle(String title) ...{
this.title = title;
}
}
package cn.java;
public class BookBean ... {
//ISBN ��
private String isbn;
//����
private String title;
//����
private String author;
//�����
private String publisher;
//�۸�
private int price;
public BookBean()...{
}
public BookBean(String isbn,String title,String autor,String publisher,int price)...{
this.isbn=isbn;
this.title=title;
this.author=author;
this.publisher=publisher;
this.price=price;
}
public String getAuthor() ...{
return author;
}
public void setAuthor(String author) ...{
this.author = author;
}
public String getIsbn() ...{
return isbn;
}
public void setIsbn(String isbn) ...{
this.isbn = isbn;
}
public int getPrice() ...{
return price;
}
public void setPrice(int price) ...{
this.price = price;
}
public String getPublisher() ...{
return publisher;
}
public void setPublisher(String publisher) ...{
this.publisher = publisher;
}
public String getTitle() ...{
return title;
}
public void setTitle(String title) ...{
this.title = title;
}
}
//
Serlvet 程序
import cn.java.BookBean;
import cn.java.DBUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class QueryBookServlet extends HttpServlet ... {
/** *//**
*
*/
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException,IOException...{
final String SQL ="select * from BookList where isbn=?";
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String address="unknownBook.jsp";
//����javaBean ����
BookBean book=new BookBean();
try...{
//�����ݿ�l��
conn=DBUtil.getConnection();
stmt=conn.prepareStatement(SQL);
//ָ������
stmt.setString(1,request.getParameter("isbn"));
//ִ�� ��ѯ
rs=stmt.executeQuery();
if(rs!=null)...{
rs.next();
book.setIsbn(rs.getString("isbn"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPrice(rs.getInt("price"));
address="displayBook.jsp";
}
}catch(SQLException e)...{
e.printStackTrace();
}
//����
DBUtil.closeResultSet(rs);
DBUtil.closeStatement(stmt);
DBUtil.closeConnection(conn);
request.setAttribute("bookBean",book);
//��ת��jspҳ��
RequestDispatcher dis=request.getRequestDispatcher(address);
dis.forward(request,response);
}
}
import cn.java.BookBean;
import cn.java.DBUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class QueryBookServlet extends HttpServlet ... {
/** *//**
*
*/
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException,IOException...{
final String SQL ="select * from BookList where isbn=?";
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String address="unknownBook.jsp";
//����javaBean ����
BookBean book=new BookBean();
try...{
//�����ݿ�l��
conn=DBUtil.getConnection();
stmt=conn.prepareStatement(SQL);
//ָ������
stmt.setString(1,request.getParameter("isbn"));
//ִ�� ��ѯ
rs=stmt.executeQuery();
if(rs!=null)...{
rs.next();
book.setIsbn(rs.getString("isbn"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPrice(rs.getInt("price"));
address="displayBook.jsp";
}
}catch(SQLException e)...{
e.printStackTrace();
}
//����
DBUtil.closeResultSet(rs);
DBUtil.closeStatement(stmt);
DBUtil.closeConnection(conn);
request.setAttribute("bookBean",book);
//��ת��jspҳ��
RequestDispatcher dis=request.getRequestDispatcher(address);
dis.forward(request,response);
}
}
//
数据库连接
package cn.java;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBUtil ... {
static String serverName="localhost";
static String sDBDriver="com.mysql.jdbc.Driver";
static String url="jdbc:mysql://127.0.0.1/test";
static String dbUser="root";
static String dbPwd="root";
/** *//**
* �õ�һ��Connection
* */
public static Connection getConnection()...{
Connection conn=null;
try ...{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(url, dbUser,dbPwd);
} catch (SQLException ex) ...{
Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/** *//**
* �ر�ָ���Ľ��
* @param rs Ҫ�رյĽ��
*
* */
public static void closeResultSet(ResultSet rs)...{
if(rs!=null)...{
try...{
rs.close();
}catch(SQLException e)...{
e.printStackTrace();
}
}
}
public static void closeStatement(Statement stmt)...{
if(stmt!=null)...{
try ...{
stmt.close();
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeConnection(Connection conn)...{
if(conn!=null)...{
try ...{
conn.close();
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[])...{
System.out.println(getConnection());
}
}
package cn.java;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBUtil ... {
static String serverName="localhost";
static String sDBDriver="com.mysql.jdbc.Driver";
static String url="jdbc:mysql://127.0.0.1/test";
static String dbUser="root";
static String dbPwd="root";
/** *//**
* �õ�һ��Connection
* */
public static Connection getConnection()...{
Connection conn=null;
try ...{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(url, dbUser,dbPwd);
} catch (SQLException ex) ...{
Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/** *//**
* �ر�ָ���Ľ��
* @param rs Ҫ�رյĽ��
*
* */
public static void closeResultSet(ResultSet rs)...{
if(rs!=null)...{
try...{
rs.close();
}catch(SQLException e)...{
e.printStackTrace();
}
}
}
public static void closeStatement(Statement stmt)...{
if(stmt!=null)...{
try ...{
stmt.close();
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeConnection(Connection conn)...{
if(conn!=null)...{
try ...{
conn.close();
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[])...{
System.out.println(getConnection());
}
}
//
运行首页
< html >
< title >
isbn
</ title >
< body >
< form action = " QueryBookServlet " >
< input type = " text " name = " isbn " >
< input type = " submit " name = " submit " value = " submit " >
</ form >
</ body >
</ html >
< html >
< title >
isbn
</ title >
< body >
< form action = " QueryBookServlet " >
< input type = " text " name = " isbn " >
< input type = " submit " name = " submit " value = " submit " >
</ form >
</ body >
</ html >
//
数据库表建立
create table BookList(
isbn varchar( 30 ) primary key,
title varchar( 30 ) not null ,
author varchar( 30 ) not null ,
publisher varchar( 30 ) not null ,
price int ( 4 )
);
create table BookList(
isbn varchar( 30 ) primary key,
title varchar( 30 ) not null ,
author varchar( 30 ) not null ,
publisher varchar( 30 ) not null ,
price int ( 4 )
);