You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

This application will deal with populating a image on a database and later retrieving the same from the database. This tutorial will make you understand how to deal with data of type Blob. We will be using Derby database in this application. Please use images of smaller sizes to upload on to database. This is because there are some limitations associated with the derby database.

To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.

  • Sun JDK 5.0+ (J2SE 1.5)
  • Eclipse 3.3.1.1 (Eclipse Classic package of Europa distribution), which is platform specific
  • Web Tools Platform (WTP) 2.0.1
  • Data Tools Platform (DTP) 1.5.1
  • Eclipse Modeling Framework (EMF) 2.3.1
  • Graphical Editing Framework (GEF) 3.3.1

Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:

The application development will take you through the following

Creating a Dynamic Web Project

  1. Launch Eclipse. Select File->New->Project.





  2. Select Web->Dynamic Web Project. Select Next.





  3. On the next screen give the name of the project as WebJDBC.





  4. Select default values for all other fields. Finally select Finish.





Creating a database using Administrative Console

  1. Start the server and Launch the Administrative Console using the URL http://localhost:8080/console.
  2. Enter default username and password.
  3. In the welcome page, Under Embedded DB, Select DB Manager.





  4. On the next page create a database userdbs and Select create.





  5. Once done you can see the userdbs database listed in DB Viewer portlet under Databases. This confirms that the database has been successfully created.





  6. As shown in the figure under Use DB, select userdbs from the dropdown box.





  7. Run the query as shown in the figure. This query will create table PICTURES with the columns name and pic.
    \\\



    CreateTable.sql
    create table pictures(name varchar(32) not null primary key, pic blob(16M));
    

Creating a datasource using Administrative Console

  1. Start the server and Launch the Administrative Console using the URL http://localhost:8080/console.
  2. Enter default username and password.
  3. Once in the welcome page. In console navigation, Under Services, Select Database Pools.





  4. On the next screen, Create a new database pool using Geronimo database pool wizard.





  5. On the next screen give the name as suggested in the figure. This will initiate the process to create a Derby Embedded XA datasource.





  6. Select the Driver jar and give the database name as userdbs(Remember this is the database we created in the previous step). Rest all fields can be set to default.





  7. Select Deploy to deploy the connector plan.





  8. Once done you can see the Database Pool jdbc/userds listed in the available database pools.





Adding code for Image Upload

  1. Right click on WebContent and Select New->jsp.





  2. Name the jsp as index.jsp and Select Next.





  3. Select Finish. This will create a template for index.jsp.





  4. Add the following code to index.jsp.
    index.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Image Upload</title>
    </head>
    <body>
    <h2>Select a Image and Upload it</h2>
    <form action="/WebJDBC/ImageUpload">
    <table>
    <tr>
    <td>
    Location of the Image(full path)
    </td>
    <td>
    <Input type="text" name="ImageLoc">
    </td>
    </tr>
    <tr>
    <td>
    Name of the Image
    </td>
    <td>
    <Input type="text" name="ImageName">
    </td>
    </tr>
    <tr>
    <td>
    <Input type="submit" value="submit">
    </td>
    <tr>
    </table>
    </form>
    </body>
    </html>
    
  • No labels