某网上书城可以查询、删除书籍,现要求使用JSP实现该功能 20
题目某网上书城可以查询、删除书籍,现要求使用JSP实现该功能。创建一个Web应用程序,并且创建JSP页面,点击“删除”按钮后可以删除一本书,显示“删除成功”和返回的链接。...
题目
某网上书城可以查询、删除书籍,现要求使用JSP实现该功能。创建一个Web应用程序,并且创建JSP页面,点击“删除”按钮后可以删除一本书,显示“删除成功”和返回的链接。
使用SQL Server 2005创建数据库exam,创建表book,表中有三个字段,主键:id,书名: bname,价格:price,输入几条测试记录。页面如图1、图2所示:(内容结构要与图中所示相同,效果可以不同)
图1 书籍列表页面
图2 结果页面
要求:
1、 创建Dao类操作数据库,不可以在JSP中直接连接数据库;
2、 使用纯Java的方式连接数据库,参考如下:
3、 使用三层结构构建项目 展开
某网上书城可以查询、删除书籍,现要求使用JSP实现该功能。创建一个Web应用程序,并且创建JSP页面,点击“删除”按钮后可以删除一本书,显示“删除成功”和返回的链接。
使用SQL Server 2005创建数据库exam,创建表book,表中有三个字段,主键:id,书名: bname,价格:price,输入几条测试记录。页面如图1、图2所示:(内容结构要与图中所示相同,效果可以不同)
图1 书籍列表页面
图2 结果页面
要求:
1、 创建Dao类操作数据库,不可以在JSP中直接连接数据库;
2、 使用纯Java的方式连接数据库,参考如下:
3、 使用三层结构构建项目 展开
1个回答
展开全部
简单的增删改查功能
package EBookDao;
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 DBUtil.DBUtil;
import DBUtil.EBook;
/*
*查询书集信息
*
* */
public class EBookCZ {
public List<EBook> find() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<EBook> list = new ArrayList<EBook>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM ebook";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
EBook eBook = new EBook();
eBook.setId(rs.getInt("ID"));
eBook.setBname(rs.getString("bname"));
eBook.setPrice(rs.getString("price"));
list.add(eBook);
System.out.println(rs.getInt("ID")+rs.getString("bname")+rs.getString("price"));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return list;
}
/*
*通过id删除书集信息
*
* */
public static EBook DelById(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
EBook book = null;
try {
conn = DBUtil.getConnection();
String sql = "delete ebook ";
sql += "WHERE ID=? ";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
book = new EBook();
book.setId(rs.getInt("ID"));
book.setBname(rs.getString("bname"));
book.setPrice(rs.getString("price"));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return book;
}
}
package EBookServlet;
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 DBUtil.EBook;
import EBookDao.EBookCZ;
public class EBookServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public EBookServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String method = request.getParameter("method");
String url = "";
EBookCZ bookCZ=new EBookCZ();
if (method != null && method.equals("find")) {
System.out.println("find做完了");
url = "EBookMNG.jsp";
List<EBook> list = bookCZ.find();
request.setAttribute("list", list);
} else if (method != null && method.equals("findById")) {
System.out.println("findbyid做完了");
url = "OK.jsp";
int id = Integer.parseInt(request.getParameter("id"));
EBook book = EBookCZ.DelById(id);
request.setAttribute("book", book);
}
request.getRequestDispatcher(url).forward(request, response);
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询