Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.io.PrintWriter; import java.net.URI; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.simple.*; import org.postgresql.Driver; /** * * @author SuperUser */ public class AuctionServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ Connection conn = null; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { PreparedStatement stmt = null; try { Class.forName("org.postgresql.Driver"); URI dbUri = new URI( "postgres://mvraljhmxcpilo:bKYSixo3rO1Z0cxAmyqMMcK7PG@ec2-75-101-162-243.compute-1.amazonaws.com:5432/d89kgd9u0h1bk3?username=mvraljhmxcpilo&password=bKYSixo3rO1Z0cxAmyqMMcK7PG&ssl.true&sslfactory=org.postgresql.ssl.NonValidatingFactory"); String username = dbUri.getUserInfo().split(":")[0]; String password = dbUri.getUserInfo().split(":")[1]; String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath(); this.conn = DriverManager.getConnection(dbUrl, username, password); String sql; sql = "Select * \n" + " from BidData inner join CustomerData on \n" + "BidData.CustomerID=CustomerData.CustomerID inner join \n" + "ProductData on ProductData.ProductID=BidData.ProductID;"; stmt = conn.prepareStatement(sql); ResultSet rs; rs = stmt.executeQuery(sql); JSONObject dataobj = new JSONObject(); while (rs.next()) { dataobj.put("ProductID", rs.getInt("ProductID")); dataobj.put("ProductName", rs.getString("ProductName")); dataobj.put("SellerPrice", rs.getInt("SellerPrice")); dataobj.put("TimeLimit", rs.getInt("TimeLimit")); dataobj.put("ItemCondition", rs.getString("ItemCondition")); dataobj.put("DateSubmitted", rs.getDate("DateSubmitted")); dataobj.put("ProductPrice", rs.getInt("ProductPrice")); } out.println(dataobj); rs.close(); stmt.close(); conn.close(); } catch (Exception E) { out.println(E.getMessage()); } } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }