实验:
实现查询数据库shop里面的内容并且显示到表格中
思路:
DBUtil.java(数据库连接类,它专门负责数据库连接工作)
ShopDao.java(数据访问对象类),让它去调用DBUtil,连接数据库后,在它里面写SQL语句操作数据库数据(DAO类都是进行数据操作的类,是对于数据库中的数据做增删改查等操作的代码。)
Shop.java(关系型数据转换对象型数据),表中有几个属性,我新建的类中就有几个属性,与表完全匹配,包括数据类型也是完全匹配。
这样我rs.getInt(1)在表获得的数据是不是就可以在类中写方法setXXX()把数据写到对象的属性中去
GoodsServlet.java(它的职责是处理业务逻辑,所以我们让它获取到数据,转发给JSP),调用ShopDao中的getAll()了,得到一个list对象,里面是shop表的所有数据,然后把它转发给goodslist.jsp,goodslist.jsp就能把list的数据读出来,然后绘制到页面上
步骤:
①通过derby创建数据库ShopStore,数据表Shop,为项目配置好derby(参考:https://blog.csdn.net/MYJJJJJ/article/details/105363127)
②DBUtil.java:
package cc.home.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private static String dbName= "D:\\ShopStore";
public static Connection getConn() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection("jdbc:derby:"+dbName+";create=true");
//与数据库进行连接
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs,Statement st,Connection conn) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st!=null) {
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public DBUtil() {
// TODO Auto-generated constructor stub
}
}
Shop.java:
package cc.home.entity;
public class Shop {
private int id;
private String shopId;
private String shopName;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getShopId() {
return shopId;
}
public void setShopId(String shopId) {
this.shopId = shopId;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Shop() {
// TODO Auto-generated constructor stub
}
}
ShopDao.java:
package cc.home.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import cc.home.entity.Shop;
import cc.home.utils.DBUtil;
public class ShopDao {
public ArrayList<Shop> findAll(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<Shop> list = new ArrayList<Shop>();
conn = DBUtil.getConn();//获得DBUtil里面的getConn方法,创建数据库连接
try {
ps = conn.prepareStatement("select * from shop");
rs = ps.executeQuery();//执行上面的语句
while(rs.next()) {//遍历查询shop表
Shop s = new Shop();
s.setId(rs.getInt("id"));
s.setShopId(rs.getString("shopid"));
s.setShopName(rs.getString("shopname"));
s.setPrice(rs.getFloat("price"));
list.add(s);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.closeAll(rs, ps, conn);
}
return list;
}
public ShopDao() {
// TODO Auto-generated constructor stub
}
}
GoodsServlet.java:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ShopDao shopdao = new ShopDao();
List<Shop> list = shopdao.findAll();
request.setAttribute("list", list);
request.getRequestDispatcher("/goodslist.jsp").forward(request, response);
}
goodslist.jsp:
<%@page import="java.util.List" %>
<%@page import="cc.home.entity.Shop" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" >
<tr>
<td>编号</td>
<td>商品编号</td>
<td>商品名称</td>
<td>价格</td>
</tr>
<%
List<Shop> list =(List<Shop>)request.getAttribute("list");
for(int i=0;i<list.size();i++){
Shop shop = new Shop();
shop = list.get(i);
%>
<tr>
<td><%=list.get(i).getId() %></td>
<td><%=list.get(i).getShopId() %></td>
<td><%=list.get(i).getShopName() %></td>
<td><%=list.get(i).getPrice() %></td>
</tr>
<% }%>
</table>
</body>
</html>
start.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/ShopDemo/GoodsServlet">查询商品信息</a>
</body>
</html>
执行过程中出现的问题:
① jsp文件在写list时要注意导入该包,一般情况下的报错就是忘记导入包
<%@page import=“java.util.List” %>
②在写完各个文件时无法连接数据库
因为在最开始数据库DBUtil.java里面数据库无法连接,原因是用的是EmbeddedDriver, url就不能按照client/server格式来,应该是 jdbc:derby:;create=true