(1)创建 JavaWeb 项目,导入数据库驱动 jar 包,创建对应包存放相关源文件
(2)创建实体类用于出租房屋信息的数据
(3)创建数据访问层,并编写数据库连接和数据访问层代码
(4)创建业务逻辑层,并编写业务逻辑层代码
(5)创建对应 Servlet 处理查询、修改和删除请求处理
(6)正确添加各层之间的调用依赖关系
(1)按照后台管理页面效果图,使用 JSP 创建页面
(2)页面中出租房屋信息数据均来源于数据库,初次加载显示所有出租房屋信息(注意:要求界面使用 JSTL 进行数据展示)
package com.swjd.bean;
public class House {
private int letId;
private String letName;
private String letType;
private int letPrice;
private String letPhone;
private String letAddress;
public House() {
super();
// TODO Auto-generated constructor stub
}
public House(String letName, String letType, int letPrice, String letPhone, String letAddress) {
super();
this.letName = letName;
this.letType = letType;
this.letPrice = letPrice;
this.letPhone = letPhone;
this.letAddress = letAddress;
}
public House(int letId, String letName, String letType, int letPrice, String letPhone, String letAddress) {
super();
this.letId = letId;
this.letName = letName;
this.letType = letType;
this.letPrice = letPrice;
this.letPhone = letPhone;
this.letAddress = letAddress;
}
public int getLetId() {
return letId;
}
public void setLetId(int letId) {
this.letId = letId;
}
public String getLetName() {
return letName;
}
public void setLetName(String letName) {
this.letName = letName;
}
public String getLetType() {
return letType;
}
public void setLetType(String letType) {
this.letType = letType;
}
public int getLetPrice() {
return letPrice;
}
public void setLetPrice(int letPrice) {
this.letPrice = letPrice;
}
public String getLetPhone() {
return letPhone;
}
public void setLetPhone(String letPhone) {
this.letPhone = letPhone;
}
public String getLetAddress() {
return letAddress;
}
public void setLetAddress(String letAddress) {
this.letAddress = letAddress;
}
@Override
public String toString() {
return "House [letId=" + letId + ", letName=" + letName + ", letType=" + letType + ", letPrice=" + letPrice
+ ", letPhone=" + letPhone + ", letAddress=" + letAddress + "]\n";
}
}
BaseDao.java(连接MySQL数据库)
package com.swjd.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/communitydb?CharacterEncoding=utf-8&useUnicode=true","root","123456");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet rs,PreparedStatement ps,Connection conn) {
try {
if (rs!=null) {
rs.close();
}
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
System.out.println(getConn());
}
}
HouseDao.java(接口)
package com.swjd.dao;
import java.util.List;
import com.swjd.bean.House;
public interface HouseDao {
public int add(House house);
public int delete(int id);
public int update(House house,int id);
public List selectAll();
public List selectMoHuByName(String name,String huXing);
}
HouseDaoImpl.java
package com.swjd.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.swjd.bean.House;
import com.swjd.util.BaseDao;
public class HouseDaoImpl implements HouseDao{
@Override
public int add(House house) {
Connection conn = BaseDao.getConn();
PreparedStatement ps = null;
int num = 0;
String sql ="insert into house values(null,?,?,?,?,?)";
try {
ps=conn.prepareStatement(sql);
ps.setString(1, house.getLetName());
ps.setString(2, house.getLetType());
ps.setInt(3, house.getLetPrice());
ps.setString(4, house.getLetPhone());
ps.setString(5, house.getLetAddress());
num = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
BaseDao.close(null, ps, conn);
}
return num;
}
@Override
public int delete(int id) {
Connection conn = BaseDao.getConn();
PreparedStatement ps = null;
int num = 0;
String sql ="delete from house where let_id=?";
try {
ps=conn.prepareStatement(sql);
ps.setInt(1,id);
num = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
BaseDao.close(null, ps, conn);
}
return num;
}
@Override
public int update(House house, int id) {
Connection conn = BaseDao.getConn();
PreparedStatement ps = null;
int num = 0;
String sql ="update house set let_name=?,let_type=?,let_price=?,let_phone=?,let_address=? where let_id=?";
try {
ps=conn.prepareStatement(sql);
ps.setString(1, house.getLetName());
ps.setString(2, house.getLetType());
ps.setInt(3, house.getLetPrice());
ps.setString(4, house.getLetPhone());
ps.setString(5, house.getLetAddress());
ps.setInt(6,id);
num = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
BaseDao.close(null, ps, conn);
}
return num;
}
@Override
public List selectAll() {
List list = new ArrayList();
Connection conn = BaseDao.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from house";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
House house = new House();
house.setLetId(rs.getInt("let_id"));
house.setLetName(rs.getString("let_name"));
house.setLetType(rs.getString("let_type"));
house.setLetPrice(rs.getInt("let_price"));
house.setLetPhone(rs.getString("let_phone"));
house.setLetAddress(rs.getString("let_address"));
list.add(house);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
BaseDao.close(rs, ps, conn);
}
return list;
}
@Override
public List selectMoHuByName(String name,String huXing) {
List list = new ArrayList();
Connection conn = BaseDao.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from house where let_name like '%"+name+"%' and let_type like '%"+huXing+"%'";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
House house = new House();
house.setLetId(rs.getInt("let_id"));
house.setLetName(rs.getString("let_name"));
house.setLetType(rs.getString("let_type"));
house.setLetPrice(rs.getInt("let_price"));
house.setLetPhone(rs.getString("let_phone"));
house.setLetAddress(rs.getString("let_address"));
list.add(house);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
BaseDao.close(rs, ps, conn);
}
return list;
}
public static void main(String[] args) {
HouseDaoImpl houseDaoImpl = new HouseDaoImpl();
System.out.println(houseDaoImpl.selectAll());
}
}
HouseService.java(接口)
package com.swjd.service;
import java.util.List;
import com.swjd.bean.House;
public interface HouseService {
public int add(House house);
public int delete(int id);
public int update(House house, int id);
public List selectAll();
public List selectMoHuByName(String name,String huXing);
}
HouseServiceImpl.java
package com.swjd.service;
import java.util.List;
import com.swjd.bean.House;
import com.swjd.dao.HouseDaoImpl;
public class HouseServiceImpl implements HouseService{
HouseDaoImpl houseDaoImpl = new HouseDaoImpl();
@Override
public int add(House house) {
int jg = houseDaoImpl.add(house);
return jg;
}
@Override
public int delete(int id) {
int jg = houseDaoImpl.delete(id);
return jg;
}
@Override
public int update(House house, int id) {
int jg = houseDaoImpl.update(house, id);
return jg;
}
@Override
public List selectAll() {
List list = houseDaoImpl.selectAll();
return list;
}
@Override
public List selectMoHuByName(String name,String huXing) {
List list = houseDaoImpl.selectMoHuByName(name,huXing);
return list;
}
}
Index.jsp(首页)
Insert title here
小区名称:
户型:
一室一厅
一室一卫
三室两卫
两室两厅
三室一厅
所有
ID
小区名城
小区户型
租金(元/月)
联系电话
小区地址
操作
${xiaoQu.letId }
${xiaoQu.letName }
${xiaoQu.letType }
${xiaoQu.letPrice }
${xiaoQu.letPhone }
${xiaoQu.letAddress }
修改 删除
update.jsp
Insert title here
function reset1(){
var a = document.getElementByName('xiaoQu')[0];
var b = document.getElementByName('huXing')[0];
var c = document.getElementByName('zuJin')[0];
var d = document.getElementByName('dianHua')[0];
var e = document.getElementByName('diZhi')[0];
a.value="";
b.value="";
c.value="";
d.value="";
e.value="";
}
修改出租房屋信息
小区名称:
户型:
租金:
联系电话:
小区地址:
<input name="ids" value="" style="display:none;">
IndexServlet.java
package com.swjd.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swjd.bean.House;
import com.swjd.service.HouseServiceImpl;
/**
* Servlet implementation class IndexServlet
*/
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public IndexServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
HouseServiceImpl houseServiceImpl = new HouseServiceImpl();
List list = houseServiceImpl.selectAll();
request.setAttribute("list",list);
request.getRequestDispatcher("Index.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
IndexServlet2.java
package com.swjd.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swjd.bean.House;
import com.swjd.service.HouseServiceImpl;
/**
* Servlet implementation class IndexServlet2
*/
public class IndexServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public IndexServlet2() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("xiaoQuName");
String huXing= request.getParameter("huXing");
HouseServiceImpl houseServiceImpl = new HouseServiceImpl();
List list = houseServiceImpl.selectMoHuByName(name, huXing);
request.setAttribute("list", list);
request.setAttribute("name", name);
request.setAttribute("huXing", huXing);
request.getRequestDispatcher("Index.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
UpdateServlet.java
package com.swjd.controller;
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 com.swjd.bean.House;
import com.swjd.service.HouseServiceImpl;
/**
* Servlet implementation class UpdateServlet
*/
public class UpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String xiaoQu = request.getParameter("xiaoQu");
String huXing = request.getParameter("huXing");
int price = Integer.parseInt(request.getParameter("zuJin"));
String phone = request.getParameter("dianHua");
String dizhi = request.getParameter("diZhi");
PrintWriter out = response.getWriter();
int id = Integer.parseInt(request.getParameter("ids"));
HouseServiceImpl houseServiceImpl = new HouseServiceImpl();
House house = new House(xiaoQu,huXing,price,phone,dizhi);
int jg = houseServiceImpl.update(house, id);
if (jg>0) {
out.print("alert('修改成功');location.href='IndexServlet';");
}else {
out.print("alert('修改失败');location.href='IndexServlet';");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
DeleteServlet.java
package com.swjd.controller;
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 com.swjd.service.HouseServiceImpl;
/**
* Servlet implementation class DeleteServlet
*/
public class DeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
int id = Integer.parseInt(request.getParameter("id1"));
HouseServiceImpl houseServiceImpl = new HouseServiceImpl();
PrintWriter out = response.getWriter();
int jg = houseServiceImpl.delete(id);
if (jg>0) {
out.print("alert('删除成功');location.href='IndexServlet';");
}else {
out.print("alert('删除失败');location.href='IndexServlet';");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
需要用到的jar包
运行结果
可以实现修改,删除和条件查询