JAVA DAO模式實現
阿新 • • 發佈:2019-01-01
package com.mike.daoImpl;
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 com.mike.dao.BookDAO;
import com.mike.util.ConnectionUtil;
import com.mike.vo.Book;
public class BookDaoImpl implements BookDAO {
public List listBook() {
// TODO Auto-generated method stub
ConnectionUtil conU = new ConnectionUtil();
Connection con = null;
List list = new ArrayList();
try {
con = conU.getConnection();
String QuerySQL = "select * from book";
PreparedStatement ps = con.prepareStatement(QuerySQL);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setBookID(rs.getInt(1));
book.setBookName(rs.getString(2));
book.setBookWriter(rs.getString(3));
list.add(book);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public Boolean save(Book book) {
// TODO Auto-generated method stub
ConnectionUtil conU = new ConnectionUtil();
Connection con = null;
Boolean flag = false;
try {
con = conU.getConnection();
String InsertSQL = "insert book values(" + book.getBookID() + ",'"
+ book.getBookName() + "','" + book.getBookWriter() + "')";
PreparedStatement ps = con.prepareStatement(InsertSQL);
flag = ps.execute();
return flag;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
}
6servlet控制層 獲得使用者請求,將表儲存在資料庫中,實現頁面跳轉;
package com.mike.controlServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mike.daoImpl.BookDaoImpl;
import com.mike.vo.Book;
public class BookServlet extends HttpServlet {
public BookServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String bookName = request.getParameter("bookName");
String bookWriter = request.getParameter("bookWriter");
BookDaoImpl bookDao = new BookDaoImpl();
Book book = new Book();
Integer bookID = 13574;// only for easy
book.setBookID(bookID);
book.setBookName(bookName);
book.setBookWriter(bookWriter);
bookDao.save(book);
request.getRequestDispatcher("/Book.jsp").forward(request, response);
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 com.mike.dao.BookDAO;
import com.mike.util.ConnectionUtil;
import com.mike.vo.Book;
public class BookDaoImpl implements BookDAO {
public List listBook() {
// TODO Auto-generated method stub
ConnectionUtil conU = new ConnectionUtil();
Connection con = null;
List list = new ArrayList();
try {
con = conU.getConnection();
String QuerySQL = "select * from book";
PreparedStatement ps = con.prepareStatement(QuerySQL);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setBookID(rs.getInt(1));
book.setBookName(rs.getString(2));
book.setBookWriter(rs.getString(3));
list.add(book);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public Boolean save(Book book) {
// TODO Auto-generated method stub
ConnectionUtil conU = new ConnectionUtil();
Connection con = null;
Boolean flag = false;
try {
con = conU.getConnection();
String InsertSQL = "insert book values(" + book.getBookID() + ",'"
+ book.getBookName() + "','" + book.getBookWriter() + "')";
PreparedStatement ps = con.prepareStatement(InsertSQL);
flag = ps.execute();
return flag;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return flag;
}
}
6servlet控制層 獲得使用者請求,將表儲存在資料庫中,實現頁面跳轉;
package com.mike.controlServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mike.daoImpl.BookDaoImpl;
import com.mike.vo.Book;
public class BookServlet extends HttpServlet {
public BookServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String bookName = request.getParameter("bookName");
String bookWriter = request.getParameter("bookWriter");
BookDaoImpl bookDao = new BookDaoImpl();
Book book = new Book();
Integer bookID = 13574;// only for easy
book.setBookID(bookID);
book.setBookName(bookName);
book.setBookWriter(bookWriter);
bookDao.save(book);
request.getRequestDispatcher("/Book.jsp").forward(request, response);