jsp.图书馆

bm.java

package bean;



public class bm {
	private int b_id;
	private String b_name;
	private String b_author;
	private String b_time;
	private int b_type;
	public bm() {
		super();
	}
	public bm(int b_id,String b_name, String b_author, String b_time,
			int b_type) {
		super();
		this.b_id=b_id;
		this.b_name = b_name;
		this.b_author = b_author;
		this.b_time = b_time;
		this.b_type = b_type;
	}
	public int getB_id() {
		return b_id;
	}
	public void setB_id(int b_id) {
		this.b_id = b_id;
	}
	public String getB_name() {
		return b_name;
	}
	public void setB_name(String b_name) {
		this.b_name = b_name;
	}
	public String getB_author() {
		return b_author;
	}
	public void setB_author(String b_author) {
		this.b_author = b_author;
	}
	public String getB_time() {
		return b_time;
	}
	public void setB_time(String b_time) {
		this.b_time = b_time;
	}
	public int getB_type() {
		return b_type;
	}
	public void setB_type(int b_type) {
		this.b_type = b_type;
	}
	
}

 BaseDao.java

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {	
	//获取连接
	protected Connection getConnection(){
		Connection conn=null;
			try {
				Class.forName("com.mysql.jdbc.Driver");
				// 2.建立连接
				conn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/mysql", "root", "root");
			} catch (Exception e) {
				e.printStackTrace();
			} 
			return conn;
	}	 
	
	
	//关闭连接
	protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){		
	try {
		if(rs != null)
			rs.close();
		if(ps != null)
			ps.close();
		if(con != null)
			con.close();
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

 bmDao.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bean.bm;

public class bmDao extends BaseDao{

	public  List<bm> selectall() {
		ArrayList<bm> list  = new ArrayList<bm>();
		Connection conn = null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		try {
			 conn = getConnection();
			String sql = "select * from  bookmanage";
			 ps = conn.prepareStatement(sql);
			 rs = ps.executeQuery();
			while(rs.next()){
				bm b = new bm();
				b.setB_id(rs.getInt(1));
				b.setB_name(rs.getString(2));
				b.setB_author(rs.getString(3));
				b.setB_time(rs.getString(4));
				b.setB_type(rs.getInt(5));
				
				list.add(b);
			}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeAll(conn, ps, rs);
		}
		return list;
	}
	
	public int add(bm b){
		Connection conn=getConnection();
		String sql="add into bm (b_id,b_name,b_author,b_time,b_type)values(?,?,?,?)";
		PreparedStatement ps=null;
		int i=0;
		try {
			 ps=conn.prepareStatement(sql);
			 ps.setInt(1,b.getB_id());
			ps.setString(2,b.getB_name());
			ps.setString(3, b.getB_author());
			ps.setString(4, b.getB_time());
			ps.setInt(5, b.getB_type());
			i=ps.executeUpdate();//返回受影响的行数
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	finally{
			closeAll(conn, ps, null);
		}
		return i;
	}
	
	public int delete(int b_id){
		Connection conn=getConnection();
		String sql="delete from bm where b_id = ?";
		PreparedStatement ps=null;
		int i=0;
		try {
			 ps=conn.prepareStatement(sql);	
			ps.setInt(1, b_id);
			i=ps.executeUpdate();//返回受影响的行数
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	finally{
			closeAll(conn, ps, null);
		}
		return i;
	}
}
index.jsp
<%@page import="bean.bm"%>
<%@page import="dao.bmDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>

  
  <body>

  <table cellspacing="0" border="1px" width="549" height="66" style="margin: 0pt auto;">
  	<tr>
		<th>id</th>
  		<th>图书名称</th>
  		<th>图书作者</th>	
  		<th>购买时间</th>	
  		<th>图书分类</th>	
  		<th>操作</th>		
  	</tr>



  	 <%
  	 	bmDao bd = new bmDao();
  		List<bm> list = bd.selectall(); 
  		%>
	<%for(bm b:list){ %>
	<tr>
	<td><%b.getB_id(); %></td>
	<td><%b.getB_name(); %></td>
	<td><%b.getB_author() ;%></td>
	<td><%b.getB_time() ;%></td>
	<td><%b.getB_type(); %></td>
	<td><a href="delete.jsp?b_id=<%=b.getB_id()%>">删除</a></td>
	</tr>
  	<%} %>
  </table>
   <h2><a href="add.jsp">添加书籍</a></h2>
  </body>
</html>

 add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>

  
  <body>
  <form action="doadd.jsp">
   <h2>新增书籍信息</h2>
   图书名称:<input type="text" name = "b_name">
   </br>
   图书作者:<input type="text" name = "b_author">
   </br>
   购买日期<input type="text" name = "b_time">yyyy-Mm-dd格式
      </br>
     图书类别:<select name="b_type">
     	
     	 <option value="0">请选择所属分类</option>
     	 <option value="1">抒情/传记</option>
     	 <option value="2">悬疑/惊悚</option>
     	 <option value="3">名著</option>
     </select>
     </br>
     <input type="submit" value="增加书籍">
     </form>
  </body>
</html>

 doadd.jsp

<%@page import="dao.bmDao"%>
<%@page import="bean.bm"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>

  
  <body>
<%
	
	int b_type = Integer.parseInt(request.getParameter("b_type"));
	String b_name = request.getParameter("b_name");
	String b_author= request.getParameter("b_author");
	String b_time = request.getParameter("b_time");
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	Date time = sdf.parse(b_time);
	bm b = new bm();
	b.setB_type(b_type);
	b.setB_author(b_author);
	b.setB_name(b_name);
	b.setB_time(b_time);
	bmDao bd = new bmDao();
	bd.add(b); 
	  	response.sendRedirect("add.jsp");
	
 %>
  </body>
</html>

 delete.jsp

<%@page import="dao.bmDao"%>
<%@page import="bean.bm"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP ‘delete.jsp‘ starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <div align="center"><%
  	
  	int b_id = Integer.parseInt(request.getParameter("b_id"));
  	bmDao bd = new bmDao();
  	bd.delete(b_id); 
  	response.sendRedirect("index.jsp");
  	
   %> <br> 
  </div></body>
</html>

 jsp.图书馆

 

 

 

 

jsp.图书馆

上一篇:Apache优化之---网页缓存


下一篇:图片上传的那些事儿