Advertisement
Domy131097

Untitled

Dec 19th, 2018
130
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None
  1. package mljac.database;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.SQLException;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. /**
  16. * Servlet implementation class DbInsert
  17. */
  18. @WebServlet("/DbInsert")
  19. public class DbInsert extends HttpServlet {
  20. private static final long serialVersionUID = 1L;
  21.  
  22. /**
  23. * @see HttpServlet#HttpServlet()
  24. */
  25. public DbInsert() {
  26. super();
  27. // TODO Auto-generated constructor stub
  28. }
  29.  
  30. /**
  31. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  32. */
  33. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  34. String ime, probao;
  35. double cijena;
  36.  
  37. ime = request.getParameter("ime");
  38. probao = request.getParameter("probao");
  39. cijena = Double.parseDouble(request.getParameter("cijena"));
  40.  
  41. Connection conn = null;
  42. PreparedStatement statement = null;
  43.  
  44. try {
  45. Class.forName("org.sqlite.JDBC");
  46. conn = DriverManager.getConnection("jdbc:sqlite:c:\\tomcat\\db\\Mljac.db");
  47. String sql = "INSERT INTO Hrana (ime, cijena, probao) VALUES('"+ ime +"', '" + cijena +"','"+ probao +"')";
  48. statement = conn.prepareStatement(sql);
  49. int result = statement.executeUpdate();
  50. if (result == 1)
  51. response.sendRedirect("http://localhost:8080/FastFoodMljac/FoodList");
  52. else
  53. response.sendRedirect("http://localhost:8080/FastFoodMljac/Error");
  54. }catch(SQLException se) {
  55. System.out.println("SQL Exception:" + se.getMessage());
  56. }catch(Exception e) {
  57. System.out.println("Exception:" + e.getMessage());
  58. }finally {
  59. try {
  60. if(conn != null)
  61. conn.close();
  62. }catch(SQLException se) {
  63. System.out.println("Excpetion in closing connection " + se.getMessage());
  64. }
  65. }
  66.  
  67. }
  68.  
  69. /**
  70. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  71. */
  72. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  73. // TODO Auto-generated method stub
  74. doGet(request, response);
  75. }
  76.  
  77. }
  78.  
  79. <!DOCTYPE html>
  80. <html>
  81. <head>
  82. <meta charset="UTF-8">
  83. <title>Fast Food Mljac</title>
  84. </head>
  85. <body>
  86. <h1>Dodajte novo jelo</h1>
  87. <form action="DbInsert">
  88. Unesite naziv jela: <input type="text" name="ime"><br>
  89. Unesite cijenu jela: <input type="number" name="cijena" step="0.01"><br>
  90. <label>Jeste li probali jelo?</label>
  91. <select name="probao">
  92. <option value='NE'>Ne</option>
  93. <option value='DA'>Da</option>
  94. </select>
  95. <br><br>
  96. <input type="submit" value="Dodaj">
  97. </form>
  98. </body>
  99. </html>
Advertisement
RAW Paste Data Copied
Advertisement