图片内容是所有的包名,文件名。
1.创建(M)模型
package oa.bean; public class User { private String userName;
private String passWord; 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;
} @Override
public String toString() {
return "User [userName=" + userName + ", passWord=" + passWord + "]";
} }
2.创建DAO层
创建Dao层接口 package oa.dao; import oa.bean.User; /**
* @author Administrator
*
*/
public interface IUserDao { public boolean login(User user); public boolean insert(User entity);
} 2.创建Dao层实现类 package oa.dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import oa.Util.JDBCuntl;
import oa.bean.User; public class UserDaoImpl implements IUserDao { // 封装数据库操作属性 Connection conn = null; PreparedStatement pstm = null; ResultSet rs = null; // 第一步:声明返回值变量
boolean falg = false; // 登录
@Override
public boolean login(User user) { // 第二步:获取连接对象
try {
conn = JDBCuntl.getConnection();
// 第三步:声明sql语句
String sql = "select * from user"; // 第四步:根据sql语句创建预处理对象
pstm = conn.prepareStatement(sql); // 第五步:执行查询
rs = pstm.executeQuery(); // 第六步:判断
while (rs.next()) {
String uname = rs.getString(1);
String upwd = rs.getString(2); if (uname.equals(user.getUserName())
&& upwd.equals(user.getPassWord())) {
return true;
}
} } catch (Exception e) { e.printStackTrace();
} finally {
// 第八步:释放资源
try {
JDBCuntl.close(rs, pstm, conn);
} catch (SQLException e) { e.printStackTrace();
}
} // 判断
/*
* if("admin".equals(user.getUserName()) &&
* "123456".equals(user.getPassWord())){ return true; }else{ return
* false; }
*/ return false;
} // 注册
@Override
public boolean insert(User entity) { try {
// 第二步:获取连接对象
conn = JDBCuntl.getConnection(); // 第三步:声明sql语句(插入)
String sql = "insert into user(userName,passWord) values(?,?)"; // 第四步:根据sql语句出创建对象
pstm = conn.prepareStatement(sql); // 第五步:为占位符赋值
int index = 1;
pstm.setObject(index++, entity.getUserName());
pstm.setObject(index++, entity.getPassWord()); // 第六步:执行语句
int i = pstm.executeUpdate(); // 第七步:判断执行
if (i > 0) {
falg = true;
} } catch (Exception e) { e.printStackTrace();
} finally {
try {
JDBCuntl.close(null, pstm, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return falg;
} }
3.创建Service层
1.创建service层接口 package oa.service; import oa.bean.User; public interface IUserService { public boolean login(User user); public boolean insert(User entity);
} 2.创建service的实现类 package oa.service; import oa.bean.User;
import oa.dao.IUserDao;
import oa.dao.UserDaoImpl; public class UserServiceImpl implements IUserService { // 封装实体操作类
private IUserDao uDao = new UserDaoImpl(); @Override
public boolean login(User user) { return uDao.login(user);
} @Override
public boolean insert(User entity) { return uDao.insert(entity);
} }
4.建立一个工具链接数据库
package oa.Util; import java.sql.*;
import java.util.Properties;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream; /**
* 获取数据库连接对象的工具类
* @author Administrator
* @version 1.0
*/
public class JDBCuntl { private static String driverClass = null;
private static String url = null;
private static String user = null;
private static String password = null; //通过静态块获取jdbc.properties中的数据库驱动信息并初始化静态成员变量
static{
Properties props = new Properties(); InputStream is = JDBCuntl.class.getClassLoader().getResourceAsStream("jdbc.properties"); try {
props.load(is); driverClass = props.getProperty("jdbc.driver");
url = props.getProperty("jdbc.url");
user = props.getProperty("jdbc.user");
password = props.getProperty("jdbc.password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /*driverClass="com.mysql.jdbc.Driver";
url="jdbc:mysql://localhost:3306/user";
user="root";
password="1";*/ } /**
* 根据获取的数据库驱动信息来创建数据库连接对象并返回
* @return 连接对象
* @throws Exception
*/
public static Connection getConnection() throws Exception{
Connection conn = null; Class.forName(driverClass); conn = DriverManager.getConnection(url, user, password); return conn; } /**
* 统一关闭JDBC资源的方法
* @param rs 结果集对象
* @param stmt 语句对象
* @param conn 连接对象
* @throws SQLException
*/
public static void close(ResultSet rs,Statement stmt,Connection conn) throws SQLException{
if(rs != null){
rs.close();
rs = null;
} if(stmt != null){
stmt.close();
stmt = null;
} if(conn != null){
conn.close();
conn = null;
}
} } 其中的文件是方便读取数据库,也方便更改数据库
文件内容是: jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/user
jdbc.user=root
jdbc.password=1 如果连接数据库有问题,可以测试数据库
测试代码: package oa.Util; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; public class TestUtil { /**
* @param args
*/
public static void main(String[] args) { try {
Connection conn=JDBCuntl.getConnection();
PreparedStatement psmt=conn.prepareStatement("select * from user");
ResultSet rs=psmt.executeQuery(); while (rs.next()) { System.out.println(rs.getString(1)+"\t"+rs.getString(2));
}
JDBCuntl.close(rs, psmt, conn);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }
5.创建Servlet(控制器 C)
package oa.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LoginSuccess extends HttpServlet { /**
* Constructor of the object.
*/
public LoginSuccess() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter(); out.println("登录成功!欢迎你:" + request.getParameter("user")); out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
6.创建登录页面,注册页面
<!DOCTYPE html>
<html>
<head>
<title>Login.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript">
function register() { window.location = "register.html"; }
</script> </head> <body>
<h1>欢迎使用XXXX点餐系统</h1>
<form action="LoginServlet" method="post">
用户名:<input type="text" name="user"><br> <br> 密码:<input
type="password" name="pwd"><br> <br> <input
type="submit" value="提交"> <input
type="button" value="注册" onclick="register()"> <input
type="reset" value="重置">
</form>
</body>
</html>
7.创建一些跳转页面(成功,失败页面)
成功页面 此处用servlet实现 response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter(); out.println("登录成功!欢迎你:" + request.getParameter("user")); out.flush();
out.close(); 失败页面 response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter(); out.print("登录失败!用户名或者密码错误!"); out.flush();
out.close();
8.创建注册页面和注册成功失败页面
package oa.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import oa.bean.User;
import oa.service.IUserService;
import oa.service.UserServiceImpl; public class RegisterService extends HttpServlet { /**
* Constructor of the object.
*/
public RegisterService() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter(); // 获取用户输入的数据
String userName = request.getParameter("user");
String passWord = request.getParameter("pwd"); request.setCharacterEncoding("UTF-8"); // 创建实体类
User entity = new User(); // 为实体对象赋值
entity.setUserName(userName);
entity.setPassWord(passWord); // 调用Service层实现用户登录业务
IUserService uService = new UserServiceImpl(); boolean falg = uService.insert(entity); if (falg==true) {
response.sendRedirect("rSuccess.html");
}else{
response.sendRedirect("rFail.html");
} out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
注册页面 <!DOCTYPE html>
<html>
<head>
<title>register.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body>
<form action="RegisterService" method="post">
用户姓名:<input type="text" name="user"><br/><br/> 密码:<input type="password" name="pwd"><br/><br/>
<input type="submit" name="提交" value="提交"><br/> </form>
</body>
</html> 成功页面 <!DOCTYPE html>
<html>
<head>
<title>rSuccess.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body>
<font color="red" size="6">注册成功</font>
</body>
</html> 失败页面 <!DOCTYPE html>
<html>
<head>
<title>rFail.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body>
<font color="red" size="6">注册失败</font>
</body>
</html>
此处一定要在webRoot下的
WEB—INF下的
lib文件内将数据库jar包导入《一定要导入jar包》
MVC简单分层思想(连接数据库)的更多相关文章
-
【blade的UI设计】理解前端MVC与分层思想
前言 最近校招要来了,很多大三的同学一定按捺不住心中的焦躁,其中有期待也有彷徨,或许更多的是些许担忧,最近在开始疯狂的复习了吧 这里小钗有几点建议给各位: ① 不要看得太重,关心则乱,太紧张反而表现不 ...
-
初识mvc分层思想
首先要清楚的是: mvc是一种设计模式,一种分层思想,没有具体的技术与之对应,无论是js还是java或者其他的技术都可以运用. 既然是分层那么这些层都有哪些职责呢? View层(界面层): 为用户展示 ...
-
JavaWeb开发中的分层思想(一)
JavaWeb开发分层思想(一) 一.认识DAO.Service.Controller层 DAO(Data Access Object) 1.直接看英文意思就是"数据访问对象",也 ...
-
PHP中MVC的编程思想浅谈
我相信这样的文章已经被写烂了,但是我今天还是愿意冒着风险把自己的经验与大家分享一下.纯属原创,我也没什么可保留,希望对新手有帮助,有说的什么不对的地方,欢迎大家伙吐槽. 什么是MVC? 简单的说就是将 ...
-
从Microsoft.AspNet.Identity看微软推荐的一种MVC的分层架构
Microsoft.AspNet.Identity简介 Microsoft.AspNet.Identity是微软在MVC 5.0中新引入的一种membership框架,和之前ASP.NET传统的mem ...
-
javaWeb中MVC的编程思想示例
没有学习MVC之前我只写了一个Servlet类(Note_List.java),分层之后,我将这个类分成了5个类(NoteDao.java,,NoteDaoImpl.java,,NoteService ...
-
程序设计分层思想和DAO设计模式的开发
无论是一个应用程序项目还是一个Web项目,我们都可以按照分层思想进行程序设计.对于分层,当下最流行划分方式是:表现层+控制层+业务层+数据层.其中,业务层和数据层被统称为后台业务层,而表现层和控制层属 ...
-
RebotFrameWork的分层思想
RebotFrameWork的分层思想 分层思想,就是通过关键字调用的方法,把大杂烩的代码根据脚本特征拆封开来,提高代码的灵活性和清晰度,从而也让一些组件层内容可扩展.可复用.可维护. 解析下目录结构 ...
-
Robot Framework自动化测试(四)--- 分层思想
谈到Robot Framework 分层的思想,就不得不提“关键字驱动”. 关键字驱动: 通过调用的关键字不同,从而引起测试结果的不同. 在上一节的selenium API 中所介绍的方法其实就是关 ...
随机推荐
-
字符串拷贝函数strcpy写法_转
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
-
C3p0/元数据/内省-Bean/自定义查询封装类/查询/分页
c3p0连接池(C3p0连接池,只有当用户获取连接时,才会包装Connection.) 第一步:导入c3p0 第二步:在classpath目录下,创建一个c3p0-config.xml 第三步:创建工 ...
-
Struts2中的ActionContext
ActionContext(Action上下文) ActionContext介绍 通过上面用户注册例子的学习,我们知道Xwork与Web无关性,我们的Action不用去依赖于任何Web容器,不用和那些 ...
-
Android 8.0 功能和 API
Android 8.0 为用户和开发者引入多种新功能.本文重点介绍面向开发者的新功能. 用户体验 通知 在 Android 8.0 中,我们已重新设计通知,以便为管理通知行为和设置提供更轻松和更统一的 ...
-
Web站点错误提示页面和默认访问页面设置
1.asp.net 定制简单的错误处理页面 通常web应用程序在发布后,为了给用户一个友好界面和使用体验,都会在错误发生时跳转至一个自定义的错误页面,而不是asp.net向用户暴露出来的详细的异常列表 ...
-
java之集合Collection 详解之4
package cn.itcast_04; public class Student { private String name; private int age; public Student() ...
-
使用maven的插件进行maven项目的打包
1 maven项目打包的插件有3种 maven-jar-plugin maven-assembly-plugin maven-shade-plugin 2 maven-jar-plugin 现在要新增 ...
-
原创:Eclipse安装Eclipse Color Themes插件后,编辑器背景颜色被改变
如题,卸载Eclipse Color Themes插件后,背景颜色还是白色,蛋疼,修改.metadata\.plugins\org.eclipse.core.runtime\.settings中的or ...
-
Future设计模式
一.什么是Future模型: Future模式是多线程开发中非常常见的一种设计模式,它的核心思想是异步调用.这类似我们网上订餐订座,只要一个电话,客服就告诉我们已经预定成功(实际客服MM啥都还没做好) ...
-
POJ 1154
#include<iostream> #include<stdio.h> #define MAXN 20 using namespace std; int DFS(int i, ...