BookDAOJdbc.java:
package cn.hncu.stud.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; import cn.hncu.domain.Book; import cn.hncu.pubs.ConnsUtil; public class BookDAOJdbc implements IBookDAO { @Override public void save(List<Book> books) throws InterruptedException, SQLException { Connection con = ConnsUtil.getConnection(); String sql = "insert into book(name,price,studid) values(?,?,?)"; PreparedStatement pst = con.prepareStatement(sql); for(Book book:books){ pst.setString(1, book.getNamem()); pst.setDouble(2, book.getPrice()); //stud中设置了uuid这里才能设置成功 pst.setString(3, book.getStud().getId()); pst.addBatch(); } pst.executeBatch(); } }
StudDAOJdbc.java
package cn.hncu.stud.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import cn.hncu.domain.Stud; import cn.hncu.pubs.ConnsUtil; public class StudDAOJdbc implements StudDAO { @Override public List<Map<String, String>> query() { List<Map<String, String>> list = new ArrayList<Map<String,String>>(); Connection con = null; try { con = ConnsUtil.getConnection(); String sql="select * from stud"; Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql); while(rs.next()){ Map<String, String> map = new HashMap<String, String>(); map.put("id", rs.getString("id")); map.put("name", rs.getString("name")); //一个map就是一条记录(数据行) list.add(map); } } catch (InterruptedException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(con!=null){ try { con.close(); } catch (SQLException e) { throw new RuntimeException("数据库连接关闭失败", e); } } } return list; } @Override public void save(Stud stud) throws InterruptedException, SQLException { Connection con = ConnsUtil.getConnection(); String sql = "insert into stud values(?,?)"; PreparedStatement pst = con.prepareStatement(sql); String uuid = UUID.randomUUID().toString().replaceAll("-",""); pst.setString(1, uuid); pst.setString(2, stud.getName()); pst.executeUpdate(); //在这里为book补外键字段的值 stud.setId(uuid); //这一句执行在前,这样在BookDAOJdbc中会调用book.getStud().getId()就可以拿到该id值了 } }
IStudService.java:
package cn.hncu.stud.service; import java.sql.SQLException; import java.util.List; import java.util.Map; import cn.hncu.domain.Stud; import cn.hncu.pubs.tx.Transaction; public interface IStudService { public abstract List< Map<String, String> > query(); @Transaction//注意,注解必须加在接口上,加在实现类上是无效的!,因为我们的动态代理是面向接口的 public abstract void save(Stud stud) throws InterruptedException, SQLException; }
StudService.java:
package cn.hncu.stud.service; import java.sql.SQLException; import java.util.List; import java.util.Map; import cn.hncu.domain.Stud; import cn.hncu.stud.dao.BookDAOJdbc; import cn.hncu.stud.dao.IBookDAO; import cn.hncu.stud.dao.StudDAO; import cn.hncu.stud.dao.StudDAOJdbc; public class StudService implements IStudService { //注入 private StudDAO studDao = new StudDAOJdbc(); private IBookDAO bookDao = new BookDAOJdbc(); @Override public List<Map<String, String>> query() { return studDao.query(); } //※利用动态代理在背后帮忙实现事务功能,注意:该方法内部的异常必须抛出来给动态代理捕捉处理 @Override public void save(Stud stud) throws InterruptedException, SQLException { studDao.save(stud);//在这个内部为stud对象补id--先执行 bookDao.save(stud.getBooks());//通过book拿到stud对象,进而拿到studid,完成外键字段的赋值 } }
jdbc.properties:
##MySQL driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8 username=root password=1234 ##Oracle #driver=oracle.jdbc.driver.OracleDriver #url=jdbc:oracle:thin:@localhost:1521:orcl #username=scott #password=tiger
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>mvc示例演示</title> </head> <body> <br/> <a href='<c:url value="/StudServlet?cmd=query"></c:url>'>学生信息查询</a> <hr/> <form action="<c:url value='/StudServlet?cmd=save'/>" method="post"> 姓名:<input type="text" name="name" /><br/> <fieldset style="width: 200px"> <legend>图书1</legend> 书名:<input type="text" name="bookname" /><br/><br/> 价格:<input type="text" name="price" /> </fieldset> <fieldset style="width: 200px"> <legend>图书2</legend> 书名:<input type="text" name="bookname" /><br/><br/> 价格:<input type="text" name="price" /> </fieldset><br/> <input type="submit" value="保存"> </form> <br/> <a href="<c:url value='/StudServlet'/>">默认请求</a> </body> </html>
show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生信息查询</title> <style type="text/css"> table,tr,td,th{ border: 1px; border-style: solid; margin: auto; } </style> </head> <body style="text-align: center;"> <h2>学生信息</h2> <table> <tr> <th>ID</th> <th>姓名</th></tr> <c:forEach items="${studs}" var="stud"> <tr> <td>${stud.id }</td> <td>${stud.name }</td> </tr> </c:forEach> </table> </body> </html>
web.xml
<servlet> <servlet-name>StudServlet</servlet-name> <servlet-class>cn.hncu.stud.StudServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>StudServlet</servlet-name> <url-pattern>/StudServlet</url-pattern> </servlet-mapping>
嗯,我就不做过多的解释啦,这个只是自己对以前知识点的一个小总结吧。
转载请附上原文博客链接: