修改功能变成了添加功能 SQL语句好像也没什么问题吧...不知道哪里出了问题,运行也没有报错
Servelet层
package com.dgut.conctrol;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse
import com.dgut.model.Client;
import com.dgut.model.Contact;
import com.dgut.service.contactService;
import com.dgut.service.loginAndClientService;
import com.mysql.jdbc.Connection;
@WebServlet("*.go")
public class ContactServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ContactServlet() {
super();
}
@Override
public void init() throws ServletException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String servletPath = request.getServletPath();
System.out.println(servletPath);
String methodName = servletPath.substring(1, servletPath.length()-3);
try {
Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public void addContact(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String clientName=new String(request.getParameter("clientName").getBytes("ISO-8859-1"),"UTF-8");
String contactName=new String(request.getParameter("contactName").getBytes("ISO-8859-1"),"UTF-8");
String contactContents=new String(request.getParameter("contactContents").getBytes("ISO-8859-1"),"UTF-8");
String contactStart=new String(request.getParameter("contactStart").getBytes("ISO-8859-1"),"UTF-8");
String contactEnd=new String(request.getParameter("contactEnd").getBytes("ISO-8859-1"),"UTF-8");
String StaffName=new String(request.getParameter("StaffName").getBytes("ISO-8859-1"),"UTF-8");
contactService contactService=new contactService();
Contact c=new Contact(clientName,contactName,contactContents,contactStart,contactEnd,StaffName);
try{
contactService.addcontact(c);
response.sendRedirect("/project02/contactManage/lookContact.jsp");
}
catch(Exception e){
e.printStackTrace();
}
}
@SuppressWarnings("static-access")
public void deleteContact(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String contactName=new String(request.getParameter("contactName").getBytes("ISO-8859-1"),"UTF-8");
Contact c=new Contact(contactName, null, null, null, null, null);
contactService contactService=new contactService();
if( contactService.isContactExist(contactName)) {
contactService.deletecontact(c);
response.sendRedirect(" contactManage/lookContact.jsp");
} else {
request.setAttribute("ErrorMessage", "合同不存在!");
request.getRequestDispatcher("contactManage/deleteContact.jsp").forward(request, response);
return;
}
}
@SuppressWarnings("static-access")
public void updateClient(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String clientName=new String(request.getParameter("clientName").getBytes("ISO-8859-1"),"UTF-8");
String contactName=new String(request.getParameter("contactName").getBytes("ISO-8859-1"),"UTF-8");
String contactContents=new String(request.getParameter("contactContents").getBytes("ISO-8859-1"),"UTF-8");
String contactStart=new String(request.getParameter("contactStart").getBytes("ISO-8859-1"),"UTF-8");
String contactEnd=new String(request.getParameter("contactEnd").getBytes("ISO-8859-1"),"UTF-8");
String StaffName=new String(request.getParameter("StaffName").getBytes("ISO-8859-1"),"UTF-8");
contactService contactService=new contactService();
Contact c=new Contact(clientName,contactName,contactContents,contactStart,contactEnd,StaffName);
if( contactService.isContactExist(contactName)) {
contactService.updatecontact(c);
response.sendRedirect("contactManage/lookContact.jsp");
} else {
request.setAttribute("ErrorMessage", "合同名称不存在!");
request.getRequestDispatcher("contactManage/updateContact.jsp").forward(request, response);
return;
}
}
}
Dao层
package com.dgut.dao;
import java.sql.Connection;
import java.sql.SQLException;
import com.dgut.model.Contact;
public interface contactDao {
public Long numOfcontact(Connection connection,String contacttName)
throws SQLException;
public void addcontact(Connection connection,Contact c)
throws SQLException;
public void updatecontact(Connection connection,Contact c)
throws SQLException;
public void deletecontact(Connection connection,Contact c)
throws SQLException;
}
MPL
package com.dgut.daoImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.dgut.dao.contactDao;
import com.dgut.jdbc.JdbcTools;
import com.dgut.model.Contact;
public class contactDaoImpl implements contactDao {
@Override
public Long numOfcontact(Connection connection, String contactName) throws SQLException {
Long result=(long)-1;
PreparedStatement statement=null;
ResultSet rs=null;
try {
String sql="select count(*) from contact where contactName=? ";
statement=connection.prepareStatement(sql);
statement.setObject(1, contactName);
rs =statement.executeQuery();
if(rs.next()) {
result=rs.getLong(1);
}
}
catch(Exception e) {
e.printStackTrace();
}finally {
JdbcTools.releaseResource(rs, statement, null);
}
return result;
// TODO Auto-generated method stub
}
@Override
public void addcontact(Connection connection, Contact c)
throws SQLException {
PreparedStatement statement=null;
try {
String sql="insert into contact(clientName,contactName,contactContents,contactStart,contactEnd,StaffName) values(?,?,?,?,?,?)";
statement=connection.prepareStatement(sql);
statement.setObject(1, c.getClientName());
statement.setObject(2,c.getContactName());
statement.setObject(3,c.getContactContents());
statement.setObject(4, c.getContactStart());
statement.setObject(5, c.getContactEnd());
statement.setObject(6, c.getStaffName());
statement.executeLargeUpdate();
}
catch(Exception e) {
e.printStackTrace();
}finally {
JdbcTools.releaseResource(null, statement, null);
}
}
@Override
public void updatecontact(Connection connection, Contact c)
throws SQLException {
PreparedStatement statement=null;
try {
String sql="update contact set clientName=?,contactContents=?,contactStart=?,contactEnd=?,StaffName=? where contactName=?";
statement=connection.prepareStatement(sql);
statement.setObject(1,c.getClientName());
statement.setObject(2,c.getContactContents());
statement.setObject(3,c.getContactStart());
statement.setObject(4,c.getContactEnd());
statement.setObject(5,c.getStaffName());
statement.setObject(6,c.getContactName());
statement.executeLargeUpdate();
System.out.println(sql);
}
catch(Exception e) {
e.printStackTrace();
}finally {
JdbcTools.releaseResource(null, statement, null);
}
}
// TODO Auto-generated method stub
@Override
public void deletecontact(Connection connection, Contact c)
throws SQLException {
PreparedStatement statement=null;
try {
String sql="delete from contact where contactName=?";
statement=connection.prepareStatement(sql);
statement.setObject(1, c.getContactName());
statement.executeLargeUpdate();
System.out.println(sql);
}
catch(Exception e) {
e.printStackTrace();
}finally {
JdbcTools.releaseResource(null, statement, null);
}
}
// TODO Auto-generated method stub
}
Service层
package com.dgut.service;
import java.sql.Connection;
import java.sql.SQLException;
import com.dgut.dao.contactDao;
import com.dgut.daoImpl.contactDaoImpl;
import com.dgut.jdbc.JdbcTools;
import com.dgut.model.Contact;
public class contactService {
private static contactDao contactDao;
public contactService() {
super();
contactDao=new contactDaoImpl();
}
public static boolean isContactExist(String contactName) {
boolean result =false;
Connection connection=null;
try {
connection=JdbcTools.getConnection();
Long count =contactDao.numOfcontact(connection, contactName);
if(count>0)
result=true;
}catch(Exception e) {
e.printStackTrace();
}finally {
JdbcTools.releaseResource(null,null, connection);
}
return result;
}
public void addcontact(Contact c) {
Connection connection=null;
try {
connection=JdbcTools.getConnection();
contactDao.addcontact(connection, c);
}catch(Exception e) {
e.printStackTrace();
}finally {
JdbcTools.releaseResource(null,null, connection);
}
}
public static void deletecontact(Contact c) {
Connection connection=null;
try {
connection=JdbcTools.getConnection();
contactDao.deletecontact(connection, c);
}catch(Exception e) {
e.printStackTrace();
}finally {
JdbcTools.releaseResource(null,null, connection);
}
}
public void updatecontact(Contact c) {
Connection connection=null;
try {
connection=JdbcTools.getConnection();
contactDao.updatecontact(connection, c);
}catch(Exception e) {
e.printStackTrace();
}finally {
JdbcTools.releaseResource(null,null, connection);
}
}
}
Contact类
package com.dgut.model;
public class Contact {
private String clientName;
private String contactName;
private String contactContents;
private String contactStart;
private String contactEnd;
private String StaffName;
public Contact(){
super();
}
public Contact(String clientName,String contactName,String contactContents,String contactStart,String contactEnd,String StaffName) {
this.clientName=clientName;
this.contactName=contactName;
this.contactContents=contactContents;
this.contactStart=contactStart;
this.contactEnd=contactEnd;
this.StaffName=StaffName;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName=clientName;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName=contactName;
}
public String getContactContents() {
return contactContents;
}
public void setContactContents(String contactContents) {
this.contactContents=contactContents;
}
public String getContactStart() {
return contactStart;
}
public void setContactStart(String contactStart) {
this.contactStart=contactStart;
}
public String getContactEnd() {
return contactEnd;
}
public void setContactEnd(String contactEnd) {
this.contactEnd=contactEnd;
}
public String getStaffName() {
return StaffName;
}
public void setStaffName(String StaffName) {
this.StaffName=StaffName;
}
public String toString() {
return "Contact [clientName=" + clientName + ", contactName=" + contactName + ", contactContents=" + contactContents +
",contactStart="+contactStart+",contactEnd"+contactEnd+",StaffName"+StaffName+"]";
}
}
JDBC
package com.dgut.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcTools {
private static ComboPooledDataSource cpds = null;
public static Connection getConnectionFromDataSource() throws Exception{
if(cpds==null)
cpds = new ComboPooledDataSource("com.xxx.jdbc");
return cpds.getConnection();
}
public static void closeDataSource(){
cpds.close();
cpds = null;
}
public static Connection getConnection() throws Exception {
//String driverClass = "com.mysql.cj.jdbc.Driver";
String driverClass = "com.mysql.jdbc.Driver";
Class.forName(driverClass);
//String url = "jdbc:mysql://localhost:3306/jdbc?verifyServerCertificate=false&useSSL=false";
String url = "jdbc:mysql://localhost:3306/eims";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
}
public static void releaseResource(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void releaseResource(ResultSet rs, Statement statement, Connection connection) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
# addContact.jsp
<%--
Document : addClient
Created on : 2012-3-23, 21:09:51
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加合同信息</title>
</head>
<body bgcolor="lightgreen">
<table align="center"width="500" >
<tr>
<td><a href="http://localhost:8080/project02/contactManage/lookContact.jsp">合同查询</a></td>
<td>合同添加</td>
<td><a href="http://localhost:8080/project02/contactManage/updateContact.jsp">合同修改</a></td>
<td><a href="http://localhost:8080/project02/contactManage/deleteContact.jsp">合同删除</a></td>
</tr>
<form action="/project02/addContact.go" method="post">
</table>
<br>
<hr>
<br>
<table align="center"width="300" >
<tr>
<th colspan="6" align="center">添加合同信息</th>
</tr>
<tr>
<td>客户姓名</td>
<td><input type="text" name="clientName"/></td>
</tr>
<tr>
<td>合同名称</td>
<td><input type="text" name="contactName"/></td>
</tr>
<tr>
<td>合同内容</td>
<td><input type="text" name="contactContents"/></td>
</tr>
<tr>
<td>合同生效日期</td>
<td><input type="text" name="contactStart"/></td>
</tr>
<tr>
<td>合同有效期</td>
<td><input type="text" name="contactEnd"/></td>
</tr>
<tr>
<td>业务员</td>
<td><input type="text" name="StaffName"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input name="sure"type="submit"value="确认">
<input name="clear"type="reset"value="取消">
</td>
</tr>
</table>
</form>
</body>
</html>
deleteContact.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
合同删除
<table align="center"width="500" >
<tr>
<td><a href="http://localhost:8080/project02/contactManage/lookContact.jsp">合同查询</a></td>
<td><a href="http://localhost:8080/project02/contactManage/addContact.jsp">合同添加</a></td>
<td><a href="http://localhost:8080/project02/contactManage/updateContact.jsp">合同修改</a></td>
<td>合同删除</td>
</tr>
</table>
<br>${requestScope.ErrorMessage}
<hr>
<br>
<table align="center">
<tr>
<th colspan="2">删除合同信息</th>
</tr>
<form action="http://localhost:8080/project02/deleteContact.go" method="post">
<tr>
<td>合同名称</td>
<td><input type="text" name="contactName"/>输入要删除的合同名称</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="sure" value="确认"/>
<input name="clear"type="reset"value="取消"/>
</td>
</tr>
</table>
</form>
</body>
lookContact.jsp
<%--
Document : lookClient
Created on : 2012-3-23, 20:10:11
Author : Administrator
--%>
<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
合同查询
合同查询 | 合同添加 | 合同修改 | 合同删除 |
查看合同信息 | |||||
---|---|---|---|---|---|
客户姓名 | 合同名称 | 合同内容 | 合同生效日期 | 合同有效期 | 业务员 |
<%=rs.getString("clientName")%> | <%=rs.getString("contactName")%> | <%=rs.getString("contactContents")%> | <%=rs.getString("contactStart")%> | <%=rs.getString("contactEnd")%> | <%=rs.getString("StaffName")%> |
updateContact.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
修改合同信息
<table align="center"width="500" >
<tr>
<td><a href="http://localhost:8080/project02/contactManage/lookContact.jsp">合同查询</a></td>
<td><a href="http://localhost:8080/project02/contactManage/addContact.jsp">合同添加</a></td>
<td>合同修改</td>
<td><a href="http://localhost:8080/project02/contactManage/deleteContact.jsp">合同删除</a></td>
</tr>
<form action="/project02/addContact.go" method="post">
</table>
<br>
<hr>
<br>
<table align="center"width="300" >
<tr>
<th colspan="6" align="center">修改合同信息</th>
</tr>
<form action="http://localhost:8080/project02/updaContact.go" method="post">
<tr>
<td>客户姓名</td>
<td><input type="text" name="clientName"/></td>
</tr>
<tr>
<td>合同名称</td>
<td><input type="text" name="contactName"/></td>
</tr>
<tr>
<td>合同内容</td>
<td><input type="text" name="contactContents"/></td>
</tr>
<tr>
<td>合同生效日期</td>
<td><input type="text" name="contactStart"/></td>
</tr>
<tr>
<td>合同有效期</td>
<td><input type="text" name="contactEnd"/></td>
</tr>
<tr>
<td>业务员</td>
<td><input type="text" name="StaffName"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input name="sure"type="submit"value="确认">
<input name="clear"type="reset"value="取消">
</td>
</tr>
</table>
</form>
</body>