web作业,,来记录一下,,代码量没有多少,挺好写的。
增加了一个下架图书功能,,当flag为1时,,显示在当前页面中,flag为0时,,,不显示,,点击下架图书时显示。
package beans;
import java.sql.*;
public class bookInfo {
private String driverStr="com.mysql.jdbc.Driver";
private String connStr="jdbc:mysql://localhost:3306/book?characterEncoding=gb2312";//数据库名
private String dbusername="root";
private String dbpassword="sa";//密码和数据库一致
private Connection conn=null;
private Statement stmt=null;
public bookInfo()
{
try{
Class.forName(driverStr);
conn=DriverManager.getConnection(connStr,dbusername, dbpassword);
stmt=conn.createStatement();
}
catch(Exception ex){System.out.println("无法同数据库建立连接!");}
}
public int executeUpdate(String s)
{
int result=0;
try{result=stmt.executeUpdate(s);}
catch(Exception ex){System.out.println("执行更新错误!");}
return result;
}
public ResultSet executeQuery(String s)
{
ResultSet rs=null;
try{rs=stmt.executeQuery(s);}
catch(Exception ex){System.out.println("执行查询错误!");}
return rs;
}
public void close()
{
try{
stmt.close();
conn.close();
}
catch(Exception e){}
}
}
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="UTF-8" import="java.sql.*"%>
图书管理界面
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="UTF-8" import="java.sql.*"%>
增加图书
<%
request.setCharacterEncoding("gb2312");
String submit=request.getParameter("submit");
if(submit!=null&&!submit.equals(""))
{
String bookname=request.getParameter("bookname");
String author=request.getParameter("author");
String price=request.getParameter("price");
String sql="insert into book_info(bookname,author,price) values('"+bookname+"','"+author+"',"+price+")";
int i=db.executeUpdate(sql);
if(i!=-1)
{
out.println("");
response.setHeader("refresh","1;url=index.jsp");
}
else{
out.println("");
response.setHeader("refresh","1;url=add.jsp");
}
db.close();
}
%>
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="UTF-8" import="java.sql.*" %>
删除图书
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
String sql="delete from book_info where id="+id;
int i=db.executeUpdate(sql);
if(i==1)
{out.println("");
response.setHeader("refresh","1;url=index.jsp");
}
else{
out.println("");
response.setHeader("refresh","1;url=del.jsp");
}
db.close();
%>
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="UTF-8" import="java.sql.*"%>
修改图书
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
ResultSet rs=db.executeQuery("select * from book_info where id="+id);
rs.next();
%>
<%
db.close();
%>
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.sql.*" errorPage="error.jsp"%>
图书修改完成
<%
request.setCharacterEncoding("gb2312");
String bookname=request.getParameter("bookname");
String author=request.getParameter("author");
String price=request.getParameter("price");
String id=request.getParameter("id");
String sql="update book_info set bookname='"+bookname+"',author='"+author+"',price="+price+" where id="+id;
int i=db.executeUpdate(sql);
if(i==1)
{out.println("");
response.setHeader("refresh","1;url=index.jsp");
return;
}
db.close();
%>
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="UTF-8" import="java.sql.*" %>
下架图书
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
String sql="update book_info set flag=0 where id="+id;
int i=db.executeUpdate(sql);
if(i==1)
{out.println("");
response.setHeader("refresh","1;url=index.jsp");
}
else{
out.println("");
response.setHeader("refresh","1;url=recycle.jsp");
}
db.close();
%>
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="UTF-8" import="java.sql.*"%>
图书管理界面
下架的图书
书名 作者 价格 管理
<%
String s="select * from book_info";
ResultSet rs=db.executeQuery(s);
while(rs.next())
{
int flag=rs.getInt(5);
if(flag==0)
{
int id=rs.getInt(1);
out.println(" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" 上架 ");
}
}
rs.close();
db.close();
%>
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="UTF-8" import="java.sql.*" %>
下架图书
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
String sql="update book_info set flag=1 where id="+id;
int i=db.executeUpdate(sql);
if(i==1)
{out.println("");
response.setHeader("refresh","1;url=index.jsp");
}
else{
out.println("");
response.setHeader("refresh","1;url=recycle.jsp");
}
db.close();
%>