Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. @GetMapping("/nuevo")
  2. public static String nuevoGet() {
  3. return "nuevoProducto";
  4. }
  5.  
  6. @PostMapping("/nuevo")
  7. public static String nuevoPost(@RequestParam String nombre,
  8. @RequestParam String descripcion, @RequestParam String precio,
  9. @RequestParam String tipo, @RequestParam String imagen, Model template) throws SQLException {
  10.  
  11. if (nombre.equals("") || descripcion.equals("") || precio.equals("")
  12. || tipo.equals("") || imagen.equals("")) {
  13.  
  14. template.addAttribute("nombre", nombre);
  15. template.addAttribute("descripcion", descripcion);
  16. template.addAttribute("precio", precio);
  17. template.addAttribute("tipo", tipo);
  18. template.addAttribute("imagen", imagen);
  19. template.addAttribute("MensajeError",
  20. "Disculpe, hubo un error en la carga de su producto, por favor, revise los datos");
  21.  
  22. return "nuevoProducto";
  23.  
  24. }else{
  25.  
  26. Connection connection;
  27. connection = DriverManager.getConnection(
  28. "jdbc:postgresql://localhost:5432/SakuraCakes", "postgres",
  29. "admin");
  30.  
  31. PreparedStatement ps = connection
  32. .prepareStatement("INSERT INTO productos(nombre, descripcion, precio, tipo, imagen)"
  33. + "VALUES(?, ?, ?, ?, ?);");
  34.  
  35. Double p = Double.parseDouble(precio);
  36.  
  37. ps.setString(1, nombre);
  38. ps.setString(2, descripcion);
  39. ps.setDouble(3, p);
  40. ps.setString(4, tipo);
  41. ps.setString(5, imagen);
  42. ResultSet result = ps.executeQuery();
  43. Producto d = new Producto(result.getString("nombre"),
  44. result.getString("descripcion"),
  45. result.getDouble("precio"), result.getString("tipo"), result.getString("imagen"));
  46.  
  47. template.addAttribute("productos", d);
  48. return "listo";
  49. }
  50. }
  51.  
  52. <form action="/nuevo" method="POST" id="form">
  53. <label for="nombre">Nombre</label>
  54. <input name="nombre" type="text" placeholder="nombre..." th:value="${nombre}"></input>
  55. <label for="descripcion">Descripcion</label>
  56. <textarea name="descripcion" class="form-control" placeholder="Describe tu producto..." th:value="${descripcion}" rows="5" maxlength="300"></textarea>
  57. <label for="precio">Precio</label>
  58. <input name="precio" type="text" placeholder="precio..." th:value="${precio}"></input>
  59. <label for="tipo">Tipo</label>
  60. <input name="tipo" type="text" placeholder="tipo..." th:value="${tipo}"></input>
  61. <label for="imagen">Imagen</label>
  62. <input id="inputUrlImagen" name="imagen" type="text" th:value="${imagen}"></input>
  63. <button id="botonSubirImagen" type="button">Subir Imagen</button>
  64. <br/><br/><br/>
  65. <input type="submit" id="boton" value="Enviar"></input>
  66. <label></label><p th:text="${MensajeError}"></p>
  67. </form>
  68.  
  69. $(document).ready( function(){
  70. $("#boton").click(function(ev){
  71. ev.preventDefault();
  72.  
  73. $('#form').submit();
  74. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement