Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.Statement;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /*
- * 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.
- */
- /**
- *
- * @author Gianluca
- */
- public class InserisciVinoServlet extends HttpServlet {
- protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
- res.setContentType("text/html");
- int id = 0;
- int successo = 0;
- Connection connection = null;
- Statement statement = null;
- String url = "jdbc:mysql://localhost:3306/enoteca";
- PrintWriter pw = res.getWriter();
- String nome = req.getParameter("nome");
- String produttore = req.getParameter("produttore");
- try {
- int anno = Integer.parseInt(req.getParameter("anno"));
- double prezzo = Double.parseDouble(req.getParameter("prezzo"));
- String query = "INSERT into vino VALUES" + "('" + id + "', '" + nome + "', '" + produttore + "', '" + anno + "', '" + prezzo + "');";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- connection = (com.mysql.jdbc.Connection) DriverManager.getConnection(url, "root", "");
- statement = (com.mysql.jdbc.Statement) connection.createStatement();
- successo = statement.executeUpdate(query);
- pw.println("<html>");
- pw.println("<head>");
- pw.println("<title>Aggiunta avvenuta</title>");
- pw.println("</head>");
- pw.println("<body>");
- pw.println("<div>Aggiunta avvenuta con successso</div>");
- pw.println("<a href='http://apocaliss92.ddns.net/enoteca_home.html'>Torna alla Home</a>");
- pw.println("</body>");
- pw.println("</html>");
- statement.close();
- connection.close();
- } catch (NumberFormatException ex) {
- pw.println("<html>");
- pw.println("<head>");
- pw.println("<title>Errore</title>");
- pw.println("</head>");
- pw.println("<body>");
- pw.println("<div>Aggiunta fallita</div>");
- pw.println("<div>Anno deve contenere un valore intero a 4 cifre e prezzo un valore numerico</div>");
- pw.println("<a href='http://apocaliss92.ddns.net/enoteca_home.html'>Torna alla Home</a>");
- pw.println("</body>");
- pw.println("</html>");
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(InserisciVinoServlet.class.getName()).log(Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- Logger.getLogger(InserisciVinoServlet.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- Logger.getLogger(InserisciVinoServlet.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger.getLogger(InserisciVinoServlet.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement