Advertisement
Guest User

DBCoffee

a guest
Dec 2nd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3.  
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9.  
  10. import java.sql.*;
  11.  
  12. import javax.sql.*;
  13. import javax.naming.*;
  14. import javax.ejb.*;
  15. import java.util.Properties;
  16.  
  17.  
  18. @WebServlet("/Dbaccess.do")
  19. public class Dbaccess extends HttpServlet {
  20. private static final long serialVersionUID = 1L;
  21.  
  22. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  23.  
  24. response.setContentType ("text/html");
  25. PrintWriter out = response.getWriter ();
  26.  
  27. out.println ("<?xml version=\"1.0\"?>");
  28. out.println ("<!DOCTYPE html PUBLIC \"-//W3C//DTD" + "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + "/TR/xhtml1-strict.dtd\">");
  29. out.println ("<html xmlns=\"http://www.w3.org/1999/html1\">");
  30.  
  31. out.println ("<head>");
  32. out.println ("<title>DBCoffee Example</title>");
  33. out.println ("</head>");
  34.  
  35. out.println ("<body>");
  36. out.println ("<h1> Coffees Table </h1>");
  37. out.println ("<table>");
  38. out.println ("<tr>");
  39. out.println ("<td>COF_NAME</td>");
  40. out.println ("<td>SUP_ID</td>");
  41. out.println ("<td>PRICE</td>");
  42. out.println ("<td>SALES</td>");
  43. out.println ("<td>TOTAL</td>");
  44. out.println ("</tr>");
  45.  
  46. Connection con = null;
  47. Statement stmt = null;
  48.  
  49. try{String driver = "com.mysql.jdbc.Driver";
  50. Class.forName(driver).newInstance();
  51. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/coffee?user=root&password=nikola94");
  52. stmt = con.createStatement ();
  53. ResultSet rs = stmt.executeQuery("select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES");
  54.  
  55. while (rs.next()){
  56. String coffeeName = rs.getString("COF_NAME");
  57. int supplierID = rs.getInt("SUP_ID");
  58. float price = rs.getFloat("PRICE");
  59. int sales = rs.getInt("SALES");
  60. int total = rs.getInt("TOTAL");
  61. out.println ("<tr>");
  62. out.println ("<td>" + coffeeName + "</td>");
  63. out.println ("<td>" + supplierID + "</td>");
  64. out.println ("<td>" + price + "</td>");
  65. out.println ("<td>" + sales + "</td>");
  66. out.println ("<td>" + total + "</td>");
  67. out.println ("</tr>");
  68. }
  69. rs.close ();
  70. stmt.close ();
  71. con.close();
  72. }
  73. catch (SQLException e){System.err.println(e.getMessage());}
  74. catch (ClassNotFoundException e){System.err.println(e.getMessage());}
  75. catch (IllegalAccessException e){System.err.println(e.getMessage());}
  76. catch (InstantiationException e){System.err.println(e.getMessage());}
  77.  
  78. out.println ("</table>");
  79. out.println ("</body>");
  80.  
  81. out.println ("</html>");
  82.  
  83. out.close ();
  84. }
  85.  
  86.  
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement