用户管理者的表
在java数据库中建admin表
表中有数据id,name,sex,role
create table admin(
id int(11) not null primary key auto_increment,
name varchar(40) unique,
pass varchar(12),
sex enum('男','女'),
role int(11)
)engine=innodb default charset=utf8
转换成实体操作
-
在MyEclipse里创建包,www.csdn.net.domain
-
在包里创建Admin类
package www.hbsi.net.domain;
import java.io.Serializable;
/**
* 把一个表admin映射(mapping)成Class类
*
* @author redarmy
*
*/
//可序列化的接口
public class Admin implements Serializable {
/**
* id int(11) not null primary key auto_increment,---calss 成员属性
* name varchar(40) unique,
* pass varchar(12),
* sex enum('男','女'),
* role int(11)
*
* 表 class
* int int,Integer
* varchar Stirng
* enum Stirng
* role int
*/
private static final long serialVersionUID = 1L;
// 把admin表中的字段映射成Admin类的成员属性(字段类型的转换如下 int --int Integer,varchar--String )
private int id;
private String name;
private String pass;
private String sex;
private int role;
//构造器
public Admin() {
super();
// TODO Auto-generated constructor stub
}
public Admin(String name, String pass, String sex, int role) {
super();
this.name = name;
this.pass = pass;
this.sex = sex;
this.role = role;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
@Override
public String toString() {
return "Admin [id=" + id + ", name=" + name + ", pass=" + pass
+ ", role=" + role + ", sex=" + sex + "]";
}
}
创建数据库对象
-
创建包:www.csdn.net.util
-
创建类:DBConn
package www.csdn.net.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBConn { private static Connection conn=null; public static Connection getConn(){ if(conn==null){ try { //驱动程序jar //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //解决数据库连接乱码问题 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java?user=root&password=123456&useUnicode=true&characterEncoding=UTF-8"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return conn; } /** * 释放rs、pstmt的资源 * @param rs * @param pstmt */ public static void realse(ResultSet rs, PreparedStatement pstmt) { if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(pstmt!=null){ try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
-
加载驱动
-
在lib下添加压缩文件mysql,自动加载
-
创建包:www.csdn.net.junit
-
创建类:DBConnTest.java
-
-
测试数据库
package www.csdn.net.junit; import org.junit.Test; import www.csdn.net.util.DBConn; public class DBConnTest { @Test public void getConn(){ System.out.println(DBConn.getConn()); } }
规定了对admin表的所有的操作,转换成对实体Admin的操作
创建包:www.csdn.net.dao
创建接口:AdminDao.java
package www.hbsi.net.dao;
import java.util.List;
import www.hbsi.net.domain.Admin;
/**
* 规定了对admin表的所有的操作,转换成对实体Admin的操作
*
* @author redarmy
*
*/
public interface AdminDao {
/**
* 插入操作
*
* @param entity
* @return
*/
public boolean insert(Admin entity);
/**
* 删除操作
*
* @param entity
* @return
*/
public boolean delete(Admin entity);
/**
* 根据主键删除操作
*
* @param id
* @return
*/
public boolean delete(int id);
/**
* 根据主键查询的操作
*
* @param id
* @return
*/
public Admin findById(int id);
/**
* 查询所有的操作
*
* @return
*/
public List<Admin> findAll();
/**
* 根据用户名和密码查询用户
*
* @param name
* @return
*/
public Admin login(String name, String pass);
/**
* 获取当前页信息
* @param nowpage
* @return
*/
public List<Admin> findNowPageInfo(int nowpage);
/**
* 获取总记录数
* @return
*/
public int getCountSize();
}
实现接口里的方法
包:www.csdn.net.dao
创建类:AdminDaoImpl.java
package www.hbsi.net.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import www.csdn.net.util.DBConn;
import www.hbsi.net.domain.Admin;
public class AdminDaoImpl implements AdminDao {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
public boolean delete(Admin entity) {
// TODO Auto-generated method stub
return false;
}
public boolean delete(int id) {
// TODO Auto-generated method stub
return false;
}
public List<Admin> findAll() {
// TODO Auto-generated method stub
return null;
}
public Admin findById(int id) {
// TODO Auto-generated method stub
return null;
}
public List<Admin> findNowPageInfo(int nowpage) {
// TODO Auto-generated method stub
return null;
}
public int getCountSize() {
// TODO Auto-generated method stub
return 0;
}
public boolean insert(Admin entity) {
// TODO Auto-generated method stub
return false;
}
public Admin login(String name, String pass) {
// 1、声明返回值变量
Admin entity = null;
// 2、声明sql语句
String sql = "select id,name,pass,sex,role from admin where name=? and pass=? ";
// 3、获取连接对象
conn = DBConn.getConn();
try {
// 4、根据sql语句获取预处理对象
pstmt = conn.prepareStatement(sql);
// 5、为占位符赋值
int index = 1;
pstmt.setString(index++, name);
pstmt.setString(index++, pass);
// 6、执行查询
rs = pstmt.executeQuery();
// 7、判断rs.next(); if,while
if (rs.next()) {
// 实例化对象
entity = new Admin();
// 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性
entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值
// 赋值给了Class Admin 的id属性
entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表
// name字段的值 赋值给了 Class
// Admin 的 name属性
entity.setPass(rs.getString("pass"));
entity.setSex(rs.getString("sex"));
entity.setRole(rs.getInt("role"));
}
// 8、释放资源
DBConn.realse(rs, pstmt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return entity;
}
}
测试AdminDaoImpl类
在www.csdn.net.jutil里新建类AdminDaoImplTest
package www.csdn.net.junit;
import org.junit.Test;
import www.csdn.net.util.AdminDaoImpl;
import www.hbsi.net.dao.AdminDao;
import www.hbsi.net.domain.Admin;
public class AdminDaoImolTest {
//AdminDao接口,AdminDaoImpl是接口的实现类,所有一个接口实现类的对象可以转换成接口的对象(多态性)。
private AdminDao adminDao = new AdminDaoImpl();
@Test
public void login(){
Admin entity = adminDao.login("Jack", "123");
System.out.println(entity.toString());
}
}
Login界面
-
在WebRoot下新建文件夹message
-
在message下新建Login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">
<form action="${pageContext.request.contextPath}/login.do"
method="post">
用户名
<input type="text" name="name" />
<br />
密 码:
<input type="password" name="pass" />
<br />
<input type="submit" value="登陆">
</form>
</div>
</body>
</html>
<br>
</body>
</html>
业务层
-
新建包:www.csdn.net.service
-
创建接口:AdminService
-
创建类:AdminServiceImpl
AdminService.java
package www.csdn.net.service;
import java.util.List;
import www.csdn.net.domain.Admin;
/**
* 规定了对admin表的所有的操作,转换成对实体Admin的操作
*
* @author redarmy
*
*/
public interface AdminService {
/**
* 插入操作
*
* @param entity
* @return
*/
public boolean insert(Admin entity);
/**
* 删除操作
*
* @param entity
* @return
*/
public boolean delete(Admin entity);
/**
* 根据主键删除操作
*
* @param id
* @return
*/
public boolean delete(int id);
/**
* 根据主键查询的操作
*
* @param id
* @return
*/
public Admin findById(int id);
/**
* 查询所有的操作
*
* @return
*/
public List<Admin> findAll();
/**
* 根据用户名和密码查询用户
*
* @param name
* @return
*/
public Admin login(String name, String pass);
/**
* 获取当前页信息
* @param nowpage
* @return
*/
public List<Admin> findNowPageInfo(int nowpage);
/**
* 获取总记录数
* @return
*/
public int getCountSize();
}
AdminServiceImpl.java
package www.csdn.net.service;
import java.util.List;
import www.csdn.net.dao.AdminDao;
import www.csdn.net.dao.AdminDaoImpl;
import www.csdn.net.domain.Admin;
public class AdminServiceImpl implements AdminService {
private AdminDao adminDao = new AdminDaoImpl();
public boolean delete(Admin entity) {
return adminDao.delete(entity);
}
public boolean delete(int id) {
return adminDao.delete(id);
}
public List<Admin> findAll() {
// TODO Auto-generated method stub
return adminDao.findAll();
}
public Admin findById(int id) {
return adminDao.findById(id);
}
public boolean insert(Admin entity) {
return adminDao.insert(entity);
}
public Admin login(String name, String pass) {
return adminDao.login(name, pass);
}
public List<Admin> findNowPageInfo(int nowpage) {
// TODO Auto-generated method stub
return adminDao.findNowPageInfo(nowpage);
}
public int getCountSize() {
// TODO Auto-generated method stub
return adminDao.getCountSize();
}
}
servlet控制器:login.do
新建包:www.csdn.net.servlet
新建servlet:Login.do
LoginServlet.java
package www.csdn.net.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import www.csdn.net.domain.Admin;
import www.csdn.net.service.AdminService;
import www.csdn.net.service.AdminServiceImpl;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 获取用户名和密码
String name = request.getParameter("name");
String pass = request.getParameter("pass");
// 创建业务对象
AdminService adminService = new AdminServiceImpl();
Admin entity = adminService.login(name, pass);
if (entity == null) {
response.sendRedirect("./message/login.jsp");
} else {
request.setAttribute("entity", entity);
request.getRequestDispatcher("./message/index.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
Index主界面
-
在message下新建index.jsp
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>欢迎${entity.name}登陆后台界面</h1>
</body>
</html>
程序运行结果如下:
在地址栏输入
http://localhost:8080/login/message/Login.jsp