jsp部分:javascript
默認主頁:java
index.jsp:sql
<body>
<jsp:forward page="/servlet/Controller">
<jsp:param value="showAllCustomers" name="op"/>
</jsp:forward>
</body>數據庫
顯示客戶信息:jsp
listCustomer.jsp:post
<body>
<div>
<a href="${pageContext.request.contextPath }/addCustomer.jsp">添加</a>
<a href="javascript:delMulti()">刪除</a>
</div>
<c:if test="${empty page.records}">
沒有客戶信息
</c:if>
<c:if test="${!empty page.records}">
<form action="${pageContext.request.contextPath}/servlet/Controller?op=delMulti" method="post">
<table bordercolor="1" border="1" width="80%">
<tr>
<th>選擇</th>
<th>客戶姓名</th>
<th>性別</th>
<th>出生日期</th>
<th>聯繫電話</th>
<th>郵箱</th>
<th>愛好</th>
<th>類型</th>
<th>描述</th>
<th>操做</th>
</tr>
<c:forEach items="${page.records}" var="c" varStatus="vs">
<table bordercolor="1" border="1" width="80%">
<tr class="${vs.index%2==0?'order':'even'}">
<td>
<input type="checkbox" name="ids" value="${ c.id}"/>
</td>
<td>${c.name}</td>
<td>${c.gender}</td>
<td>${c.birthday}</td>
<td>${c.cellphone}</td>
<td>${c.email}</td>
<td>${c.preference}</td>
<td>${c.type}</td>
<td>${c.description}</td>
<td>
<a href="${pageContext.request.contextPath }/servlet/Controller?op=editCustomerUI&customerId=${c.id}">修改</a>
<a href="javascript:delOne('${c.id}')">刪除</a>
</td>
</tr>
</table>
</c:forEach>
</table>
</form>
<%@ include file="/common/page.jsp" %>
</c:if>
<script type="text/javascript">
function delMulti(){
var select=false;
var idsArray=document.getElementsByName("ids");
for(var i=0;i<idsArray.length;i++){
if(idsArray[i].checked){
select=true;
}
}
if(select){
var sure=window.confirm("是否選擇刪除");
if(sure){
document.forms[0].submit();
}
}else{
alert("請先選擇要刪除的客戶");
}
}
function delOne(customerId){
var sure=window.confirm("確認要刪除嗎");
if(sure){
window.location.href="${pageContext.request.contextPath }/servlet/Controller?op=delOneCustomer&customerId="+customerId;
}
}
</script>
</body>this
增長客戶信息頁面:orm
addCustomer.jsp:接口
<form action="${pageContext.request.contextPath}/servlet/Controller?op=addCustomer" method="post">
<table border="1">
<tr>
<td>
姓名:<input type="text" name="name"/>
</td>
</tr>
<tr>
<td>
性別:<input type="radio" name="gender" value="1"/>男
<input type="radio" name="gender" value="0"/>女
</td>
</tr>
<tr>
<td>
出生日期:<input type="text" name="birthday" value="1991-06-11"/>
</td>
</tr>
<tr>
<td>
聯繫電話:<input type="text" name="cellphone" value="187"/>
</td>
</tr>
<tr>
<td>
郵箱:<input type="text" name="email"/>
</td>
</tr>
<tr>
<td>
興趣愛好:<input type="checkbox" name="preferences" value="吃飯" checked="checked"/>吃飯
<input type="checkbox" name="preferences" value="睡覺"/>睡覺
<input type="checkbox" name="preferences" value="學java"/>學java
</td>
</tr>
<tr>
<td>
用戶類型:<input type="radio" name="type" value="普通客戶" checked="checked"/>普通用戶
<input type="radio" name="type" value="vip" />vip用戶
</td>
</tr>
<tr>
<td>
描述:<textarea rows="3" cols="38" name="description"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="保存"/>
</td>
</tr>
</table>
</form>
</body>ip
修改客戶信息頁面:
<body>
<form action="${pageContext.request.contextPath}/servlet/Controller?op=editCustomer" method="post">
<input type="hidden" name="id" value="${c.id}"/>
<table border="1">
<tr>
<td>
姓名:<input type="text" name="name" value="${c.name}"/>
</td>
</tr>
<tr>
<td>
性別:<input type="radio" name="gender" value="1" ${c.gender=='1'?'checked="checked"':''}}"/>男
<input type="radio" name="gender" value="0" ${c.gender=='0'?'checked="checked"':''}/>女
</td>
</tr>
<tr>
<td>
出生日期:<input type="text" name="birthday" value="${c.birthday}"/>
</td>
</tr>
<tr>
<td>
聯繫電話:<input type="text" name="cellphone" value="${c.cellphone}"/>
</td>
</tr>
<tr>
<td>
郵箱:<input type="text" name="email" value="${c.email}"/>
</td>
</tr>
<tr>
<td>
興趣愛好:<input type="checkbox" name="preferences" value="吃飯" ${fn:contains(c.preference,'吃飯')?'checked="checked"':' ' }/>吃飯
<input type="checkbox" name="preferences" value="睡覺" ${fn:contains(c.preference,'睡覺')?'checked="checked"':' '}/>睡覺
<input type="checkbox" name="preferences" value="學java" ${fn:contains(c.preference,'學java')?'checked="checked"':' '}/>學java
</td>
</tr>
<tr>
<td>
用戶類型:<input type="radio" name="type" value="普通客戶" ${c.type=='普通用戶'?'checked="checked"':'' }/>普通用戶
<input type="radio" name="type" value="vip" ${c.type=='普通用戶'?'checked="checked"':'' }/>vip用戶
</td>
</tr>
<tr>
<td>
描述:<textarea rows="3" cols="38" name="description" value="${c.description}"></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="保存"/>
</td>
</tr>
</table>
</form>
</body>
數據庫信息封裝類:
Customer:
public class Customer implements Serializable{
private String id;
private String name;
private String gender;
private Date birthday;
private String cellphone;
private String email;
private String preference;
private String type;
private String description;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getCellphone() {
return cellphone;
}
public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}
public String getPreference() {
return preference;
}
public void setPreference(String preference) {
this.preference = preference;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
dao層:
dao接口:
public interface CustomerDao {
@Deprecated
List<Customer> findAll();
void add(Customer c);
void delete(String customerId);
Customer findById(String customerId);
void update(Customer c);
int getTotalRecordsNum();
List<Customer> findPageCustomers(int offset,int size);
}
dao接口實現:
public class CustomerDaoImpl implements CustomerDao {
@Deprecated
public List<Customer> findAll() {
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("select *from customer");
rs=stmt.executeQuery();
List<Customer> list=new ArrayList<Customer>();
while(rs.next()){
Customer c =new Customer();
c.setId(rs.getString("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setBirthday(rs.getDate("birthday"));
c.setEmail(rs.getString("email"));
c.setCellphone(rs.getString("cellphone"));
c.setPreference(rs.getString("preference"));
c.setType(rs.getString("type"));
c.setDescription(rs.getString("description"));
list.add(c);
}
return list;
}catch(Exception e){
throw new RuntimeException();
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
public void add(Customer c) {
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("insert into customer values(?,?,?,?,?,?,?,?,?)");
stmt.setString(1, c.getId());
stmt.setString(2, c.getName());
stmt.setString(3, c.getGender());
stmt.setDate(4, new java.sql.Date(c.getBirthday().getTime()));
stmt.setString(5, c.getCellphone());
stmt.setString(6, c.getEmail());
stmt.setString(7, c.getPreference());
stmt.setString(8, c.getType());
stmt.setString(9, c.getDescription());
stmt.executeUpdate();
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.release(null, stmt, conn);
}
}
public void delete(String customerId) {
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("delete from customer where id=?");
stmt.setString(1, customerId);
stmt.executeUpdate();
}catch(Exception e){
throw new RuntimeException();
}finally{
JdbcUtil.release(null, stmt, conn);
}
}
public Customer findById(String customerId) {
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("select *from customer where id=?");
stmt.setString(1, customerId);
rs=stmt.executeQuery();
if(rs.next()){
Customer c =new Customer();
c.setId(rs.getString("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setBirthday(rs.getDate("birthday"));
c.setEmail(rs.getString("email"));
c.setCellphone(rs.getString("cellphone"));
c.setType(rs.getString("type"));
c.setDescription(rs.getString("description"));
return c;
}
return null;
}catch(Exception e){
throw new RuntimeException();
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
public void update(Customer c) {
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("update customer set id=?, name=?,gender=?,birthday=?,cellphone=?,email=?,preference=?,type=?,description=?");
stmt.setString(1, c.getId());
stmt.setString(2, c.getName());
stmt.setString(3, c.getGender());
stmt.setDate(4, new java.sql.Date(c.getBirthday().getTime()));
stmt.setString(5, c.getCellphone());
stmt.setString(6, c.getEmail());
stmt.setString(7, c.getPreference());
stmt.setString(8, c.getType());
stmt.setString(9, c.getDescription());
stmt.executeUpdate();
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.release(null, stmt, conn);
}
}
public int getTotalRecordsNum() {
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement("select count(*) from customer");
rs=stmt.executeQuery();
if(rs.next()){
return rs.getInt(1);
}
return 0;
}catch(Exception e){
throw new RuntimeException();
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
public List<Customer> findPageCustomers(int offset, int size) { Connection conn=null; PreparedStatement stmt=null; ResultSet rs=null; try{ conn=JdbcUtil.getConnection(); stmt=conn.prepareStatement("select *from customer limit ?,?"); stmt.setInt(1, offset); stmt.setInt(2, size); rs=stmt.executeQuery(); List<Customer> list=new ArrayList<Customer>(); while(rs.next()){ Customer c =new Customer(); c.setId(rs.getString("id")); c.setName(rs.getString("name")); c.setGender(rs.getString("gender")); c.setBirthday(rs.getDate("birthday")); c.setEmail(rs.getString("email")); c.setCellphone(rs.getString("cellphone")); c.setPreference(rs.getString("preference")); c.setType(rs.getString("type")); c.setDescription(rs.getString("description")); list.add(c); } return list; }catch(Exception e){ throw new RuntimeException(); }finally{ JdbcUtil.release(rs, stmt, conn); } }}