📄 customerfriendgroup.java
字号:
package com.mole.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mole.struts.dao.DBConnector;
public class customerFriendGroup extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
private HttpServletRequest request;
private HttpServletResponse response;
private DBConnector dbc = new DBConnector();
/**
* Constructor of the object.
*/
public customerFriendGroup() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType(CONTENT_TYPE);
this.request = request;
this.response = response;
String action = request.getParameter("action");
if ("1".equals(action))
getFriendGroup(request.getParameter("cid"));
else if ("2".equals(action))
addFriend(request.getParameter("cid"), request.getParameter("fid"),
request.getParameter("gid"));
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
private void getFriendGroup(String cid) {
if (cid != null) {
ArrayList<Object[]> al = null;
String sql = "SELECT [Name],[ID] FROM [CustomerFriendGroup] WHERE [CustomerID]=?";
try {
al = dbc.executeQuery(sql, cid);
output(al);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void addFriend(String cid, String fid, String gid) {
if (cid != null && fid != null && gid != null) {
int result = 0;
ArrayList<Object[]> al = null;
String sql1 = "SELECT * FROM [CustomerFriend] WHERE [CustomerID]=? AND [FriendID]=?";
String sql2 = "INSERT INTO [CustomerFriend]([CustomerID],[FriendID],[GroupID]) VALUES(?,?,?)";
try {
al = dbc.executeQuery(sql1, cid, fid);
if (al.size() > 0) {
output(-1);
} else {
result = dbc.executeUpdate(sql2, cid, fid, gid);
output(result);
}
} catch (Exception e) {
e.printStackTrace();
output(-2);
}
}
}
private void output(ArrayList<Object[]> al) {
if (al != null && al.size() > 0) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < al.size(); i++) {
Object[] obj = al.get(i);
for (Object o : obj) {
sb.append(o.toString() + ",");
}
}
String str = sb.substring(0, sb.length() - 1);
try {
PrintWriter out = response.getWriter();
out.print(str);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void output(int result) {
try {
PrintWriter out = response.getWriter();
out.print(result);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -