架构:
Book.java:
package cn.ding.entity;
import java.util.Date;
/**
*
-
@author irin
*/
public class Book {
private int id;
private String name;
private double price;
private String author;
private Date pubDate;public Book() {
}public Book(String name, double price, String author, Date pubDate) {
this.name = name;
this.price = price;
this.author = author;
this.pubDate = pubDate;
}public Book(int id, String name, double price, String author, Date pubDate) {
this.id = id;
this.name = name;
this.price = price;
this.author = author;
this.pubDate = pubDate;
}public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public double getPrice() {
return price;
}public void setPrice(double price) {
this.price = price;
}public String getAuthor() {
return author;
}public void setAuthor(String author) {
this.author = author;
}public Date getPubDate() {
return pubDate;
}public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
}
BookDao:
package cn.ding.dao;
import cn.ding.entity.Book;
import cn.ding.utils.DBHelper;
import static java.lang.reflect.Array.getDouble;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.swing.UIManager.getString;
import static jdk.nashorn.internal.objects.Global.getDate;
/**
*
- @author irin */ public class BookDao extends DBHelper{ //查询所有书籍 public List getAll(){ List list = new ArrayList(); String sql = "select id,name,price,author,pubDate from book"; try { ResultSet rs = this.executeQuery(sql); while(rs.next()){ list.add(new Book(rs.getInt(1),rs.getString(2),rs.getDouble(3),rs.getString(4),rs.getDate(5))); } } catch (Exception e) { e.printStackTrace(); }finally{ this.close(); } return list; }
}
DBHelper:
package cn.ding.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
-
@author irin
*/
public class DBHelper {
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;//获得连接
private void getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/booksys";
conn = DriverManager.getConnection(url,"root","Ding1234");
} catch (Exception e) {
e.printStackTrace();
}}
//关闭
public void close(){
if(conn!=null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}if(ps!=null){ try { ps.close(); } catch (Exception e) { e.printStackTrace(); } ps = null; } if(rs!=null){ try { rs.close(); } catch (Exception e) { e.printStackTrace(); } rs = null; }
}
//更新--增加、修改、删除
public int executeUpdate(String sql,Object...objects){
try {
this.getConnection();
ps = conn.prepareStatement(sql);
if(objects!=null){
for(int i=0;i<objects.length;i++){
ps.setObject(i+1, objects[i]);
}
}
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
this.close();
}
return -1;
}//查询
public ResultSet executeQuery(String sql,Object...objects){
try {
this.getConnection();
ps = conn.prepareStatement(sql);
if(objects!=null){
for(int i=0;i<objects.length;i++){
ps.setObject(i+1, objects[i]);
}
}
return rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}finally{
this.close();
}
return null;
}
}
ListServlet:
package cn.ding.servlet;
import cn.ding.dao.BookDao;
import cn.ding.entity.Book;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
@author irin
*/
public class ListServlet extends HttpServlet {
private BookDao bookDao = new BookDao();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List list = bookDao.getAll();
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter pw = resp.getWriter();
pw.print("");
pw.print("");
pw.print("书籍列表");
pw.print("");
pw.print("");
pw.print("");
pw.print(""); ");
pw.print(""); ");
pw.print("编号");
pw.print("
pw.print(""); ");
pw.print("姓名");
pw.print("
pw.print(""); ");
pw.print("价格");
pw.print("
pw.print(""); ");
pw.print("作者");
pw.print("
pw.print(""); ");
pw.print("出版日期");
pw.print("
pw.print("for(int i=0;i<list.size();i++){ pw.print("<tr>"); pw.print("<td>"); pw.print(list.get(i).getId()); pw.print("</td>"); pw.print("<td>"); pw.print(list.get(i).getName()); pw.print("</td>"); pw.print("<td>"); pw.print(list.get(i).getPrice()); pw.print("</td>"); pw.print("<td>"); pw.print(list.get(i).getAuthor()); pw.print("</td>"); pw.print("<td>"); pw.print(new SimpleDateFormat("YYYY-MM-DD").format(list.get(i).getPubDate())); pw.print("</td>"); pw.print("</tr>"); } pw.print("</table>"); pw.print("</body>"); pw.print("</html>");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}}
配置文件web.xml:
<?xml version="1.0" encoding="UTF-8"?>
javax.faces.PROJECT_STAGE
Development
ListServlet
cn.ding.servlet.ListServlet
1
ListServlet
/list
30
faces/index.xhtml数据库navicat:
运行结果:
求大家帮帮忙!真的很着急,到底哪里出现问题了,网页里面读不到数据库的内容!谢谢大家了!
大家都让我debug调试,可是我不太会调试,应该调试哪个文件,如何进行呢...