Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. import java.util.*;
  5. import java.sql.*;
  6.  
  7. public class AddPizza extends HttpServlet{
  8.     public void doPost(HttpServletRequest request,
  9.               HttpServletResponse response)
  10.     throws ServletException, IOException {
  11.  
  12.     Connection conn =null; // Create connection object
  13.    
  14.     String database = "whalleys"; // Name of database
  15.     String user = "whalleys"; //
  16.     String password = "crasmael";
  17.     String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk/" + database;
  18.  
  19.     String docType =
  20.         "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+
  21.         "Transitional//EN\">\n";
  22.  
  23.     response.setContentType("text/html");
  24.     PrintWriter out = response.getWriter();
  25.     out.println(docType);
  26.  
  27.     try{
  28.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  29.     } catch(Exception e) {
  30.         System.err.println(e);
  31.     }
  32.    
  33.     // connecting to database
  34.     try{
  35.         conn = DriverManager.getConnection(url, user, password);
  36.        
  37.     }
  38.     catch(SQLException se) {
  39.         System.err.println(se);
  40.     }
  41.     // Create select statement and execute it
  42.     // get form data
  43.     String name = request.getParameter("name");
  44.     String toppings = request.getParameter("toppings");
  45.     String price = request.getParameter("price");
  46.            
  47.    
  48.     try{
  49.         String updateSQL = "insert into pizzas (name, toppings, price) values ('" + name + "', '" + toppings + "', " + price + ") ";
  50.        
  51. /*this is an update query where we insert values into an existing table, look at mysqlcrpizzas.sql for an idea*/
  52.        
  53.         System.err.println("DEBUG: Query: " + updateSQL);
  54.         Statement stmt = conn.createStatement();
  55.         int rows = stmt.executeUpdate(updateSQL);
  56.        
  57.             conn.close();
  58.     } catch(SQLException se) {
  59.         System.err.println(se);
  60.     }
  61.  
  62.    
  63.     out.println("<html><body><h1>Database updated</h1></body></html>");
  64.    
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement