簡單三層架構(登錄)

1,首先導包
dao

		//獲取數據String username = request.getParameter("username");String password = request.getParameter("password");//傳遞到Service層UserService service = new UserService();//這里的UserService 需要創建到service包下User user = service.login(username,password);//若登錄成功返回登錄的對象//這里的User為domain包下的對象,需要創建實體User

domain

package beyond.web.domain;public class User {//User里面的這些定義都是根據數據庫創建的private int id;private String username;private String password;private String email;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}	

service

package beyond.web.service;import beyond.web.dao.UserDao;
import beyond.web.domain.User;public class UserService {public User login(String username, String password) {UserDao dao = new UserDao();return  dao.login(username,password);//最后返回一個User對象即可}
}
//轉到dao

dao
導包c3p0、commons-dbutils、jstl、mysql-connector、standar
導配置文件c3p0-config.xml

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

導工具DataSourceUtils

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();}}}
package beyond.web.dao;import java.sql.SQLException;import javax.sql.DataSource;import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.taglibs.standard.tag.common.sql.DataSourceUtil;import beyond.web.domain.User;
import beyond.web.utils.DataSourceUtils;public class UserDao {public User login(String username, String password) throws SQLException {//需要導工具導包QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());//查詢,沒有事務控制String sql = "select * from user where username=? and password=?";//sql語句return runner.query(sql, new BeanHandler<User>(User.class) ,username,password);//拋異常}}

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

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

相關文章

通過隱藏option實現select的聯動效果

開始的時候需求是根據一定條件隱藏一部分<option>標簽&#xff0c;類似聯動效果&#xff0c;但是目前的html規范并沒有為<option>提供隱藏的效果&#xff0c;因此常用的設置display或者visibility無效。網上大部分解決方案是刪除<option>節點或<option>…

Java SimpleTimeZone setEndRule()方法與示例

SimpleTimeZone類setEndRule()方法 (SimpleTimeZone Class setEndRule() method) Syntax: 句法&#xff1a; public void setEndRule(int en_mm, int en_dd, int en_time);public void setEndRule(int en_mm, int en_dd, int en_dow, int en_time);public void setEndRule(int…

Linux設備驅動開發--- DMA

文章目錄1 設置DMA映射緩存一致性和DMADMA映射一致映射流式DMA映射2 完成的概念3 DMA引擎API分配DMA從通道設置從設備和控制器指定參數獲取事務描述符提交事務發布待處理DMA請求并等待回調通知4 程序單緩沖區映射分散聚集映射DMA是計算機系統的一項功能&#xff0c;它允許設備在…

類加載器

一、類加載器 1&#xff0c;什么是類加載器&#xff1f; 類加載器就是用來加載字節碼文件 2&#xff0c;類加載器的種類有哪些&#xff1f; 1&#xff09;BootStrap&#xff1a;引導類加載器&#xff1a;加載都是最基礎的文件 2&#xff09;ExtClassLoader&#xff1a;擴展類加…

一個用java讀取XML文件的簡單方法(轉)

XML文件 book.xml <book> <person> <first>Kiran</first> <last>Pai</last> <age>22</age> </person> <person> <first>Bill</first> <last>Gates</last> <age>46</age&g…

Java ObjectStreamField getName()方法與示例

ObjectStreamField類的getName()方法 (ObjectStreamField Class getName() method) getName() method is available in java.io package. getName()方法在java.io包中可用。 getName() method is used to get the name of this ObjectStreamField field. getName()方法用于獲取…

【css】CSS中折疊margin的問題

為什么要翻譯這篇說明&#xff1f;css2本有人已翻譯過&#xff0c;但看一下&#xff0c;很粗糙&#xff08;不是說自己就怎么怎么樣啊&#xff0c;翻譯者真的是很值得敬佩的&#xff01;&#xff09;&#xff0c;近來跟css與xhtml接觸得越來越多&#xff0c;但接觸得越多&#…

算法---鏈表

文章目錄反轉鏈表合并兩個有序鏈表刪除重復元素反轉鏈表 反轉鏈表包括兩種&#xff0c;反轉全部元素或者反轉部分元素。在這里&#xff0c;我們約定&#xff1a;數據元素類型是struct LinkNode&#xff0c;要反轉鏈表的第一個節點是head&#xff0c;head的前面一個節點是pre&a…

SSM

二、環境設置&#xff08;MyEclipse&#xff09; 1&#xff0c;字體設置 window–>Preference->General->Appearance->Colors and Fonts->Basic Text->Font 2&#xff0c;workspace字符集設置 window–>Preference->General->Appearance->W…

IOS NSArray,NSDictionary

小結&#xff1a; NSArray有序的集合&#xff1b; NSDictionary無序的集合&#xff0c;可排序&#xff1b; 增刪改查 ------NSArray----------- create : 1)NSArray *array [NSArray arrayWithObjects:"Henry","Jones", "Susan", "Smith&q…

Java PropertyPermission equals()方法與示例

PropertyPermission類equals()方法 (PropertyPermission Class equals() method) equals() method is available in java.util package. equals()方法在java.util包中可用。 equals() method is used to check whether this object and the given object (ob) are equal or not…

c#配合oracle快速導入excel方法--原創(6萬條記錄5分鐘左右)

原理&#xff1a;用c#采用讀取Excel數據源方式將數據讀入c#的datatable,循環datatable,將datatable中的數據用stringbuilder拼成insert into (字段名) valus (值);每5條插入一個符號&#xff08;作用是將sql字符串限制在4000字符以內&#xff09;&#xff0c;然后將拼成的字符串…

English最俗語法大全

一、先分析兩個長難句 1,It is a truth universally acknowledged that a single man in possession of a good fortune must be in want of a wife. 人們公認這樣一個事實&#xff0c;一個有錢的單身男人一定想要娶一個妻子。 in want of want 想要 university widely 廣泛的…

tfs 內網和外網切換的方法。

C:\Windows\System32\drivers\etc的hosts文件配置一個123.67.128.109 geo-dept-3轉載于:https://www.cnblogs.com/lwflt/archive/2012/07/23/2604731.html

observable_Java Observable countObservers()方法與示例

observable可觀察的類countObservers()方法 (Observable Class countObservers() method) countObservers() method is available in java.util package. countObservers()方法在java.util包中可用。 countObservers() method is used to count the number of observers exists…

設計模式--Strategy 策略模式

所謂策略模式(Strategy Pattern)&#xff0c;就是將策略 (算法) 封裝為一個對象&#xff0c;易于相互替換&#xff0c;如同 USB 設備一樣可即插即用&#xff1b;如果將策略、具體的算法和行為&#xff0c;編碼在某個類或客戶程序內部&#xff0c;將導至事后的修改和擴展不易。 …

HDU-1518 Square dfs+剪枝

該題問給定的棍子能否組成一個正方形。首先我們要判定是否總長度是4的倍數&#xff0c;然后再決定是否存在某條邊大于組合邊長。 搜索的過程中也是可以進行剪枝了。 首先將邊排序&#xff0c;我們可以假定所有的組合邊由大小遞減的邊組成&#xff0c;那么我們在搜索的時候就不用…

英語思維黃金法則

一、謂語單一原則 英文的句子當中&#xff0c;有且只有一套謂語結構。 要想使用多個謂語&#xff0c;有以下三種方法&#xff1a; 1&#xff0c;利用連詞將不同謂語并列起來 2&#xff0c;把其中的一些動詞給降級&#xff08;v-ing v-ed 非謂語動詞&#xff09; 3&#xff0c;…

java getname_Java文件類字符串getName()方法(帶示例)

java getname文件類字符串getName() (File Class String getName()) This method is available in package java.io.File.getName(). 軟件包java.io.File.getName()中提供了此方法。 This method is used to retrieve or return the filename or directory name and represente…

WF中DependencyObject和DependencyProperty的實現

WF中DependencyObject和DependencyProperty的實現 DependencyProperty的Register和RegisterAttached方法&#xff0c;將DependencyProperty存在IDictionary中完成注冊&#xff0c;確保相同name的DependencyProperty在一個ownerType類型中只能有一個。 DependencyObject的GetVal…