日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

javaweb 从数据库读取数据的详细操作

發(fā)布時(shí)間:2024/3/12 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javaweb 从数据库读取数据的详细操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 前言
  • 一、第一步創(chuàng)建bean包
  • 二、第二步創(chuàng)建dao包
  • 三、創(chuàng)建servlet
  • 四、創(chuàng)建jsp文件,用來取數(shù)據(jù)并顯示


前言

從數(shù)據(jù)庫讀取數(shù)據(jù)的詳細(xì)操作,用購物車案例作為例子


提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一、第一步創(chuàng)建bean包

可以先寫物品屬性,例如商品有編號、名稱、價(jià)格

private Integer id; private String name; private Double price;

再使用快捷鍵alt+shift+s 創(chuàng)建默認(rèn)構(gòu)造函數(shù)、帶參數(shù)構(gòu)造器、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;}}

二、第二步創(chuàng)建dao包


  • 創(chuàng)建bean的鏈表,用來接收數(shù)據(jù)
  • 3-7是使用用jdbc查詢數(shù)據(jù)庫數(shù)據(jù)的操作
  • 先連上數(shù)據(jù)庫
  • 使用username ,password登錄
  • 創(chuàng)建sql語句
  • 執(zhí)行sql語句
  • 取結(jié)果集
  • 用bean創(chuàng)建對象,將取出來的數(shù)據(jù)封裝到在里面
  • 將封裝的數(shù)據(jù)加到先前創(chuàng)建的list里
  • 返回list(因?yàn)槭窃谝粋€(gè)函數(shù)里面寫的,所以可以返回值便于后面接收值使用)
  • 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>();//創(chuàng)建bean的鏈表,用來接收數(shù)據(jù)Connection conn=null;//先連上數(shù)據(jù)庫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);//使用username ,password登錄String sql="select id,name,price from t_goods";//創(chuàng)建sql語句pstmt=conn.prepareStatement(sql);//執(zhí)行sql語句rs=pstmt.executeQuery();//取結(jié)果集while(rs.next()) {Goods i=new Goods();//用bean創(chuàng)建對象,將取出來的數(shù)據(jù)封裝到在里面i.setId(rs.getInt(1));i.setName(rs.getString(2));i.setPrice(rs.getDouble(3));list.add(i);//將封裝的數(shù)據(jù)加到先前創(chuàng)建的list里}} catch (Exception e) {// TODO: handle exception}finally {//使用完,要關(guān)閉連接if(pstmt!=null) {try {pstmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(conn!=null) {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return list;} }

    三、創(chuàng)建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();//創(chuàng)建GoodsDao的對象List<Goods> goods=dao.find();//調(diào)用find方法request.setAttribute("goods", goods);//放到request域中request.getRequestDispatcher("/shopping/index.jsp").forward(request, response);//轉(zhuǎn)發(fā)給shopping/index.jsp}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}

    四、創(chuàng)建jsp文件,用來取數(shù)據(jù)并顯示

  • 取出request域中的數(shù)據(jù)(這個(gè)數(shù)據(jù)是Goods的鏈表,request域中取出的object類型,需要強(qiáng)轉(zhuǎn)一下)

  • 使用jsp拼接語句,達(dá)到循環(huán)讀取數(shù)據(jù)

  • <%@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>價(jià)格</th><th></th></tr></thead><tbody><%<%--使用jsp拼接語句,達(dá)到循環(huán)讀取數(shù)據(jù)--%>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>

    以上為從數(shù)據(jù)庫讀取數(shù)據(jù)的詳細(xì)操作。
    歡迎訪問我的個(gè)人博客http://dzyblog.xyz/有更好的閱讀體驗(yàn)

    總結(jié)

    以上是生活随笔為你收集整理的javaweb 从数据库读取数据的详细操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。