pstmt.setInt(1,Integer.parseInt(ids[i]));
pstmt.executeUpdate();
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
publicLinkget(intid){
Connectionconn=newConnectionUtil().openConnection();
try{
PreparedStatementpstmt=conn.prepareStatement(GET_LINK_SQL);
pstmt.setInt(1,id);
ResultSetrs=pstmt.executeQuery();
if(rs.next()){
//intid=rs.getInt
(1);
Stringname=rs.getString
(2);
Stringurl=rs.getString(3);
Linkl=newLink();
l.setId(id);
l.setName(name);
l.setUrl(url);
returnl;
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
returnnull;
}
publicListlist(){
Connectionconn=newConnectionUtil().openConnection();
try{
Statementstmt=conn.createStatement();
ResultSetrs=stmt.executeQuery(QUERY_LINK_SQL);
Listlist=newArrayList();
while(rs.next()){
intid=rs.getInt
(1);
Stringname=rs.getString
(2);
Stringurl=rs.getString(3);
Linkl=newLink();
l.setId(id);
l.setName(name);
l.setUrl(url);
list.add(l);
}
returnlist;
}catch(SQLExceptione){
e.printStackTrace();
}finally{
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
returnnull;
}
publicvoidupdate(Linkl){
Connectionconn=newConnectionUtil().openConnection();
try{
PreparedStatementpstmt=conn.prepareStatement(UPDATE_LINK_SQL);
pstmt.setString(1,l.getName());
pstmt.setString(2,l.getUrl());
pstmt.setInt(3,l.getId());
pstmt.executeUpdate();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
}
//LinkServlet.java
packagecom.sise.servlet;
importjava.io.IOException;
importjava.util.List;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.sise.dao.LinkDao;
importcom.sise.dao.impl.LinkDaoImpl;
importcom.sise.vo.Link;
publicclassLinkServletextendsHttpServlet{
publicLinkServlet(){
super();
}
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
StringmethodName=request.getParameter("methodName");
if(methodName!
=null&&methodName.equals("add")){
add(request,response);
}elseif(methodName!
=null&&methodName.equals("query")){
query(request,response);
}elseif(methodName!
=null&&methodName.equals("delete")){
delete(request,response);
}elseif(methodName!
=null&&methodName.equals("forward")){
forward(request,response);
}elseif(methodName!
=null&&methodName.equals("update")){
update(request,response);
}else{
return;
}
/*
//响应用户请求
Stringname=request.getParameter("name");
Stringurl=request.getParameter("url");
//调用后台逻辑
LinkDaodao=newLinkDaoImpl();
Linkl=newLink();
l.setName(name);
l.setUrl(url);
dao.add(l);
Listlist=dao.list();
request.setAttribute("LinkList",list);
//转发
request.getRequestDispatcher("/link.jsp").forward(request,response);
*/
}
publicvoidadd(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//响应用户请求
Stringname=request.getParameter("name");
Stringurl=request.getParameter("url");
//调用后台逻辑
LinkDaodao=newLinkDaoImpl();
Linkl=newLink();
l.setName(name);
l.setUrl(url);
dao.add(l);
query(request,response);
}
publicvoidquery(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//调用后台逻辑
LinkDaodao=newLinkDaoImpl();
Listlist=dao.list();
request.setAttribute("LinkList",list);
//转发
request.getRequestDispatcher("/link.jsp").forward(request,response);
}
publicvoiddelete(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//调用后台逻辑
LinkDaodao=newLinkDaoImpl();
String[]ids=request.getParameterValues("ids");
dao.delete(ids);
query(request,response);
}
publicvoidforward(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//调用后台逻辑
Stringid=request.getParameter("id");
LinkDaodao=newLinkDaoImpl();
Linklink=dao.get(Integer.parseInt(id));
request.setAttribute("link",link);
request.getRequestDispatcher("/editLink.jsp")
.forward(request,response);
}
publicvoidupdate(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//响应用户请求
Stringid=request.getParameter("id");
Stringname=request.getParameter("name");
Stringurl=request.getParameter("url");
//调用后台逻辑
LinkDaodao=newLinkDaoImpl();
Linkl=newLink();
l.setId(Integer.parseInt(id));
l.setName(name);
l.setUrl(url);
dao.update(l);
query(request,response);
}
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
//ConnectionUtil.java
packagecom.sise.util;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.util.Properties;
publicclassConnectionUtil{
publicstaticvoidmain(String[]args){
ConnectionUtilcu=newConnectionUtil();
System.out.println(cu.openConnection());
}
publicConnectionopenConnection(){
Stringurl="";
Stringdriver="";
Stringuser="";
Stringpassword="";
Propertiesprop=newProperties();
try{
prop.load(this.getClass().getClassLoader().getResourceAsStream(
"DBConfig.properties"));
driver=prop.getProperty("driver");
url=prop.getProperty("url");
user=