1 package servlet;2 import javax.servlet.*;3 import java.io.*;4 import javax.servlet.http.*;5 import java.sql.*;6 public class TradeServlet extends HttpServlet7 {8 private String url;9 private String user;10 private String password;11
12 public void init() throws ServletException13 {14 ServletContext sc=getServletContext();15 String driverClass=sc.getInitParameter("driverClass");16 url=sc.getInitParameter("url");17 user=sc.getInitParameter("user");18 password=sc.getInitParameter("password");19 try20 {21 Class.forName(driverClass);22 }23 catch(ClassNotFoundException ce)24 {25 throw new ServletException("加载数据库驱动失败!");26 }27 }28
29 public void doGet(HttpServletRequest req, HttpServletResponse resp)30 throws ServletException,IOException31 {32 Connection conn=null;33 Statement stmt=null;34 PreparedStatement pstmt=null;35 ResultSet rs=null;36
37 resp.setContentType("text/html;charset=gb2312");38 PrintWriter out=resp.getWriter();39
40 req.setCharacterEncoding("gb2312");41
42 String userid=req.getParameter("userid");43 String quantity=req.getParameter("quantity");44
45 if(null==userid || userid.equals("") ||46 null==quantity || quantity.equals(""))47 {48
49 out.println("错误的请求参数");50 out.close();51 }52 else53 {54 try55 {56 conn=DriverManager.getConnection(url,user,password);57
58 conn.setAutoCommit(false);59 conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);60
61 stmt=conn.createStatement();62 rs=stmt.executeQuery("select price,amount from bookinfo where id=3");63 rs.next();64 float price=rs.getFloat(1);65 int amount=rs.getInt(2);66
67 int num=Integer.parseInt(quantity);68 if(amount>=num)69 {70 pstmt=conn.prepareStatement("update bookinfo set amount = ? where id = 3");71 pstmt.setInt(1,amount-num);72 pstmt.executeUpdate();73 }74 else75 {76 out.println("您所购买的图书库存数量不足。");77 out.close();78 return;79 }80 pstmt=conn.prepareStatement("select balance from account where userid = ?");81 pstmt.setString(1,userid);82 rs=pstmt.executeQuery();83
84 rs.next();85 float balance=rs.getFloat(1);86
87 float totalPrice=price*num;88
89 if(balance>=totalPrice)90 {91 pstmt=conn.prepareStatement("update account set balance = ? where userid = ?");92 pstmt.setFloat(1,balance-totalPrice);93 pstmt.setString(2,userid);94 pstmt.executeUpdate();95 }96 else97 {98 conn.rollback();99 out.println("您的余额不足。");100 out.close();101 return;102 }103 conn.commit();104 out.println("交易成功!");105 out.close();106 }107 catch(SQLException se)108 {109 if(conn!=null)110 {111 try112 {113 conn.rollback();114 }115 catch(SQLException ***)116 {117 ***.printStackTrace();118 }119 }120 se.printStackTrace();121 }122 finally123 {124 if(rs!=null)125 {126 try127 {128 rs.close();129 }130 catch(SQLException se)131 {132 se.printStackTrace();133 }134 rs=null;135 }136 if(stmt!=null)137 {138 try139 {140 stmt.close();141 }142 catch(SQLException se)143 {144 se.printStackTrace();145 }146 stmt=null;147 }148 if(pstmt!=null)149 {150 try151 {152 pstmt.close();153 }154 catch(SQLException se)155 {156 se.printStackTrace();157 }158 pstmt=null;159 }160 if(conn!=null)161 {162 try163 {164 conn.close();165 }166 catch(SQLException se)167 {168 se.printStackTrace();169 }170 conn=null;171 }172 }173 }174 }175
176 public void doPost(HttpServletRequest req, HttpServletResponse resp)177 throws ServletException,IOException178 {179 doGet(req,resp);180 }181 }