📄 balance.java
字号:
package server.servlet;
import java.io.*;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.util.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import server.database.*;
import java.sql.*;
import changeToJson.ToJson;
/**
* @version 1.0
* @author
*/
public class Balance extends HttpServlet implements Servlet {
private Vector splitString(String s,char c,boolean trim){
Vector v = new Vector();
String x = new String();
for(int i = 0; i < s.length(); ++i){
if(s.charAt(i) == c){
if(trim){
x = x.trim();
}
v.add(x);
x = new String();
}
else{
x += s.charAt(i);
}
}
if(trim){
x = x.trim();
}
v.add(x);
return v;
};
/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
//private HashMap list[] = new HashMap[10];
//private Vector list = new Vector();
private HashMap list[];
private String p[] = {"name","price","discount","id"};
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setHeader("Cache-Control","no-cache");
String opNumber = req.getParameter("op");
Operation op=new Operation();
String sql;
PrintWriter out=resp.getWriter();
//System.out.print(opNumber);
if(opNumber.equals("1")){
String ids = req.getParameter("id");
//System.out.print(ids);
Vector id = splitString(ids,' ',false);
//System.out.print(id.size());
list = new HashMap[id.size()];
for(int i = 0; i < id.size(); ++i){
sql = "select name,price,discount,id from Book where id='"+id.get(i)+"'";
try{
ResultSet rs = op.select(sql);
while(rs.next()){
HashMap temp = new HashMap();
for(int j = 0; j < p.length; j++){
temp.put(p[j],rs.getString(p[j]));
}
//list.add(temp);
list[i] = temp;
}
}
catch(ClassNotFoundException e){System.out.print(e);}
catch(SQLException e){System.out.print(e);}
catch(InstantiationException e){System.out.print(e);}
catch(IllegalAccessException e){System.out.print(e);}
}
String Json = ToJson.toJson(list);
//System.out.println(Json);
out.print(Json);
}
else if(opNumber.equals("2")){
String name = req.getParameter("MemberAddress");
sql = "select adress from Consumer where name='"+name+"'";
try{
ResultSet rs = op.select(sql);
if(rs.next()){
String address = rs.getString("adress");
//System.out.print(address);
out.print(address);
}
}
catch(ClassNotFoundException e){System.out.print(e);}
catch(SQLException e){System.out.print(e);}
catch(InstantiationException e){System.out.print(e);}
catch(IllegalAccessException e){System.out.print(e);}
}
else if(opNumber.equals("3")){
String ids = req.getParameter("ids");
String Address = req.getParameter("Address");
String Quantity = req.getParameter("quantity");
String PayMethod = req.getParameter("Pay");
String userID = req.getParameter("userID");
sql = "insert into BalanceForm values('"+ids+"','"+Address+"','"+Quantity+"','"+PayMethod+"','"+userID+"')";
try{
op.IDU(sql);
out.print("true");
}
catch(ClassNotFoundException e){System.out.print(e);}
catch(SQLException e){System.out.print(e);}
catch(InstantiationException e){System.out.print(e);}
catch(IllegalAccessException e){System.out.print(e);}
}
}
/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -