生活随笔
收集整理的這篇文章主要介紹了
javaweb 从数据库读取数据的详细操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 前言
- 一、第一步創建bean包
- 二、第二步創建dao包
- 三、創建servlet
- 四、創建jsp文件,用來取數據并顯示
前言
從數據庫讀取數據的詳細操作,用購物車案例作為例子
提示:以下是本篇文章正文內容,下面案例可供參考
一、第一步創建bean包
可以先寫物品屬性,例如商品有編號、名稱、價格
private Integer id
;
private String name
;
private Double price
;
再使用快捷鍵alt+shift+s 創建默認構造函數、帶參數構造器、get和set方法
package javaweb.bean;public class Goods {private Integer id
;private String name
;private Double price
;public Goods() {super();}public Goods(Integer id
, String name
, Double price
) {super();this.id
= id
;this.name
= name
;this.price
= price
;}public Integer getId() {return id
;}public void setId(Integer 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
;}}
二、第二步創建dao包
創建bean的鏈表,用來接收數據3-7是使用用jdbc查詢數據庫數據的操作先連上數據庫使用username ,password登錄創建sql語句執行sql語句取結果集用bean創建對象,將取出來的數據封裝到在里面將封裝的數據加到先前創建的list里返回list(因為是在一個函數里面寫的,所以可以返回值便于后面接收值使用)
package javaweb.dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import cn.hutool.json.JSONArray;
import javaweb.bean.Goods;public class GoodsDao {public List<Goods> find(){List<Goods> list
=new ArrayList<Goods>();Connection conn
=null;PreparedStatement pstmt
=null;ResultSet rs
=null;try {Class.forName("com.mysql.jdbc.Driver");String url
= "jdbc:mysql://localhost:3306/javaweb?useUnicode=true&characterEcoding=utf-8";String username
= "root";String password
= null;conn
=DriverManager.getConnection(url
,username
,password
);String sql
="select id,name,price from t_goods";pstmt
=conn
.prepareStatement(sql
);rs
=pstmt
.executeQuery();while(rs
.next()) {Goods i
=new Goods();i
.setId(rs
.getInt(1));i
.setName(rs
.getString(2));i
.setPrice(rs
.getDouble(3));list
.add(i
);}} catch (Exception e
) {}finally {if(pstmt
!=null) {try {pstmt
.close();} catch (SQLException e
) {e
.printStackTrace();}}if(conn
!=null) {try {conn
.close();} catch (SQLException e
) {e
.printStackTrace();}}}return list
;}
}
三、創建servlet
package javaweb.servlet;import java.io.IOException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import cn.hutool.json.JSONArray;
import javaweb.bean.Goods;
import javaweb.dao.GoodsDao;@WebServlet("/shopping/index")
public class ShoppingServlet extends HttpServlet {protected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException, IOException {GoodsDao dao
=new GoodsDao();List<Goods> goods
=dao
.find();request
.setAttribute("goods", goods
);request
.getRequestDispatcher("/shopping/index.jsp").forward(request
, response
);}protected void doPost(HttpServletRequest request
, HttpServletResponse response
) throws ServletException, IOException {doGet(request
, response
);}}
四、創建jsp文件,用來取數據并顯示
取出request域中的數據(這個數據是Goods的鏈表,request域中取出的object類型,需要強轉一下)
使用jsp拼接語句,達到循環讀取數據
<%@page import="java.util.List"%>
<%@page import="javaweb.bean.Goods"%>
<%@ page language
="java" contentType
="text/html; charset=UTF-8"pageEncoding
="UTF-8"%>
<!DOCTYPE html
>
<html>
<head>
<meta charset
="UTF-8">
<title>小紅帽商城
</title
>
<link rel
="stylesheet" type
="text/css" href
="${pageContext.request.contextPath}/layui/css/layui.css">
<script type
="text/javascript" src
="${pageContext.request.contextPath }/layui/layui.js"></script
>
</head
>
<body>
<div
class="layui-bg-red"><div
class="layui-container"><h3
class="layui-inline ">小紅帽商城
</h3
><div
class="layui-nav layui-inline layui-bg-red"><div
class="layui-nav-item"><a href
="${pageContext.request.contextPath}/shopping">商城首頁
</a
></div
></div
></div
>
</div
><div
class="layui-container"><table
class="layui-table"><thead><tr><th>編號
</th
><th>商品名稱
</th
><th>價格
</th
><th></th
></tr
></thead
><tbody><%<%--使用jsp拼接語句,達到循環讀取數據
--%>List<Goods> goods
=(List<Goods>)request
.getAttribute("goods");for(Goods i
:goods
){%><tr><td><%=i
.getId() %></td
><td><%=i
.getName() %></td
><td><%=i
.getPrice()%></td
><td style
="width:140px;text-align:center;"><form action
="${pageContext.request.contextPath}/shopping/add" class="layui-form layui-inline"><input type
="text" value
="1" name
="number" style
="width:60px;" class="layui-input layui-input-inline"><button type
="submit" class="layui-btn layui-bg-red "><i
class="layui-icon layui-icon-cart"></i
></button
> </form
></td
></tr
><%}%></tbody
></table
></div
>
</body
>
</html
>
以上為從數據庫讀取數據的詳細操作。
歡迎訪問我的個人博客http://dzyblog.xyz/有更好的閱讀體驗
總結
以上是生活随笔為你收集整理的javaweb 从数据库读取数据的详细操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。