package cn.edu.djtu;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
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 cn.edu.djtu.tools.JDBCconnect;
/**
* Servlet implementation class add
*/
@WebServlet("/add")
public class add extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public add() {
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
}
String userString;
String nameString;
String labelString;
String phoneString;
String birthdayString;
String companyString;
String groupString;
String commentString;
int i;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
userString = request.getParameter("user");
nameString = request.getParameter("name");
labelString = request.getParameter("label");
phoneString = request.getParameter("phone");
String yearString = request.getParameter("year");
String monthString = request.getParameter("month");
String dayString= request.getParameter("day");
if(yearString.isEmpty()){
yearString="0000";
}
if(monthString.isEmpty()){
monthString="00";
}
if(dayString.isEmpty()){
dayString="00";
}
birthdayString= yearString +"-"+monthString+"-"+dayString;
companyString= request.getParameter("company");
groupString= request.getParameter("grouping");
commentString= request.getParameter("comment");
String sql = "insert into contacts(user,name,birthday,company,grouping,comment) values(?,?,?,?,?,?)";
//String sqll = "delete from contacts where user = ?";
String sqlString = "insert into phonelist(phonenum,name,user,label) values (?,?,?,?)";
Connection conn = JDBCconnect.connect();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1,userString );
ps.setString(2,nameString);
ps.setString(3,birthdayString );
ps.setString(4,companyString);
ps.setString(5,groupString);
ps.setString(6,commentString);
i = ps.executeUpdate();
}catch (SQLException e) {
request.getRequestDispatcher("add.jsp").include(request, response);
response.getWriter().print("此联系人已存在");
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
if(i != 0){
try{
ps = conn.prepareStatement(sqlString);
ps.setString(1, phoneString);
ps.setString(2,nameString);
ps.setString(3,userString);
ps.setString(4,labelString);
ps.executeUpdate();
request.getRequestDispatcher("homepage.jsp").include(request,response);
response.getWriter().print("添加成功");
} catch (SQLException e) {
request.getRequestDispatcher("add.jsp").include(request,response);
response.getWriter().print("此电话已存在");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
jsp+servlet通讯录
需积分: 9 101 浏览量
2014-10-06
08:58:54
上传
评论
收藏 1.32MB ZIP 举报
lchyzr
- 粉丝: 0
- 资源: 1
最新资源
- Picasso_v3.1 2.ipa
- chromedriver-mac-arm64.zip
- 蓝zapro.apk
- chromedriver-linux64.zip
- UCAS研一深度学习实验-MNIST手写数字识别python源码+详细注释(高分项目)
- 基于Python和PyTorch框架完成的一个手写数字识别实验源码(带MINIST手写数字数据集)+详细注释(高分项目)
- 基于Matlab在MNIST数据集上利用CNN完成手写体数字识别任务,并实现单层CNN反向传播算法+源代码+文档说明(高分项目)
- NVIDIA驱动、CUDA和Pytorch及其依赖
- 基于SVM多特征融合的微表情识别python源码+项目说明+详细注释(高分课程设计)
- html动态爱心代码一(附源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈