摘要:是數(shù)據(jù)中查詢結(jié)果返回的一種對(duì)象商品集合連接數(shù)據(jù)庫(kù)語(yǔ)句把一個(gè)商品加入集合返回集合釋放數(shù)據(jù)集對(duì)象釋放語(yǔ)句對(duì)象執(zhí)行語(yǔ)句要記得捕獲異常,且要用釋放資源。
JSP 商品信息[Web]
采用Model1(jsp+Javabean)實(shí)現(xiàn)
實(shí)現(xiàn)DBHelper類(連接數(shù)據(jù)庫(kù))
創(chuàng)建實(shí)體類
創(chuàng)建業(yè)務(wù)邏輯類(DAO)
創(chuàng)建頁(yè)面層
一、環(huán)境準(zhǔn)備 1.1 MySQL 安裝Mac 安裝方式
官網(wǎng)下載安裝包dmg即可
安裝
偏好設(shè)置啟動(dòng)mysql
配置bash_profile
添加“export PATH=$PATH:/usr/local/mysql/bin”
下載MySQL驅(qū)動(dòng) JDBC
1.2 創(chuàng)建項(xiàng)目IDEA選擇: Java Enterprise -> Web application
配置項(xiàng)目:
WEB_INF 內(nèi)創(chuàng)建 classes 和 lib 文件夾
File -> Project Structure -> paths -> Use module compile output path 選擇classes文件夾
File -> Project Structure -> Dependecies -> “+”號(hào) -> JAR… -> 選擇創(chuàng)建的lib文件夾
1.3 數(shù)據(jù)庫(kù)創(chuàng)建開(kāi)啟終端:登錄數(shù)據(jù)庫(kù)
mysql -u root -p
創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù) -> shopping :
create database shopping;
查看是否創(chuàng)建成功:
show databases;1.4 連接數(shù)據(jù)庫(kù)測(cè)試
IDEA: View -> Tool Windows -> Database
: 選擇 Data Source -> MySQL
Database:shopping
再填寫(xiě)用戶名+密碼,Test Connection
復(fù)制粘貼:
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for items -- ---------------------------- DROP TABLE IF EXISTS `items`; CREATE TABLE `items` ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) default NULL, `city` varchar(50) default NULL, `price` int(11) default NULL, `number` int(11) default NULL, `picture` varchar(500) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of items -- ---------------------------- INSERT INTO `items` VALUES ("1", "沃特籃球鞋", "佛山", "180", "500", "001.jpg"); INSERT INTO `items` VALUES ("2", "安踏運(yùn)動(dòng)鞋", "福州", "120", "800", "002.jpg"); INSERT INTO `items` VALUES ("3", "耐克運(yùn)動(dòng)鞋", "廣州", "500", "1000", "003.jpg"); INSERT INTO `items` VALUES ("4", "阿迪達(dá)斯T血衫", "上海", "388", "600", "004.jpg"); INSERT INTO `items` VALUES ("5", "李寧文化衫", "廣州", "180", "900", "005.jpg"); INSERT INTO `items` VALUES ("6", "小米3", "北京", "1999", "3000", "006.jpg"); INSERT INTO `items` VALUES ("7", "小米2S", "北京", "1299", "1000", "007.jpg"); INSERT INTO `items` VALUES ("8", "thinkpad筆記本", "北京", "6999", "500", "008.jpg"); INSERT INTO `items` VALUES ("9", "dell筆記本", "北京", "3999", "500", "009.jpg"); INSERT INTO `items` VALUES ("10", "ipad5", "北京", "5999", "500", "010.jpg");
運(yùn)行,生成表
1.5.3 刷新數(shù)據(jù)庫(kù),查看結(jié)果 1.6 存放數(shù)據(jù)庫(kù)的圖片到Web項(xiàng)目項(xiàng)目中web目錄下新建一個(gè)文件夾images
找10張圖片放入,命名格式”001.jpg”,”002.jgp”…
二、DBHelper類 連接數(shù)據(jù)庫(kù)定義靜態(tài)變量:數(shù)據(jù)庫(kù)驅(qū)動(dòng)
public static final String driver = "com.mysql.jdbc.Driver"; //數(shù)據(jù)庫(kù)驅(qū)動(dòng) //連接數(shù)據(jù)庫(kù)的URL地址 public static final String url = "jdbc:mysql://localhost:3306/shopping?useUnicode=true&charactorEncoding=UTF-8"; public static final String username = "root"; public static final String password = "amoy1205"; public static Connection conn = null;
靜態(tài)代碼塊負(fù)責(zé)加載驅(qū)動(dòng),需要捕獲異常
static{ try{ Class.forName(driver); //加載驅(qū)動(dòng) } catch (Exception ex){ ex.printStackTrace(); } }
單例模式:返回?cái)?shù)據(jù)庫(kù)連接對(duì)象
public static Connection getConnection() throws Exception{ if(conn==null){ conn = DriverManager.getConnection(url, username, password); return conn; } return conn; //說(shuō)明被實(shí)例化過(guò) }
寫(xiě)個(gè)方法測(cè)試是否連接成功:
public static void main(String[] args){ try { Connection conn = DBHelper.getConnection(); if(conn!=null){ System.out.println("數(shù)據(jù)庫(kù)連接正常"); } else { System.out.println("數(shù)據(jù)庫(kù)連接異常"); } } catch (Exception ex){ ex.printStackTrace(); } }三、item 類(存放商品實(shí)體)
定義商品屬性 :
private int id ; //商品編號(hào) private String name; //商品名稱 private String city; //產(chǎn)地 private int price; //價(jià)格 private int number; //庫(kù)存 private String picture; //商品圖片四、ItemDAO 類 (業(yè)務(wù)邏輯類) 4.1 獲取全部商品信息的方法
通過(guò)SQL語(yǔ)句:select * from Items; 從表items查詢結(jié)果。
public ArrayListgetAllItems() { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; //(ResultSet)是數(shù)據(jù)中查詢結(jié)果返回的一種對(duì)象 ArrayList list = new ArrayList ();//商品集合 try{ conn = DBHelper.getConnection(); //連接數(shù)據(jù)庫(kù) String sql = "select * from Items;"; //SQL語(yǔ)句 stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while(rs.next()) { Items item = new Items(); item.setId(rs.getInt("id")); item.setName(rs.getString("name")); item.setCity(rs.getString("city")); item.setNumber(rs.getInt("number")); item.setPrice(rs.getInt("price")); item.setPicture(rs.getString("picture")); list.add(item); //把一個(gè)商品加入集合 } return list; //返回集合 } catch (Exception ex){ ex.printStackTrace(); return null; } finally { //釋放數(shù)據(jù)集對(duì)象 if(rs!=null) { try { rs.close(); rs = null; } catch (Exception ex) { ex.printStackTrace(); } } //釋放語(yǔ)句對(duì)象 if(stmt!=null) { try { stmt.close(); stmt = null; } catch (Exception ex) { ex.printStackTrace(); } } } }
執(zhí)行SQL語(yǔ)句要記得捕獲異常,且要用finally釋放資源。
PreparedStatement : 執(zhí)行SQL查詢語(yǔ)句的API之一4.2 根據(jù)商品編號(hào)獲取商品信息
「JDBC 中preparedStatement和Statement區(qū)別」參考資料:https://blog.csdn.net/xuebing1995/article/details/72235380
public Items getItemsById()
和獲取全部信息的代碼差不多,這里僅修改try{}里的代碼:
修改sql查詢語(yǔ)句
stmt.setInt(1,id)將指定的參數(shù)設(shè)置為給定的java int值, sql將查詢id匹配的條目
查詢結(jié)果不用循環(huán)
返回item而不是list
try{ conn = DBHelper.getConnection(); String sql = "select * from Items WHERE id=?;"; //SQL語(yǔ)句 stmt = conn.prepareStatement(sql); stmt.setInt(1,id); rs = stmt.executeQuery(); if(rs.next()) { Items item = new Items(); item.setId(rs.getInt("id")); item.setName(rs.getString("name")); item.setCity(rs.getString("city")); item.setNumber(rs.getInt("number")); item.setPrice(rs.getInt("price")); item.setPicture(rs.getString("picture")); return item; } else { return null; } }4.3 通過(guò)cookie實(shí)現(xiàn) 商品瀏覽記錄DAO
① 傳入list字符串,通過(guò)分隔符”#”識(shí)別出不同的商品id,存放到字符串?dāng)?shù)組arr中
注意: Tomcat高版本中,Cookie對(duì)象的構(gòu)造函數(shù)的兩個(gè)字符串參數(shù):Cookie名字和Cookie值都不能包含空白字符以及下列字符:[]() < > = , " / ? @ :
因此,分隔符采用”#”
② 根據(jù)分割后的id,查詢商品信息,添加到itemlist中,返回
public ArrayList五、頁(yè)面層 5.1 index.jsp 5.1.1 中添加css樣式getViewList(String list){ System.out.println("list:"+list); ArrayList itemlist = new ArrayList (); int iCount = 5; if (list!=null && list.length()>=0) { String[] arr = list.split("#"); System.out.println("arr.length="+arr.length); if(arr.length>=5) { for(int i=arr.length-1;i>=arr.length-iCount;i--) { itemlist.add(getItemsById(Integer.parseInt(arr[i]))); } } else { for(int i=arr.length-1;i>=0;i--) { itemlist.add(getItemsById(Integer.parseInt(arr[i]))); } } return itemlist; } else { return null; } }
5.1.2 獲取全部商品信息商品展示
調(diào)用ItemsDAO的getAllItems() 獲得所有商品的Item實(shí)例
遍歷打印商品信息:
商品展示
<%
ItemsDAO itemsDao = new ItemsDAO();
ArrayList |
為圖片設(shè)置超鏈接,目的:進(jìn)入到商品詳情頁(yè)面5.2 details.jsp
實(shí)現(xiàn)功能:顯示商品詳情(點(diǎn)取超鏈接時(shí)傳入的id值再調(diào)用ItemsDAO的getItemsById()獲取商品信息)+最近瀏覽商品記錄(cookie實(shí)現(xiàn))
css樣式和index.jsp相同,復(fù)制即可中需要處理的:
if(c.getName().equals("ListViewCookie")) { list = c.getValue(); }
③ 追加本次瀏覽的記錄:
list+=request.getParameter("id")+"#";
④ 創(chuàng)建新的cookie對(duì)象,并將其放到response:
Cookie cookie = new Cookie("ListViewCookie",list); response.addCookie(cookie);
⑤ 再通過(guò)ItemsDAO的getViewList()獲取cookie的字符串,根據(jù)返回的列表打印最近瀏覽的記錄
標(biāo)簽中添加完整代碼:
|
<%
}
%>
<%
String list ="";
//從客戶端獲得Cookies集合
Cookie[] cookies = request.getCookies();
//遍歷這個(gè)Cookies集合
if(cookies!=null&&cookies.length>0)
{
for(Cookie c:cookies)
{
if(c.getName().equals("ListViewCookie"))
{
list = c.getValue();
}
}
}
list+=request.getParameter("id")+"#";
//如果瀏覽記錄超過(guò)1000條,清零.
String[] arr = list.split("#");
if(arr!=null&&arr.length>0)
{
if(arr.length>=1000)
{
list="";
}
}
Cookie cookie = new Cookie("ListViewCookie",list);
response.addCookie(cookie);
%>
您瀏覽過(guò)的商品 <% ArrayList |
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/76636.html
摘要:創(chuàng)建一個(gè)工程在里面添加依賴,依賴不要隨便改我改了出錯(cuò)了好幾次都找不到原因可以輕松的將對(duì)象轉(zhuǎn)換成對(duì)象和文檔同樣也可以將轉(zhuǎn)換成對(duì)象和配置 1.創(chuàng)建一個(gè)web工程2.在pom里面添加依賴,依賴不要隨便改,我改了出錯(cuò)了好幾次都找不到原因 UTF-8 1.7 1.7 2.5.0 1.2 3.0-alpha-1 ...
摘要:對(duì)象具有請(qǐng)求域,即完成客戶端的請(qǐng)求之前,該對(duì)象一直有效。提交的數(shù)據(jù)量最多不超過(guò)。安全性較低但效率比方式高。適合提交數(shù)據(jù)量大,安全性高的用戶信息。除非本次會(huì)話的所有頁(yè)面都關(guān)閉后再重新訪問(wèn)某個(gè)或者,將會(huì)創(chuàng)建新的會(huì)話。 JSP 簡(jiǎn)介 全名為Java Server Pages,其根本是一個(gè)簡(jiǎn)化的Servlet設(shè)計(jì),實(shí)現(xiàn)了在Java當(dāng)中使用HTML標(biāo)簽。JSP是一種動(dòng)態(tài)網(wǎng)頁(yè)技術(shù)標(biāo)準(zhǔn),也是Java...
摘要:于是乎服務(wù)器向用戶瀏覽器發(fā)送了一個(gè)名為的,它的值是的值。標(biāo)記著該用戶已經(jīng)登陸了跳轉(zhuǎn)到其他頁(yè)面,告訴用戶成功登陸了。注冊(cè)多個(gè)用戶,不斷發(fā)帖子,擾亂正常發(fā)帖秩序。在處理表單的中刷新。監(jiān)聽(tīng)用戶提交事件。 什么是Session Session 是另一種記錄瀏覽器狀態(tài)的機(jī)制。不同的是Cookie保存在瀏覽器中,Session保存在服務(wù)器中。用戶使用瀏覽器訪問(wèn)服務(wù)器的時(shí)候,服務(wù)器把用戶的信息以某種...
閱讀 918·2021-10-13 09:39
閱讀 3571·2021-09-26 10:16
閱讀 2932·2019-08-30 15:54
閱讀 1070·2019-08-30 14:22
閱讀 2913·2019-08-29 15:39
閱讀 3289·2019-08-27 10:52
閱讀 838·2019-08-26 13:59
閱讀 1750·2019-08-26 12:20