图书管理系统(增,改,删,查)功能的实现

显示功能实现(index.jsp)

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gbk"%>

<%@ page contentType="text/html" %>

 
    My JSP 'index.jsp' starting page
 
 
   
添加图书信息


   


   
   
   
   
   
   
 <%
 try{
  Class.forName("com.mysql.jdbc.Driver"); //数据库驱动加载
  //取得数据库连接
  Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");
  //实例化createStatement对象
  Statement stmt=con.createStatement();
  String s="select * from book";
  //执行查询操作
  ResultSet rs=stmt.executeQuery(s);
  while(rs.next()){
  int id=rs.getInt(1);
  out.println("");
  //String bookname=rs.getString(2);
  //String author=rs.getString(3);
  //Float price=rs.getFloat(4);
}
rs.close();
stmt.close();
con.close();


 %>  
   
书名 作者 价格 管理
"+rs.getString(2)+""+rs.getString(3)+""
  +rs.getString(4)+"
修改 删除

<%
}catch(Exception e){
System.out.println(e);
}
%>
 


添加部分功能实现(edit.jsp)

<%@ page language="java" import="java.util.*"  import="java.sql.*" pageEncoding="gbk"%>
<%@ page contentType="text/html" %>

   
    My JSP 'edit.jsp' starting page
   
 
 <% 
 try{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");
Statement stmt=con.createStatement();
String id=request.getParameter("id");
ResultSet rs=stmt.executeQuery("select * from book where id="+id); 
rs.next(); 
 %>
 


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
修改图书信息
书名
作者:
价格:

 
 
 
 

 

<%
rs.close();
con.close();
}catch(Exception e)
{
System.out.println(e);
}
 %>
 



添加功能验证代码实现(add.jsp)

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gbk"%>
<%@ page contentType="text/html" %>

     
    My JSP 'add.jsp' starting page
   
 
    <%
    request.setCharacterEncoding("gbk");
     %>
     


     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
添加图书信息
书名:
作者:
价格:

     
     
     

     

 <%
  String submit=request.getParameter("submit");
  if(!"".equals(submit)&&submit!=null)
  {
  String bookname=request.getParameter("bookname");
  String author=request.getParameter("author");
  String price=request.getParameter("price");
  try{
  Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");
Statement stmt=con.createStatement();
String sql="insert into book(bookname,author,price)values('"+bookname+"','"+author+"',"+price+")";
int i=stmt.executeUpdate(sql);
if(i==1)
{
out.println("");
response.setHeader("refresh","1;url=index.jsp");
}
else{
out.println("");
response.setHeader("refresh","1;url=add.jsp");
}
stmt.close();
con.close();
}catch(Exception e){
System.out.println(e);
}
  %>
 <%

  %>
 


删除功能实现(del.jsp)

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gbk"%>
<%@ page contentType="text/html" %>

     
    My JSP 'del.jsp' starting page
 
 
<% 
request.setCharacterEncoding("gbk");
Class.forName("com.mysql.jdbc.Driver");
  Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");
  Statement stmt=con.createStatement();
  String id=request.getParameter("id");
  int i=stmt.executeUpdate("delete from book where id="+id);
  if(i==1)
  {
  out.println("");
  response.setHeader("refresh","1;url=index.jsp");
  }else{
  out.println("");
  response.setHeader("refresh","1;url=index.jsp");
  }
  stmt.close();
  con.close();
%>
 


修改功能代码实现(update.jsp)

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gbk"%>
<%@ page contentType="text/html" %>

 
    My JSP 'update.jsp' starting page
 
 
<%
request.setCharacterEncoding("gbk");
String bookname=request.getParameter("bookname");
String author=request.getParameter("author");
String price=request.getParameter("price");
String id=request.getParameter("id");
Class.forName("com.mysql.jdbc.Driver");
  Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");
  Statement stmt=con.createStatement();
 
  String sql="update book set bookname='"+bookname+"',author='"+author+"',price="+price+"where id="+id;
  int i=stmt.executeUpdate(sql);
  if(i==1)
  {
  out.println("");
  response.setHeader("refresh","1;url=index.jsp");
  }
  stmt.close();
  con.close();
 %>
 


数据库实现(book.sql)

-- ----------------------------
创建表book
-- ----------------------------
CREATE TABLE `book` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `bookname` varchar(20) DEFAULT NULL,
  `author` varchar(20) DEFAULT NULL,
  `price` float(7,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
)


-- ----------------------------
插入数据
-- ----------------------------
INSERT INTO `book` VALUES ('2', 'web基础教程', '姜忠', '90.00');
INSERT INTO `book` VALUES ('3', '数据库应用技术', '城东', '90.00');
INSERT INTO `book` VALUES ('4', 'android开发实战经典', '李兴华', '88.00');
INSERT INTO `book` VALUES ('6', 'javaweb开发实战经典', '李兴华', '60.00');
INSERT INTO `book` VALUES ('12', '数据库系统简明教程', '王珊', '27.00');
INSERT INTO `book` VALUES ('13', 'java开发实战经典', '李兴华', '65.00');
INSERT INTO `book` VALUES ('14', 'oracle开发实战经典', '李兴华', '72.00');

你可能感兴趣的:(JavaWeb开发)