Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help needed regarding saving BLOB in oracle using JSP

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Help needed regarding saving BLOB in oracle using JSP

    Hello everybody. I've landed into a serious problem. I have to upload images/videos in Oracle 9i database and I also have to device some mechanism through which I could view them later. I've got some codes which did not solve my problem. I'm posting the codes along with the structure of the oracle table here. Please suggest me something to solve this problem. PLEASE please please help me out



    My Oracle 9i table structure is:
    Name of the table is IMAGEMAIN
    Id: number(3)
    Image: BLOB



    I've a html file named "index.html". Its contents are:
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title></title>
    
      </head>
      <body>
          <form method="post" action="index.jsp">
                    <input type="text" name="hint">
                    <br><input type="file" name="user_file">
                    <br>   <input type="SUBMIT" name="s1" value="Save">
       </form>
      </body>
    </html>
    
    
    
    "index.jsp" page is there to upload the BLOB into database:
    <%@ page import = "java.sql.*"%>
    <%@ page import = "java.io.*,java.util.*,javax.servlet.*,javax.servlet.http.*" %>
    <html>
        <head>
          <title>JSP Page</title>
        </head>
        <body>
            <h1>Insert into Oracle9i!</h1>
            <%
            try{
                Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle9i","scott","tiger");
                String ll=request.getParameter("user_file");
                String lo=request.getParameter("hint");
                File imgfile = new File(ll);
                FileInputStream fin = new FileInputStream(imgfile);            
                PreparedStatement pre = con.prepareStatement("insert into IMAGEMAIN (id,image) values(?,?)");
                pre.setString(1,lo);
                pre.setBinaryStream(2,fin,(int)imgfile.length());
                pre.executeUpdate();          
                pre.close();
            }catch(Exception E){ out.println("the error is  "+ E);}
            %>
         </body>
    </html>
    
    
    
    And "view.jsp" to view the BLOBs in tha database:
    <%@ page language = "java" import="java.sql.*,java.io.*,java.util.*,javax.servlet.*,javax.servlet.http.*, oracle.sql.BLOB" %>
    <html>
        <head>
          <title>JSP Page</title>
        </head>
        <body>
            <h1>Retrieve from Oracle9i!</h1>
            <%
            try{            
                Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle9i","scott","tiger");
                Statement st1=con.createStatement();
                ResultSet rs1 = st1.executeQuery("select image from IMAGEMAIN where id='1'");
                String imgLen="";
                if(rs1.next()){
                imgLen = rs1.getString(1);
                out.println(imgLen.length());
                }
                rs1 = st1.executeQuery("select image from IMAGEMAIN where id='1'");
                if(rs1.next()){
                    int len = imgLen.length();
                    byte [] rb = new byte[len];
                    InputStream readImg = rs1.getBinaryStream(1);
                    int index=readImg.read(rb, 0, len);
                    System.out.println("index"+index);
                    st1.close();
                    response.reset();
                    response.setContentType("image/jpg/gif");
                    response.getOutputStream().write(rb,0,len);
                    response.getOutputStream().flush();
                 }
              }catch(Exception E){E.printStackTrace();}
            %>
         </body>
    </html>


    I get absolutely blank index.jsp and view.jsp pages. Only the heading part within the <h1> is visible. Please suggest me something!
    Last edited by helloworld922; February 25th, 2010 at 09:46 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help needed regarding saving BLOB in oracle using JSP

    please help me out! i'm in dire need of this code

Similar Threads

  1. Oracle and Sun
    By copeg in forum The Cafe
    Replies: 2
    Last Post: January 31st, 2010, 07:00 AM
  2. Database connectivity problem-Oracle
    By Entrant in forum JDBC & Databases
    Replies: 3
    Last Post: October 11th, 2009, 11:08 AM
  3. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 12:56 PM
  4. Oracle to Buy Sun
    By Fendaril in forum The Cafe
    Replies: 2
    Last Post: June 21st, 2009, 11:48 AM
  5. Replies: 1
    Last Post: November 12th, 2008, 05:16 PM