package zhu.jdbc.dao.imp;
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 zhu.jdbc.dao.ITb_User;
import zhu.jdbc.domain.Tb_User;
import zhu.jdbc.unit.UnitMysql;
public class ITb_UserImpI implements ITb_User {
public Connection conn1 = null;
public ResultSet rs = null;
public PreparedStatement ps = null;
// 查询所有的数据
@Override
public List<Tb_User> queryAllData() {
conn1 = UnitMysql.getConnection();// 链接数据库
List<Tb_User> list = new ArrayList<Tb_User>();
try {
String sqlSelect = "select * from users "; // 查询多条数据
ps = conn1.prepareStatement(sqlSelect);
rs = ps.executeQuery();
Tb_User user = null;
while (rs.next()) {
user = new Tb_User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setBirthday(rs.getDate("birthday"));
user.setSex(rs.getBoolean("sex"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
UnitMysql.Close(rs, ps, conn1);
}
return list;
}
// 新增
@Override
public int insertData(Tb_User t) {
conn1 = UnitMysql.getConnection();
int i = 0;
try {
String sqlInsert = "insert into users(name,password,email,birthday,sex) values(?,?,?,?,?) ;";
ps = conn1.prepareStatement(sqlInsert,
PreparedStatement.RETURN_GENERATED_KEYS);
ps.setString(1, t.getName());
ps.setString(2, t.getPassword());
ps.setString(3, t.getEmail());
ps.setDate(4, new java.sql.Date(t.getBirthday().getTime()));
ps.setBoolean(5, t.isSex());
ps.executeUpdate();
rs = ps.getGeneratedKeys();// 得到 最新的 ID
if (rs.next()) {// 是否存在
i = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
UnitMysql.Close(rs, ps, conn1);
}
return i;
}
// 修改
@Override
public int update(Tb_User t) {
conn1 = UnitMysql.getConnection();
int i = 0;
try {
String sqlUpdate = "update users set name=?, password =? ,sex=? where id=?";
ps = conn1.prepareStatement(sqlUpdate);
ps.setString(1, t.getName());
ps.setString(2, t.getPassword());
ps.setBoolean(3, t.isSex());
ps.setInt(4, t.getId());
i = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
UnitMysql.Close(null, ps, conn1);
}
return i;
}
// 删除
@Override
public int delete(int id) {
conn1 = UnitMysql.getConnection();
int i = 0;
try {
String sqlDelete = "delete from users where id=?";
ps = conn1.prepareStatement(sqlDelete);
ps.setInt(1, id);
i = ps.executeUpdate();
if (i == 1) {
return i;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
UnitMysql.Close(null, ps, conn1);
}
return i;
}
// 查询一条数据通过ID
@Override
public Tb_User queryDataById(int id) {
conn1 = UnitMysql.getConnection();
String sql = "select * from users where id=?";
Tb_User user = null;
if (id > 0) {
try {
ps = conn1.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
user = new Tb_User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setBirthday(rs.getDate("birthday"));
user.setSex(rs.getBoolean("sex"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
UnitMysql.Close(null, ps, conn1);
}
}
return user;
}
}
package zhu.jdbc.command;
import java.util.List;
public interface IServiceCommand<T> {
//查询所有的数据
public List<T> queryAllData();
//新增数据
public boolean insertData(T t);
//修改数据
public boolean update(T t);
//删除数据
public boolean delete(int id);
//查询一条数据通过ID
public T queryDataById(int id);
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!-- c标签要使用,那么就必须要有它 -->
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set scope="page" var="url"
value="${pageContext.request.contextPath }"></c:set>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>新增用户</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">
</head>
<body>
<div align="center"
style="width: 400px; position: relative;left:450px">
<form action="${url}/zhu/Servlet_TbUser?who=Insert" method="post">
<h4>新增用户</h4>
姓名: <input type="text" name="name"><br />
密码: <input type="text" name="password"><br />
出生日期 : <input type="text" name="birthday"><br />
性别: <select name="sex">
<option value="0">男</option>
<option value="1">女</option>
</select><br />
<input type="submit" value="新增"/>
<hr />
</form>
</div>
<div align="center"style="width: 400px; position: relative;left:450px;">
<form action="${url}/zhu/Servlet_TbUser?who=queryAll" method="post">
<input type="submit" value="查询所有的数据"/> <br/>
<table border="1" cellspacing="0">
<thead>
<tr><td>ID</td><td>姓名</td><td>密码</td><td>日期</td><td>性别</td><td>操作</td></tr>
</thead>
<tbody>
<c:forEach items="${list}" var="list">
<tr>
<td>${list.id }</td>
<td>${list.name }</td>
<td>${list.password }</td>
<td>${list.birthday }</td>
<td><c:if test="${list.sex==false }">男</c:if>
<c:if test="${list.sex==true }">女</c:if></td>
<td><a href= "${url}/zhu/Servlet_TbUser?who=queryById&id=${list.id}" style='text-decoration:none' onclick='update(this)' >修改 </a>
<a href= "${url}/zhu/Servlet_TbUser?who=delete&id=${list.id}" style='text-decoration:none' >删除</a> </td>
</tr>
</c:forEach>
</tbody>
</table>
<hr />
</form>
</div>
<div align="center"
style="width: 400px; position: relative;left:450px">
<form action="${url}/zhu/Servlet_TbUser?who=update" method="post">
<h4>修改用户</h4>
<input type="hidden"name="id" value="${user.id }"/>
姓名: <input type="text" name="name" value="${user.name }"><br />
密码: <input type="text" name="password" value="${user.password }"><br />
出生日期 : <input type="text" name="birthday" value="${user.birthday }"><br />
性别:<c:if test="${user.sex==false }">
<select name="sex" >
<option value="0">男</option>
<option value="1">女</option>
</select>
</c:if>
<c:if test="${user.sex==true }">
<select name="sex" >
<option value="1">女</option>
<option value="0">男</option>
</select>
</c:if><br />
<input type="submit" value="保存修改"/>
<hr />
</form>
</div>
</body>
</html>
<%@ 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>${msg }</h1> <br>
</body>
</html>