Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import com.mysql.jdbc.Connection;
  2. import com.mysql.jdbc.Statement;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. /*
  14. * To change this license header, choose License Headers in Project Properties.
  15. * To change this template file, choose Tools | Templates
  16. * and open the template in the editor.
  17. */
  18. /**
  19. *
  20. * @author Gianluca
  21. */
  22. public class InserisciVinoServlet extends HttpServlet {
  23.  
  24. protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
  25. res.setContentType("text/html");
  26. int id = 0;
  27. int successo = 0;
  28. Connection connection = null;
  29. Statement statement = null;
  30. String url = "jdbc:mysql://localhost:3306/enoteca";
  31. PrintWriter pw = res.getWriter();
  32. String nome = req.getParameter("nome");
  33. String produttore = req.getParameter("produttore");
  34. try {
  35. int anno = Integer.parseInt(req.getParameter("anno"));
  36. double prezzo = Double.parseDouble(req.getParameter("prezzo"));
  37. String query = "INSERT into vino VALUES" + "('" + id + "', '" + nome + "', '" + produttore + "', '" + anno + "', '" + prezzo + "');";
  38. Class.forName("com.mysql.jdbc.Driver").newInstance();
  39. connection = (com.mysql.jdbc.Connection) DriverManager.getConnection(url, "root", "");
  40. statement = (com.mysql.jdbc.Statement) connection.createStatement();
  41. successo = statement.executeUpdate(query);
  42.  
  43. pw.println("<html>");
  44. pw.println("<head>");
  45. pw.println("<title>Aggiunta avvenuta</title>");
  46. pw.println("</head>");
  47. pw.println("<body>");
  48. pw.println("<div>Aggiunta avvenuta con successso</div>");
  49. pw.println("<a href='http://apocaliss92.ddns.net/enoteca_home.html'>Torna alla Home</a>");
  50. pw.println("</body>");
  51. pw.println("</html>");
  52. statement.close();
  53. connection.close();
  54. } catch (NumberFormatException ex) {
  55. pw.println("<html>");
  56. pw.println("<head>");
  57. pw.println("<title>Errore</title>");
  58. pw.println("</head>");
  59. pw.println("<body>");
  60. pw.println("<div>Aggiunta fallita</div>");
  61. pw.println("<div>Anno deve contenere un valore intero a 4 cifre e prezzo un valore numerico</div>");
  62. pw.println("<a href='http://apocaliss92.ddns.net/enoteca_home.html'>Torna alla Home</a>");
  63. pw.println("</body>");
  64. pw.println("</html>");
  65. } catch (ClassNotFoundException ex) {
  66. Logger.getLogger(InserisciVinoServlet.class.getName()).log(Level.SEVERE, null, ex);
  67. } catch (InstantiationException ex) {
  68. Logger.getLogger(InserisciVinoServlet.class.getName()).log(Level.SEVERE, null, ex);
  69. } catch (IllegalAccessException ex) {
  70. Logger.getLogger(InserisciVinoServlet.class.getName()).log(Level.SEVERE, null, ex);
  71. } catch (SQLException ex) {
  72. Logger.getLogger(InserisciVinoServlet.class.getName()).log(Level.SEVERE, null, ex);
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement