简单三层架构(登录)

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.mzph.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…