增刪改查

web層代碼如下:

package beyondwsq.web;import java.io.IOException;
import java.sql.SQLException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import beyondwsq.domain.Product;
import beyondwsq.service.AdminProductService;public class AdminProductListServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//傳遞請求到service層AdminProductService service = new AdminProductService();List<Product> productList = null;try {productList = service.findAllProduct();//獲取所有的數據} catch (SQLException e) {e.printStackTrace();}//獲取數據完畢request.setAttribute("productList", productList);//將productList放到request域當中request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);;//轉發到該頁面(list.jsp)下;這里是服務器內部地址,不需要寫web應用的名稱//到list.jsp頁面中取出數據}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}

service層方法如下:

package beyondwsq.service;import java.sql.SQLException;
import java.util.List;import beyondwsq.dao.AdminProductDao;
import beyondwsq.domain.Product;public class AdminProductService {//查詢所有的商品public List<Product> findAllProduct() throws SQLException {//因為沒事啥復雜業務,所以直接傳遞請求到dao層AdminProductDao dao = new AdminProductDao();return dao.findAllProduct();//需要事務控制,拋到service層,不需要事務控制直接拋到web層}}

dao層代碼如下:

package beyondwsq.dao;import java.sql.SQLException;
import java.util.List;import javax.activation.DataSource;import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.taglibs.standard.tag.common.sql.DataSourceUtil;import beyondwsq.domain.Product;
import beyondwsq.utils.DataSourceUtils;public class AdminProductDao {//進行數據庫訪問,public List<Product> findAllProduct() throws SQLException {QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());//不需要事務控制直接傳參String sql = "select *from product";//進行sql訪問List<Product> productList = runner.query(sql, new BeanListHandler<Product>(Product.class));//查詢Query方法return productList;//拋給service}}

domain代碼:

package beyondwsq.domain;public class Product {/*`pid` varchar(32) NOT NULL,`pname` varchar(50) DEFAULT NULL,`market_price` double DEFAULT NULL,`shop_price` double DEFAULT NULL,`pimage` varchar(200) DEFAULT NULL,`pdate` date DEFAULT NULL,`is_hot` int(11) DEFAULT NULL,`pdesc` varchar(255) DEFAULT NULL,`pflag` int(11) DEFAULT NULL,`cid` varchar(32) DEFAULT NULL,*//*	數據庫web17下創建表的代碼如下:CREATE TABLE `product` (`pid` VARCHAR(50) NOT NULL,`pname` VARCHAR(50) DEFAULT NULL,`market_price` DOUBLE DEFAULT NULL,`shop_price` DOUBLE DEFAULT NULL,`pimage` VARCHAR(200) DEFAULT NULL,`pdate` DATE DEFAULT NULL,`is_hot` INT(11) DEFAULT NULL,`pdesc` VARCHAR(255) DEFAULT NULL,`pflag` INT(11) DEFAULT NULL,`cid` VARCHAR(32) DEFAULT NULL,PRIMARY KEY (`pid`))*/private String pid;private String pname;private double market_price;private double shop_price;private String pimage;private String pdate;private int is_hot;private String pdesc;private int pflag;public String getPid() {return pid;}public void setPid(String pid) {this.pid = pid;}public String getPname() {return pname;}public void setPname(String pname) {this.pname = pname;}public double getMarket_price() {return market_price;}public void setMarket_price(double market_price) {this.market_price = market_price;}public double getShop_price() {return shop_price;}public void setShop_price(double shop_price) {this.shop_price = shop_price;}public String getPimage() {return pimage;}public void setPimage(String pimage) {this.pimage = pimage;}public String getPdate() {return pdate;}public void setPdate(String pdate) {this.pdate = pdate;}public int getIs_hot() {return is_hot;}public void setIs_hot(int is_hot) {this.is_hot = is_hot;}public String getPdesc() {return pdesc;}public void setPdesc(String pdesc) {this.pdesc = pdesc;}public int getPflag() {return pflag;}public void setPflag(int pflag) {this.pflag = pflag;}public String getCid() {return cid;}public void setCid(String cid) {this.cid = cid;}private String cid;
}	

utils代碼如下:

package beyondwsq.utils;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DataSourceUtils {private static DataSource dataSource = new ComboPooledDataSource();private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();// 直接可以獲取一個連接池public static DataSource getDataSource() {return dataSource;}public static Connection getConnection() throws SQLException{return dataSource.getConnection();}// 獲取連接對象public static Connection getCurrentConnection() throws SQLException {Connection con = tl.get();if (con == null) {con = dataSource.getConnection();tl.set(con);}return con;}// 開啟事務public static void startTransaction() throws SQLException {Connection con = getCurrentConnection();if (con != null) {con.setAutoCommit(false);}}// 事務回滾public static void rollback() throws SQLException {Connection con = getCurrentConnection();if (con != null) {con.rollback();}}// 提交并且 關閉資源及從ThreadLocall中釋放public static void commitAndRelease() throws SQLException {Connection con = getCurrentConnection();if (con != null) {con.commit(); // 事務提交con.close();// 關閉資源tl.remove();// 從線程綁定中移除}}// 關閉資源方法public static void closeConnection() throws SQLException {Connection con = getCurrentConnection();if (con != null) {con.close();}}public static void closeStatement(Statement st) throws SQLException {if (st != null) {st.close();}}public static void closeResultSet(ResultSet rs) throws SQLException {if (rs != null) {rs.close();}}
}

c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config><default-config><property name="user">root</property><property name="password">wsq</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///web17</property></default-config> 
</c3p0-config> 

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379583.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379583.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379583.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

在WebBrowser中通過模擬鍵盤鼠標操控網頁中的文件上傳控件

引言 這兩天沉迷了Google SketchUp&#xff0c;剛剛玩夠&#xff0c;一時興起&#xff0c;研究了一下WebBrowser。 我在《WebBrowser控件使用技巧分享》一文中曾談到過“我現在可以通過WebBrowser實現對各種Html元素的操控&#xff0c;唯獨無法控制Html的上傳控件”&#xff0c…

編寫最簡單的字符設備驅動

編寫最簡單的字符設備驅動1 編寫驅動代碼2 編寫makefile3 編譯和加載驅動4 編寫應用程序測試驅動參考文章&#xff1a; linux驅動開發第1講&#xff1a;帶你編寫一個最簡單的字符設備驅動 linux驅動開發第2講&#xff1a;應用層的write如何調用到驅動中的write 1 編寫驅動代碼…

Java ObjectStreamField toString()方法與示例

ObjectStreamField類toString()方法 (ObjectStreamField Class toString() method) toString() method is available in java.io package. toString()方法在java.io包中可用。 toString() method is used to return a string that defines this field. toString()方法用于返回定…

linux內核文件描述符fd、文件索引節點inode、文件對象file關系

文件描述符fd、文件索引節點inode、文件對象file關系1 VFS對象1.1 超級塊對象1.2 索引節點對象1.3 文件對象1.4 進程描述符1.5 files_struct2 如何根據文件描述符fd找到文件&#xff1f;1 VFS對象 在說fd、inode和file關系之前&#xff0c;我們先了解VFS的幾個概念。分別是進程…

sql2005 獲取表字段信息和視圖字段信息

獲取表字段名,和字段說明SELECT[Table Name]OBJECT_NAME(c.object_id), [ColumnName]c.name, [Description]ex.value FROMsys.columns c LEFTOUTERJOINsys.exte…

解析css之position

CSS的很多其他屬性大多容易理解&#xff0c;比如字體&#xff0c;文本&#xff0c;背景等。有些CSS書籍也會對這些簡單的屬性進行大張旗鼓的介紹&#xff0c;而偏偏忽略了對一些難纏的屬性講解&#xff0c;有避重就輕的嫌疑。CSS中主要難以理解的屬性包括盒型結構&#xff0c;以…

Java ObjectInputStream readLong()方法(帶示例)

ObjectInputStream類readLong()方法 (ObjectInputStream Class readLong() method) readLong() method is available in java.io package. readLong()方法在java.io包中可用。 readLong() method is used to read 8 bytes (i.e. 64 bit) of long value from this ObjectInputSt…

交換瓶子(藍橋杯)

有N個瓶子&#xff0c;編號 1 ~ N&#xff0c;放在架子上。 比如有5個瓶子&#xff1a; 2 1 3 5 4 要求每次拿起2個瓶子&#xff0c;交換它們的位置。 經過若干次后&#xff0c;使得瓶子的序號為&#xff1a; 1 2 3 4 5 對于這么簡單的情況&#xff0c;顯然&#xff0c;至少…

Linux設備驅動開發---字符設備驅動程序

字符設備驅動程序1 主設備和次設備的概念設備號的注冊和釋放靜態方法動態方法區別2 設備文件操作struct file_operations與struct file、struct inode關系3 分配和注冊字符設備class_createcdev_adddevice_create4 字符設備驅動程序字符設備通過字符&#xff08;一個接一個的字…

Java LinkedHashMap getOrDefault()方法與示例

LinkedHashMap類的getOrDefault()方法 (LinkedHashMap Class getOrDefault() method) getOrDefault() method is available in java.util package. getOrDefault()方法在java.util包中可用。 getOrDefault() method is used to get the value associated with the given key el…

Java中的異常棧軌跡和異常鏈

Java中允許對異常進行再次拋出&#xff0c;以提交給上一層進行處理&#xff0c;最為明顯的例子為Java的常規異常。 常規異常&#xff1a;有Java所定義的異常&#xff0c;不需要異常聲明&#xff0c;在未被try-catch的情況下&#xff0c;會被默認上報到main()方法。 Example: pu…

貪心算法---背包問題(物品可以分割問題)

問題背景&#xff1a; 有一天&#xff0c;阿里巴巴趕著一頭毛驢上山砍柴。砍好柴準備下山時&#xff0c;遠處突然出現一股煙塵&#xff0c;彌漫著直向上空飛揚&#xff0c;朝他這兒卷過來&#xff0c;而且越來越近。靠近以后&#xff0c;他才看清原來是一支馬隊&#xff0c;他…

同步---信號量

信號量1 信號量2 驅動程序和測試程序3 內核的具體實現總結1 信號量 Linux中的信號量是一種睡眠鎖。如果有一個任務試圖獲得一個已經被占用的信號量時&#xff0c;信號量會將其放到一個等待隊列&#xff0c;然后讓其睡眠&#xff0c;這時處理器去執行其他代碼。當持有信號量的進…

Java Float類floatToIntBits()方法與示例

Float類floatToIntBits()方法 (Float class floatToIntBits() method) floatToIntBits() method is available in java.lang package. floatToIntBits()方法在java.lang包中可用。 floatToIntBits() method follows IEEE 754 floating-point standards and according to standa…

解釋三度帶和六度帶的概念以及各坐標系如何定義

★ 地形圖坐標系&#xff1a;我國的地形圖采用高斯&#xff0d;克呂格平面直角坐標系。在該坐標系中&#xff0c;橫軸&#xff1a;赤道&#xff0c;用&#xff39;表示&#xff1b;縱軸&#xff1a;中央經線&#xff0c;用&#xff38;表示&#xff1b;坐標原點&#xff1a;中央…

0-1背包問題(物品不可分割)

問題背景&#xff1a; 所謂“鐘點秘書”&#xff0c;是指年輕白領女性利用工余時間為客戶提供秘書服務&#xff0c;并按鐘點收取酬金。“鐘點秘書”為客戶提供有償服務的方式一般是&#xff1a;采用電話、電傳、上網等“遙控”式 服務&#xff0c;或親自到客戶公司處理部分業務…

算法---KMP算法

字符串1 KMP算法狀態機概述構建狀態轉移1 KMP算法 原文鏈接&#xff1a;https://zhuanlan.zhihu.com/p/83334559 先約定&#xff0c;本文用pat表示模式串&#xff0c;長度為M&#xff0c;txt表示文本串&#xff0c;長度為N&#xff0c;KMP算法是在txt中查找子串pat&#xff0…

cache初接觸,并利用了DataView

我們在寫代碼的時候,如果數據控件要獲得數據,一般方法,Conn.Open();OleDbCommand cmd;cmd new OleDbCommand(sql, Conn);GridView1.DataSource dbcenter.accessGetDataSet(sql);GridView1.DataBind();Conn.close();但如果多個數據控件要綁定數據,則比較頻繁打開數據庫,效率一…

Java ByteArrayInputStream reset()方法及示例

ByteArrayInputStream類reset()方法 (ByteArrayInputStream Class reset() method) reset() method is available in java.util package. reset()方法在java.util包中可用。 reset() method is used to reset this ByteArrayInputStream to the last time marked position and …

回文數猜想

問題描述&#xff1a; 一個正整數&#xff0c;如果從左向右讀&#xff08;稱之為正序數&#xff09;和從右向左讀&#xff08;稱之為倒序數&#xff09;是一樣的&#xff0c;這樣的數就叫回文數。任取一個正整數&#xff0c;如果不是回文數&#xff0c;將該數與他的倒序數相加…