Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <%@ page import="java.sql.*" %>
  2. <%@ page import='java.io.InputStream' %>
  3. <%@ page import='java.io.OutputStream' %>
  4. <%
  5. String login = "root";
  6. String password = "";
  7. String url = "jdbc:mysql://localhost/dbimagenes";
  8. Connection conn = null;
  9. Statement statement = null;
  10. ResultSet rs = null;
  11. int nBytes=0;
  12. %>
  13. <html><style type="text/css">
  14. <!--
  15. body {
  16. background-color: #F5f5f5;
  17. }
  18. -->
  19. </style><body>
  20. <h1>Imagen desde MySQL</h1>
  21. <table>
  22. <tr><td>
  23. <%
  24. Class.forName("com.mysql.jdbc.Driver").newInstance ();
  25. conn = DriverManager.getConnection(url, login, password);
  26. statement = conn.createStatement();
  27. rs = statement.executeQuery("SELECT imagen FROM t_imagenes where id='2'");
  28. try{
  29. if(rs.next()){
  30. response.setContentType("image/jpeg");
  31. InputStream is = rs.getBinaryStream(1);
  32. OutputStream aux= response.getOutputStream();
  33. out.println("jajaja");
  34.  
  35. byte [] buffer = new byte[4096];
  36. for (;;) {
  37. nBytes = is.read(buffer);
  38. if (nBytes == -1)
  39. break;
  40.  
  41. aux.write(buffer, 0, nBytes);
  42.  
  43. }
  44.  
  45. is.close();
  46. aux.flush();
  47. aux.close();
  48.  
  49.  
  50. }else{
  51.  
  52. throw new SQLException("image not found");
  53. }
  54. rs.close();
  55. } catch (SQLException e) { out.println("Imagen no encontrada");}
  56.  
  57. out.println("no se muestra");
  58. %>
  59. </td></tr></table>
  60.  
  61. <p> Imagen</p>
  62. <a href="index.html">PRINCIPAL</a>
  63. </body></html>
  64.  
  65. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  66. <%@include file="conexion.jsp" %>
  67. <!DOCTYPE html>
  68. <html>
  69. <head>
  70. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  71. <title>JSP Page</title>
  72. </head>
  73. <body>
  74. <h1>Insertar</h1>
  75. <%
  76. String nombre=request.getParameter("txtnombre");
  77. String marca=request.getParameter("txtmarca");
  78. String imagen=request.getParameter("txtimagen");
  79.  
  80. if(nombre!=null && marca!=null && imagen!=null){
  81. String qry ="insert into t_imagenes(nombre,marca,imagen) values('"+nombre+"','"+marca+"','"+imagen+"')";
  82. sql.executeUpdate(qry);
  83. out.print("Datos Registrados "
  84. + "<a href='index.jsp'>REGRESAR</a>");
  85.  
  86. }else{
  87.  
  88. %>
  89. <form name="frmimagenes" method="post" action="insertar.jsp">
  90. nombre: <input type="text" name="txtnombre"/><br/>
  91. marca: <input type="text" name="txtmarca"/><br/>
  92. imagen: <input type="file" name="txtimagen" value="" size="50" /><br/>
  93. <input type="submit" value="Guardar">
  94. </form>
  95.  
  96. <%}//else%>
  97. </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement