Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ page import="java.sql.*" %>
- <%@ page import='java.io.InputStream' %>
- <%@ page import='java.io.OutputStream' %>
- <%
- String login = "root";
- String password = "";
- String url = "jdbc:mysql://localhost/dbimagenes";
- Connection conn = null;
- Statement statement = null;
- ResultSet rs = null;
- int nBytes = 0;
- %>
- <html><style type="text/css">
- <!--
- body {
- background-color: #F5f5f5;
- }
- -->
- </style><body>
- <h1>Imagen desde MySQL</h1>
- <table>
- <tr><td>
- <%
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(url, login, password);
- statement = conn.createStatement();
- rs = statement.executeQuery("SELECT imagen FROM t_imagenes where id='2'");
- try {
- if (rs.next()) {
- response.setContentType("image/jpeg");
- InputStream is = rs.getBinaryStream(1);
- OutputStream aux = response.getOutputStream();
- out.println("jajaja");
- byte[] buffer = new byte[4096];
- for (;;) {
- nBytes = is.read(buffer);
- if (nBytes == -1) {
- break;
- }
- aux.write(buffer, 0, nBytes);
- }
- is.close();
- aux.flush();
- aux.close();
- } else {
- throw new SQLException("image not found");
- }
- rs.close();
- } catch (SQLException e) {
- out.println("Imagen no encontrada");
- }
- out.println("no se muestra");
- %>
- </td></tr></table>
- <p> Imagen</p>
- <a href="index.html">PRINCIPAL</a>
- </body>
- </html>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <%@include file="conexion.jsp" %>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Page</title>
- </head>
- <body>
- <h1>Insertar</h1>
- <%
- String nombre=request.getParameter("txtnombre");
- String marca=request.getParameter("txtmarca");
- String imagen=request.getParameter("txtimagen");
- if(nombre!=null && marca!=null && imagen!=null){
- String qry ="insert into t_imagenes(nombre,marca,imagen) values('"+nombre+"','"+marca+"','"+imagen+"')";
- sql.executeUpdate(qry);
- out.print("Datos Registrados "
- + "<a href='index.jsp'>REGRESAR</a>");
- }else{
- %>
- <form name="frmimagenes" method="post" action="insertar.jsp">
- nombre: <input type="text" name="txtnombre"/><br/>
- marca: <input type="text" name="txtmarca"/><br/>
- imagen: <input type="file" name="txtimagen" value="" size="50" /><br/>
- <input type="submit" value="Guardar">
- </form>
- <%}//else%>
- </body>
- </html>
- OutputStream oImage;
- try {
- rs = statement.executeQuery("SELECT imagen FROM t_imagenes where id='2'");
- if(rs.next()) {
- byte barray[] = rs.getBytes(1);
- response.setContentType("image/gif");
- oImage=response.getOutputStream();
- oImage.write(barray);
- oImage.flush();
- oImage.close();
- }
- }
- catch(Exception ex){
- //ex.printStackTrace();
- }finally {
- try{
- if(con!=null)
- con.close();
- }catch(Exception ex){
- // ex.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement