model层:
package com.pxb.model;
public class Guard {
private Integer guardId;
private String username;
private String password;
private String guardName;
private String sex;
private String tel;
private String buildingName;
@Override
public String toString() {
return "Guard{" +
"guardId=" + guardId +
", username='" + username + '\'' +
", password='" + password + '\'' +
", guardName='" + guardName + '\'' +
", sex='" + sex + '\'' +
", tel='" + tel + '\'' +
", buildingName='" + buildingName + '\'' +
'}';
}
public String getBuildingName() {
return buildingName;
}
public void setBuildingName(String buildingName) {
this.buildingName = buildingName;
}
public Guard(String username, String password) {
this.username = username;
this.password = password;
}
public Integer getGuardId() {
return guardId;
}
public void setGuardId(Integer guardId) {
this.guardId = guardId;
}
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 getGuardName() {
return guardName;
}
public void setGuardName(String guardName) {
this.guardName = guardName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Guard() {
}
}
util层:
package com.pxb.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbUtil {
private static final String username = "root";
private static final String password = "";
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/db_housing?useUnicode=true&characterEncoding=UTF8";
private Connection conn;
static {
try {
Class.forName(driver);
System.out.println("数据库加载驱动成功!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("数据库驱动加载异常!");
}
}
public Connection getConnection(){
try {
conn = DriverManager.getConnection(url,username,password);
System.out.println("数据库连接成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}
return conn;
}
public static void main(String[] args) {
DbUtil d = new DbUtil();
System.out.println(d.getConnection());
}
}
dao层:
package com.pxb.dao;
import com.pxb.model.Guard;
import com.pxb.util.DbUtil;
import com.pxb.util.StringUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class GuardDao {
public List<Guard> guardList(Connection conn) throws SQLException {
List<Guard> guardList = new ArrayList<Guard>();
StringBuffer sb = new StringBuffer("select * from guard t1");
PreparedStatement pstmt = conn.prepareStatement(sb.toString().replaceFirst("and","where"));
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Guard guard1 = new Guard();
guard1.setGuardId(rs.getInt("guardId"));
guard1.setUsername(rs.getString("username"));
guard1.setPassword(rs.getString("password"));
guard1.setGuardName(rs.getString("guardName"));
guard1.setBuildingName(rs.getString("buildingName"));
guard1.setSex(rs.getString("sex"));
guard1.setTel(rs.getString("tel"));
guardList.add(guard1);
}
return guardList;
}
public static void main(String[] args) {
GuardDao guardDao=new GuardDao();
Connection con=null;
DbUtil dbUtil=new DbUtil();
con=dbUtil.getConnection();
try {
List<Guard> list=guardDao.guardList(con);
System.out.println(list.toString());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
servlet层:
package com.pxb.servlet;
import com.pxb.dao.GuardDao;
import com.pxb.model.Guard;
import com.pxb.util.DbUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public class GuardServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
DbUtil dbUtil = new DbUtil();
GuardDao guardDao = new GuardDao();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
System.out.println("=>进入servlet");
Guard guard = new Guard();
Connection conn = null;
conn = dbUtil.getConnection();
try {
List<Guard> guardList = guardDao.guardList(conn);
System.out.println(guardList.toString());
request.setAttribute("guardList",guardList);
request.setAttribute("mainPage","admin/guard.jsp");
request.getRequestDispatcher("mainAdmin.jsp").forward(request,response);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
jsp页面:
<table class="table table-hover text-center">
<tr>
<%-- <th width="100" style="text-align:left; padding-left:20px;">编号</th>--%>
<%-- <th width="10%">用户名</th>--%>
<%-- <th width="50">姓名</th>--%>
<th>编号</th>
<th>用户名</th>
<th>姓名</th>
<th>性别</th>
<th>楼层</th>
<th>电话</th>
<%-- <th width="10%">录入时间</th>--%>
<th width="310">操作</th>
</tr>
<volist name="list" id="vo">
<c:forEach varStatus="i" var="guard" items="${guardList}">
<tr>
<%-- <td style="text-align:left; padding-left:20px;"><input type="checkbox" name="id[]" value="" />--%>
<%-- 1</td>--%>
<td>${guard.guardId}</td>
<td>${guard.username}</td>
<td>${guard.guardName}</td>
<td>${guard.sex}</td>
<td>${guard.buildingName}</td>
<td>${guard.tel}</td>
<%-- <td>2016-07-01</td>--%>
<td><div class="button-group"> <a class="button border-main" href="add.html"><span class="icon-edit"></span> 修改</a> <a class="button border-red" href="javascript:void(0)" onclick="return del(1,1,1)"><span class="icon-trash-o"></span> 删除</a> </div></td>
</tr>
</c:forEach>
<%-- <tr>--%>
<%-- <td style="text-align:left; padding:19px 0;padding-left:20px;"><input type="checkbox" id="checkall"/>--%>
<%-- 全选 </td>--%>
<%-- <td colspan="7" style="text-align:left;padding-left:20px;"><a href="javascript:void(0)" class="button border-red icon-trash-o" style="padding:5px 15px;" onclick="DelSelect()"> 删除</a> </td>--%>
<%-- </tr>--%>
</volist>
</table>