首页 > 代码库 > 项目代码SSH整合
项目代码SSH整合
工具包中代码:package util;import java.sql.*;public class DBUtil { public DBUtil() { } public static Connection getConnection() { String URL = "jdbc:mysql://localhost/bookdb"; String username = "root"; String password = ""; Connection conn = null; try { conn = DriverManager.getConnection(URL, username, password); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(); } return conn; } public static void close(Connection conn, Statement stmt) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void close(Connection conn, Statement stmt, ResultSet rs) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } close(conn, stmt); } static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException(); } }}---------------------------------------------------------package util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil { //JdbcUtil private static SessionFactory factory; static{ //读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); //建立SessionFactory factory = cfg.buildSessionFactory(); } public static Session getSession(){ return factory.openSession(); } public static void closeSession(Session session){ if (session != null) { if (session.isOpen()) { //关闭session session.close(); } } } }
applicationContext-actions.xml:::::<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="bookAction" class="action.BookAction" scope="prototype"> <property name="bookDao" ref="bookDao"/> </bean></beans>----------------------------------------------------------------------------applicationContext-commons.xml:::::<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置那些类那些方法使用事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* dao.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" /> </aop:config></beans>-------------------------------------------------------------applicationContext-daos.xml::::<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置那些类那些方法使用事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* dao.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" /> </aop:config></beans>
在action包中写业务逻辑的代码:package action;import java.util.List;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.opensymphony.xwork2.ModelDriven;import dao.BookDao;import entity.Book;public class BookAction implements ModelDriven<Book>{ private Book book = new Book(); private int[] book_ids; private List<Book> list; //private BookDao bookDao = BookDaoImpl.getInstance(); //private BookDao bookDao = BookDaoHibernateImpl.getInstance(); private BookDao bookDao; // public BookAction(){// super();// BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");// bookDao = (BookDao)factory.getBean("bookDao"); // } public String execute(){ list = bookDao.findAll(); return "list"; } public String add(){ bookDao.add(book); return "listAction"; } public String delete(){ bookDao.delete(book.getId()); return "listAction"; } public String deleteAll(){ for(int id : book_ids){ bookDao.delete(id); } return "listAction"; } public String toUpdate(){ book = bookDao.findById(book.getId()); return "edit"; } public String update(){ bookDao.update(book); return "listAction"; } public Book getModel() { return book; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public List<Book> getList() { return list; } public void setList(List<Book> list) { this.list = list; } public int[] getBook_ids() { return book_ids; } public void setBook_ids(int[] book_ids) { this.book_ids = book_ids; } public BookDao getBookDao() { return bookDao; } public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; }}------------------------------------------------------------------------daopackage dao;import java.util.List;import entity.Book;public interface BookDao { public abstract void add(Book book); public abstract void delete(Integer id); public abstract void update(Book book); public abstract Book findById(Integer id); public abstract List<Book> findAll();}package dao;import java.util.List;import org.hibernate.Session;import util.HibernateUtil;import entity.Book;public class BookDaoHibernateImpl implements BookDao { // private static BookDao bookDao = new BookDaoHibernateImpl();//// private BookDaoHibernateImpl() {// }//// public static BookDao getInstance() {// return bookDao;// } public void add(Book book) { Session session = null; try { session = HibernateUtil.getSession(); session.beginTransaction(); session.save(book); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtil.closeSession(session); } } public void delete(Integer id) { Session session = null; try { session = HibernateUtil.getSession(); session.beginTransaction(); session.delete(session.get(Book.class, id)); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtil.closeSession(session); } } public List<Book> findAll() { List<Book> list = null; Session session = null; try { session = HibernateUtil.getSession(); session.beginTransaction(); list = session.createQuery("from Book").list(); System.out.println("-----Hibernate-----"); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtil.closeSession(session); } return list; } public Book findById(Integer id) { Book book = null; Session session = null; try { session = HibernateUtil.getSession(); session.beginTransaction(); book = (Book)session.get(Book.class, id); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtil.closeSession(session); } return book; } public void update(Book book) { Session session = null; try { session = HibernateUtil.getSession(); session.beginTransaction(); session.update(book); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtil.closeSession(session); } }}package dao;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import entity.Book;public class BookDaoHibernateImpl2 extends HibernateDaoSupport implements BookDao { public void add(Book book) { getHibernateTemplate().save(book); } public void delete(Integer id) { getHibernateTemplate().delete(getHibernateTemplate().load(Book.class, id)); } public List<Book> findAll() { System.out.println("-----Spring-----"); return getHibernateTemplate().find("from Book"); } public Book findById(Integer id) { //return (Book)getHibernateTemplate().load(Book.class, id); return (Book)getHibernateTemplate().get(Book.class, id); } public void update(Book book) { getHibernateTemplate().update(book); }}jdbc de shi xian package dao;import java.sql.*;import java.util.ArrayList;import java.util.List;import util.DBUtil;import entity.Book;public class BookDaoImpl implements BookDao {// private static BookDao bookDao = new BookDaoImpl();//// private BookDaoImpl() {// }//// public static BookDao getInstance() {// return bookDao;// } public void add(Book book) { String sql = "insert into books (name, title, price, yr, description, saleAmount) values(?,?,?,?,?,?)"; Connection conn = DBUtil.getConnection(); PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); int index = 1; pstmt.setString(index++, book.getName()); pstmt.setString(index++, book.getTitle()); pstmt.setFloat(index++, book.getPrice()); pstmt.setDate(index++, book.getYr()); pstmt.setString(index++, book.getDescription()); pstmt.setInt(index++, book.getSaleAmount()); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(); } finally { DBUtil.close(conn, pstmt); } } public void delete(Integer id) { String sql = "delete from books where id = ?"; Connection conn = DBUtil.getConnection(); PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); int index = 1; pstmt.setInt(index++, id); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(); } finally { DBUtil.close(conn, pstmt); } } public void update(Book book) { String sql = "update books set name=?,title=?,price=?,yr=?,description=?,saleamount=? where id = ?"; Connection conn = DBUtil.getConnection(); PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); int index = 1; pstmt.setString(index++, book.getName()); pstmt.setString(index++, book.getTitle()); pstmt.setFloat(index++, book.getPrice()); pstmt.setDate(index++, book.getYr()); pstmt.setString(index++, book.getDescription()); pstmt.setInt(index++, book.getSaleAmount()); pstmt.setInt(index++, book.getId()); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(); } finally { DBUtil.close(conn, pstmt); } } public Book findById(Integer id) { String sql = "select * from books where id= ?"; Connection conn = DBUtil.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; Book book = new Book(); try { pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); rs = pstmt.executeQuery(); if (rs.next()) { int index = 1; book.setId(rs.getInt(index++)); book.setName(rs.getString(index++)); book.setTitle(rs.getString(index++)); book.setPrice(rs.getFloat(index++)); book.setYr(rs.getDate(index++)); book.setDescription(rs.getString(index++)); book.setSaleAmount(rs.getInt(index++)); } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(); } finally { DBUtil.close(conn, pstmt, rs); } return book; } public List<Book> findAll() { String sql = "select * from books"; Connection conn = DBUtil.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; List<Book> list = new ArrayList<Book>(); try { pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { Book book = new Book(); int index = 1; book.setId(rs.getInt(index++)); book.setName(rs.getString(index++)); book.setTitle(rs.getString(index++)); book.setPrice(rs.getFloat(index++)); book.setYr(rs.getDate(index++)); book.setDescription(rs.getString(index++)); book.setSaleAmount(rs.getInt(index++)); list.add(book); } System.out.println("-----JDBC-----"); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(); } finally { DBUtil.close(conn, pstmt, rs); } return list; }}-----------------------------------------------------------------------entitypackage entity;public class Book { private Integer id; private String name; private String title; private Float price; private java.sql.Date yr; private String description; private Integer saleAmount; public Book() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public java.sql.Date getYr() { return yr; } public void setYr(java.sql.Date yr) { this.yr = yr; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Integer getSaleAmount() { return saleAmount; } public void setSaleAmount(Integer saleAmount) { this.saleAmount = saleAmount; }}<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="entity.Book" table="books"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <property name="title"/> <property name="price"/> <property name="yr"/> <property name="description"/> <property name="saleAmount"/> </class></hibernate-mapping>
前台界面的代码css:BODY { BORDER-RIGHT: medium none; BORDER-TOP: medium none; SCROLLBAR-FACE-COLOR: #d6e3f7; FONT-SIZE: 12px; MARGIN: 0px; SCROLLBAR-HIGHLIGHT-COLOR: white; BORDER-LEFT: medium none; SCROLLBAR-SHADOW-COLOR: #336699; SCROLLBAR-ARROW-COLOR: black; BORDER-BOTTOM: medium none; SCROLLBAR-DARKSHADOW-COLOR: white; SCROLLBAR-BASE-COLOR: #d6e3f7}TD { FONT-SIZE: 12px}.PageTitle { FONT-SIZE: 12px; COLOR: white; HEIGHT: 22px; BACKGROUND-COLOR: #336699}.btn { BORDER-RIGHT: medium none; BORDER-TOP: medium none; FONT-SIZE: 12px; BORDER-LEFT: medium none; WIDTH: 60px; CURSOR: hand; PADDING-TOP: 2px; BORDER-BOTTOM: medium none; HEIGHT: 21px; BACKGROUND-COLOR: #888888}.btn80 { BORDER-RIGHT: medium none; BORDER-TOP: medium none; FONT-SIZE: 12px; BACKGROUND-IMAGE: url(../images/btn80.gif); BORDER-LEFT: medium none; WIDTH: 80px; CURSOR: hand; COLOR: black; PADDING-TOP: 2px; BORDER-BOTTOM: medium none; HEIGHT: 21px}.button { BORDER-RIGHT: white 1px solid; BORDER-TOP: white 1px solid; FONT-SIZE: 12px; BORDER-LEFT: white 1px solid; WIDTH: 40px; CURSOR: hand; PADDING-TOP: 2px; BORDER-BOTTOM: white 1px solid; HEIGHT: 20px; BACKGROUND-COLOR: #ebebeb}.xpImg { BORDER-RIGHT: white 0px solid; BORDER-TOP: white 0px solid; DISPLAY: inline; BEHAVIOR: url(../css/img.htc); BORDER-LEFT: white 0px solid; BORDER-BOTTOM: white 0px solid}SELECT { FONT-SIZE: 12px; COLOR: maroon; HEIGHT: 22px}A { COLOR: #000084; TEXT-DECORATION: none}A:hover { COLOR: red}.Scroll { SCROLLBAR-FACE-COLOR: #d6e3f7; SCROLLBAR-HIGHLIGHT-COLOR: white; SCROLLBAR-SHADOW-COLOR: #336699; SCROLLBAR-ARROW-COLOR: black; SCROLLBAR-DARKSHADOW-COLOR: white; SCROLLBAR-BASE-COLOR: #d6e3f7; BACKGROUND-COLOR: white}.TextBox { BORDER-RIGHT: medium none; BORDER-TOP: medium none; FONT-SIZE: 12px; BORDER-LEFT: medium none; COLOR: maroon; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: transparent}.TextBox1 { BORDER-RIGHT: #336699 1px solid; BORDER-TOP: gray 1px solid; FONT-SIZE: 12px; BORDER-LEFT: gray 1px solid; BORDER-BOTTOM: #336699 1px solid; BACKGROUND-COLOR: #ffffee}.label { PADDING-RIGHT: 3px; HEIGHT: 22px; BACKGROUND-COLOR: #eeeef1; TEXT-ALIGN: right}.GridHeader { FONT-WEIGHT: normal; COLOR: black; HEIGHT: 21px; BACKGROUND-COLOR: #e0e0e0}.t1 { HEIGHT: 21px}.t2 { HEIGHT: 21px}.leftTd { BACKGROUND-IMAGE: url(../images/mid_grad.gif); BACKGROUND-REPEAT: repeat-y}.MsgTable { BORDER-RIGHT: black 1px solid; BORDER-TOP: #eeeef1 1px solid; FONT-SIZE: 12px; FILTER: progid:DXImageTransform.Microsoft.Shadow (color=‘gray‘,Direction=135,Strength=3); ; LEFT: expression(document.body.offsetWidth/3); BORDER-LEFT: #eeeef1 1px solid; ; WIDTH: expression(document.body.offsetWidth/3); COLOR: black; BORDER-BOTTOM: black 1px solid; POSITION: absolute; ; TOP: expression(document.body.offsetHeight/3); BACKGROUND-COLOR: #ffffcc}.tbquery { BORDER-RIGHT: black 1px solid; BORDER-TOP: #eeeef1 1px solid; BORDER-LEFT: #eeeef1 1px solid; BORDER-BOTTOM: black 1px solid}.tbinput { BORDER-RIGHT: gray 1px solid; BORDER-TOP: #eeeef1 1px solid; BORDER-LEFT: #eeeef1 1px solid; BORDER-BOTTOM: gray 1px solid}.inputtitle { FONT-WEIGHT: bold; HEIGHT: 22px; BACKGROUND-COLOR: #dddddd; TEXT-ALIGN: left}.xpimg { BORDER-RIGHT: white 1px solid; BORDER-TOP: white 1px solid; BEHAVIOR: url(../css/img.htc); BORDER-LEFT: white 1px solid; CURSOR: hand; BORDER-BOTTOM: white 1px solid}.gridarrow { DISPLAY: inline; FONT-SIZE: 12px; COLOR: gray}.gridtable { BORDER-COLLAPSE: collapse}.tdtitle { COLOR: #000084; TEXT-DECORATION: none}.Mutipleselect { BORDER-RIGHT: #336699 1px solid; BORDER-TOP: gray 1px solid; FONT-SIZE: 12px; BORDER-LEFT: gray 1px solid; BORDER-BOTTOM: #336699 1px solid; BACKGROUND-COLOR: #ffffee}BUTTON { CURSOR: hand}----------------------------------------------------------------------add.jsp<%@ page pageEncoding="UTF-8"%><HTML> <HEAD> <title>图书管理系统</title><LINK href="http://www.mamicode.com/css/Default.css" type=text/css rel=stylesheet></HEAD><body > <TABLE width="100%" align=center> <TBODY> <form name="Form1" method="post" action=""> <TABLE width="95%" align="center" cellspacing="0" border="0" cellpadding="0" style="TABLE-LAYOUT:fixed" ID=Table3 > <TR> <TD style="BACKGROUND-POSITION:left top; BACKGROUND-IMAGE:url(images/lc.gif); WIDTH:6px; BORDER-BOTTOM:#b1c3ed 1px solid; HEIGHT:22px"> </TD> <TD style="BACKGROUND-POSITION:left top; BACKGROUND-IMAGE:url(images/mid_grad.gif); BORDER-BOTTOM:#b1c3ed 1px solid">添加</TD> <TD style="BACKGROUND-POSITION:left top; BACKGROUND-IMAGE:url(images/mid_grad.gif); BORDER-BOTTOM:#b1c3ed 1px solid" align="right"><IMG src=http://www.mamicode.com/‘images/handle.gif‘>"BACKGROUND-POSITION:left top; BACKGROUND-IMAGE:url(images/rc.gif); WIDTH:6px; BORDER-BOTTOM:#b1c3ed 1px solid; HEIGHT:22px"> </TD> </TR> <TR> <TD height=280 colspan="4" style="BORDER-RIGHT:#b1c3ed 1px solid; BORDER-TOP:#b1c3ed 1px solid; BORDER-LEFT:#b1c3ed 1px solid; BORDER-BOTTOM:#b1c3ed 1px solid"> <table cellSpacing=2 cellPadding=3 width="100%" height=100% bgcolor="#eeeeee" border=0 style1="border:solid 1px gray"> <tr> <td align="right"> 作者</td> <td><input type="text" name="name" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 书名</td> <td><input type="text" name="title" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 价格</td> <td><input type="text" name="price" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 日期</td> <td><input type="text" name="yr" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 简介</td> <td><input type="text" name="description" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 数量</td> <td><input type="text" name="saleAmount" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td COLSPAN="4" ALIGN="center"> <input type=button class="btn" onclick="saveForm()" value="http://www.mamicode.com/添加"> <input type=button class="btn" value="http://www.mamicode.com/取消" onclick="javascript:window.close();"> </td> </tr> <tr> <td colspan="2"> </td> </tr> </TABLE> </TD> </TR> </TABLE> </form> </TBODY> </TABLE><SCRIPT LANGUAGE="JavaScript">function saveForm() { var form1 = document.Form1; var rtStr = new Array; rtStr[0] = form1.name.value; rtStr[1] = form1.title.value; rtStr[2] = form1.price.value; rtStr[3] = form1.yr.value; rtStr[4] = form1.description.value; rtStr[5] = form1.saleAmount.value; window.returnValue = rtStr; window.close();}</SCRIPT></body></HTML>-------------------------------------------------------------------edit.jsp<%@ page pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><HTML> <HEAD> <title>图书管理系统</title><LINK href="http://www.mamicode.com/css/Default.css" type=text/css rel=stylesheet></HEAD><body > <TABLE width="100%" align=center> <TBODY> <form name="Form1" method="post" action=""> <TABLE width="95%" align="center" cellspacing="0" border="0" cellpadding="0" style="TABLE-LAYOUT:fixed" ID=Table3 > <TR> <TD style="BACKGROUND-POSITION:left top; BACKGROUND-IMAGE:url(images/lc.gif); WIDTH:6px; BORDER-BOTTOM:#b1c3ed 1px solid; HEIGHT:22px"> </TD> <TD style="BACKGROUND-POSITION:left top; BACKGROUND-IMAGE:url(images/mid_grad.gif); BORDER-BOTTOM:#b1c3ed 1px solid">编辑:</TD> <TD style="BACKGROUND-POSITION:left top; BACKGROUND-IMAGE:url(images/mid_grad.gif); BORDER-BOTTOM:#b1c3ed 1px solid" align="right"><IMG src=http://www.mamicode.com/‘images/handle.gif‘>"BACKGROUND-POSITION:left top; BACKGROUND-IMAGE:url(images/rc.gif); WIDTH:6px; BORDER-BOTTOM:#b1c3ed 1px solid; HEIGHT:22px"> </TD> </TR> <TR> <TD height=280 colspan="4" style="BORDER-RIGHT:#b1c3ed 1px solid; BORDER-TOP:#b1c3ed 1px solid; BORDER-LEFT:#b1c3ed 1px solid; BORDER-BOTTOM:#b1c3ed 1px solid"> <table cellSpacing=2 cellPadding=3 width="100%" height=100% bgcolor="#eeeeee" border=0 style1="border:solid 1px gray"> <tr> <td align="right"> 作者:</td> <td><input type="text" name="name" value="http://www.mamicode.com/${book.name}" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 书名:</td> <td><input type="text" name="title" value="http://www.mamicode.com/${book.title}" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 价格:</td> <td><input type="text" name="price" value="http://www.mamicode.com/${book.price}" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 日期:</td> <td><input type="text" name="yr" value="http://www.mamicode.com/${book.yr}" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 简介:</td> <td><input type="text" name="description" value="http://www.mamicode.com/${book.description}" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td align="right"> 数量:</td> <td><input type="text" name="saleAmount" value="http://www.mamicode.com/${book.saleAmount}" class="textbox" maxlength="20" size="30"><font color="#FF0000">*</font></td> </tr> <tr> <td COLSPAN="4" ALIGN="center"> <input type=button class="btn" onclick="saveForm()" value="http://www.mamicode.com/保存"> <input type=button class="btn" value="http://www.mamicode.com/取消" onclick="javascript:window.close();"> </td> </tr> <tr> <td colspan="2"> </td> </tr> </TABLE> </TD> </TR> </TABLE> </form> </TBODY> </TABLE><SCRIPT LANGUAGE="JavaScript">function saveForm() { var form1 = document.Form1; var rtStr = new Array; rtStr[0] = form1.name.value; rtStr[1] = form1.title.value; rtStr[2] = form1.price.value; rtStr[3] = form1.yr.value; rtStr[4] = form1.description.value; rtStr[5] = form1.saleAmount.value; window.returnValue = rtStr; window.close();}</SCRIPT></body></HTML>------------------------------------------------------------------list.jsp<%@ page pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><HTML><HEAD><TITLE>图书管理系统</TITLE><LINK href="http://www.mamicode.com/css/Default.css" type="text/css" rel="stylesheet"><SCRIPT language="javascript"><!--function GoAdd() { var strFeatures = "dialogWidth: 330px; dialogHeight: 380px; center: yes; scroll:yes;help: no; status: no; edge: raised;"; var result = new Array; result = window.showModalDialog("add.jsp", 0, strFeatures); if(result != null){ addForm.name.value = result[0]; addForm.title.value = result[1]; addForm.price.value = result[2]; addForm.yr.value = result[3]; addForm.description.value = result[4]; addForm.saleAmount.value = result[5]; addForm.submit(); }}function GoEdit(id) { var strFeatures = "dialogWidth: 350px; dialogHeight: 360px; center: yes; scroll:no;help: no; status: no; edge: raised;"; var result = new Array; result = window.showModalDialog("toUpdate_book?id="+id, 0, strFeatures); if(result != null){ addForm.name.value = result[0]; addForm.title.value = result[1]; addForm.price.value = result[2]; addForm.yr.value = result[3]; addForm.description.value = result[4]; addForm.saleAmount.value = result[5]; addForm.action = "update_book?id="+id; addForm.submit(); }}function GoDelete(id) { if(confirm("真的要删除吗?")) { Form1.action = "delete_book?id="+id; Form1.submit(); }}function CheckSelOnle() { var objs = document.getElementsByTagName("INPUT"); var bSel = false; for(var i=0;i<objs.length;i++) { if(objs[i].type =="checkbox" && objs[i].checked && objs[i].id!="chkAll") { bSel = true; break; } } if(!bSel) { alert("请至少选择一条记录!"); return false ; } if( confirm("确定要删除选中的内容吗?")) { Form1.action = "deleteAll_book"; Form1.submit(); } else { return false; }}function CheckAll() { var objs = document.getElementsByTagName("INPUT"); for(var i=0;i<objs.length;i++) { if(objs[i].type.toLowerCase() =="checkbox" ) objs[i].checked = event.srcElement.checked; } }//--></SCRIPT></HEAD><BODY ><FORM id="Form1" name="Form1" method=post><TABLE width="100%" align=center> <TBODY> <TR> <TD class=PageTitle> 图书管理 </TD></TR></TBODY></TABLE><TABLE border=0 width=100%> <TBODY> <TR> <td align="left" style="PADDING-LEFT: 20px"></td> <TD style="PADDING-LEFT: 20px" align=right> <INPUT class=button style=‘HEIGHT: 18px; width:60px; cursor:hand; BACKGROUND-COLOR: #BEBEBE‘ onclick=javascript:GoAdd(); type="button" value="http://www.mamicode.com/增加"> <INPUT class=button style=‘HEIGHT: 18px; width:60px; cursor:hand; BACKGROUND-COLOR: #BEBEBE‘ onclick=javascript:CheckSelOnle(); type="button" value="http://www.mamicode.com/批量删除"> </TD> </TR> </TBODY></TABLE><TABLE id=DataGrid1 style="BORDER-LEFT-COLOR: gray; BORDER-BOTTOM-COLOR: gray; WIDTH: 100%; BORDER-TOP-COLOR: gray; BORDER-COLLAPSE: collapse; BORDER-RIGHT-COLOR: gray" borderColor=gray cellSpacing=1 rules=all align=center border=1> <TBODY> <TR class=gridheader> <TD noWrap width="5%" align="center"><INPUT id=chkAll onclick=javascript:CheckAll(); type=checkbox></TD> <TD noWrap width="5%"> 书号</TD> <TD noWrap width="10%"> 作者</TD> <TD noWrap width="20%"> 书名</TD> <TD noWrap width="5%"> 价格</TD> <TD noWrap width="20%"> 日期</TD> <TD noWrap width="20%"> 简介</TD> <TD noWrap width="5%"> 数量</TD> <TD noWrap width="10%"> 操作</TD> </TR> <c:forEach var="book" items="${list}"> <TR class=t1> <TD align="center"><INPUT type="checkbox" name="book_ids" value="http://www.mamicode.com/${book.id}"></TD> <TD> ${book.id}</TD> <TD> ${book.name}</TD> <TD> ${book.title}</TD> <TD> ${book.price}</TD> <TD> ${book.yr}</TD> <TD> ${book.description}</TD> <TD> ${book.saleAmount}</TD> <TD> <IMG title=修改 style="CURSOR: hand" onclick="javascript:GoEdit(${book.id})" src="http://www.mamicode.com/images/edit.gif"> <IMG title=删除 style="CURSOR: hand" onclick="javascript:GoDelete(${book.id})" src="http://www.mamicode.com/images/delete.gif"> </TD> </TR> </c:forEach> <TR class="gridheader"> <TD colSpan="12"> </TD> </TR> </TBODY> </TABLE></FORM><FORM NAME="addForm" method=post action="add_book"> <INPUT id="name" type=hidden name="name"> <INPUT id="title" type=hidden name="title"> <INPUT id="price" type=hidden name="price"> <INPUT id="yr" type=hidden name="yr"> <INPUT id="description" type=hidden name="description"> <INPUT id="saleAmount" type=hidden name="saleAmount"></FORM></BODY></HTML>-----------------------------------------------------------------------index.jsp<html> <head> <title>books</title> </head> <body> <% response.sendRedirect("list_book"); %> </body></html>
创建数据库的代码:
CREATE DATABASE BookDB;
USE BookDB;
CREATE TABLE books
(id int not null primary key auto_increment,
name VARCHAR(24),
title VARCHAR(96),
price FLOAT,
yr date,
description VARCHAR(30),
saleAmount INT
);
INSERT INTO books (name, title, price, yr, description, saleAmount) VALUES(‘王芳‘,
‘Java编程指南‘, 33.75, ‘1999-01-01‘, ‘让读者轻轻松松掌握Java语言‘, 1000);
INSERT INTO books (name, title, price, yr, description, saleAmount) VALUES(‘张丙‘,
‘Weblogic技术参考‘, 45.99, ‘1999-01-01‘, ‘真的不错耶‘, 2000);
INSERT INTO books (name, title, price, yr, description, saleAmount)VALUES(‘孙艳‘,
‘Oracle数据库教程‘, 40, ‘1999-01-01‘, ‘关于Oracle的最畅销的技术书‘, 2000);
INSERT INTO books (name, title, price, yr, description, saleAmount) VALUES(‘大卫‘,
‘从Oak到Java: 语言的革命‘, 20.75, ‘1999-01-01‘, ‘很值得一看‘, 2000);
INSERT INTO books (name, title, price, yr, description, saleAmount)VALUES(‘阿明‘,
‘Apache从入门到精通‘, 50.75, ‘1999-01-01‘, ‘权威的Apache技术资料‘, 2000);
INSERT INTO books (name, title, price, yr, description, saleAmount) VALUES(‘洪军‘,
‘Java与数据算法 ‘, 54.75, ‘1999-01-01‘, ‘权威的Java技术资料‘, 2000);
项目代码SSH整合