Click here to close now.

Welcome!

Weblogic Authors: Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

WebLogic Tutorial: "Integrating Apache Poi in WebLogic Server"

The Apache Jakarta POI project provides components for the access and generation of Excel documents

The Apache Jakarta POI project provides components for the access and generation of Excel documents. The POI HSSF API is used to generate Excel Workbooks and to add Excel spreadsheets to a workbook. An Excel spreadsheet consists of rows and cells. The layout and fonts of a spreadsheet are also set with the POI HSSF API.

A database table is often required to be presented in an Excel spreadsheet. Also, a developer's requirement could be to store an Excel spreadsheet in a database table. The Apache POI HSSF project is an API to create an Excel spreadsheet. The data in the Excel spreadsheet generated with the POI HSSF project may be static data in an XML document, or dynamically retrieved data from a database. Also, an Excel document may be converted to an XML document or be stored in a database. In this tutorial we will discuss the procedure to create an Excel spreadsheet from a MySQL database table in WebLogic Server. Subsequently, an Excel spreadsheet will be stored in a database table.

Preliminary Setup
The implementation of the POI HSSF project is provided in the org.apache.poi.hssf.usermodel package. The org.apache.poi.hssf.usermodel package classes are required in the Classpath to generate an Excel spreadsheet or to parse an Excel spreadsheet. Download the Apache POI library poi-bin-2.5.1-final-20040804.zip file and extract the zip file to an installation directory (http://jakarta.apache.org/poi/). Install WebLogic 8.1 Server. Download the MySQL database (www.mysql.com). Extract the MySQL zip file mysql-4.0.25-win32.zip to a directory. Install the MySQL database. Download the MySQL Connector/J JDBC driver (www.mysql.com/products/connector/j/). Extract the MySQL zip file mysql-connector-java-3.1.10.zip to a directory. Add the MySQL JDBC driver jar file, mysql-connector-java-3.1.10-bin.jar, to the <weblogic81>\samples\domains\examples\startExamplesServer script CLASSPATH variable.

Login to the MySQL database with the DOS command:

>mysql

Access the example database test with the command:

mysql>use test

Create an example database table in the MySQL database from which an Excel spreadsheet will be generated. The SQL script to create example table Catalog is shown in Listing 1.

Next, we will add the Apache POI .jar file to the WebLogic Server Classpath and create a JDBC datasource in the WebLogic Server to retrieve data for an Excel spreadsheet.

Add the poi-2.5.1-final-20040804.jar file to the CLASSPATH variable in the <weblogic81>\samples\domains\examples\startExamplesServer script. <weblogic81> is the directory in which the WebLogic Server is installed.

Next, create a JDBC connection with the MySQL database in WebLogic Server. Start the examples server with the script startExamplesServer. Access the administration console with the URL http://localhost:7001/console or with the Administration Console link in the WebLogic Server Examples index. In the administration console, right-click on the examples>Services>JDBC>Connection Pools node and select Configure a new JDBCConnectionPool. Specify the following connection properties to configure a JDBC connection pool:

  • The database type: MySQL
  • The JDBC driver: MySQL's Driver (Type 4)
  • Database name: test
  • Host name: localhost
  • Port number: 3306
  • Driver class name: com.mysql.jdbc.Driver
  • Connection URL: jdbc:mysql://localhost:3306/test
Next, configure a JDBC datasource in the administration console. Right-click on the examples>Services>JDBC>DataSources node and select Configure a new JDBCTxDataSource. In the "Configure the data source" frame, specify a datasource name and a JNDI Name - MySqlDS, for example. In the "Connect to connection pool" frame select the Connection Pool previously configured with the MySQL database. In the "Target the datasource frame," select the examplesServer. A datasource gets configured with the MySQL database.

Generating an Excel Document with Apache POI
In this section we will generate an Excel spreadsheet from the example database table. First, create a JSP application to generate an Excel spreadsheet.

In the JSP an Excel spreadsheet will be created from a MySQL database table. The Apache POI HSSF API is used to generate an Excel spreadsheet. The Apache POI HSSF package has classes for the different components of an Excel spreadsheet. Some of the commonly used classes of the Apache POI HSSF package are listed in Table 1.

First, import the Apache POI HSSF package:

<%@ page import="org.apache.poi.hssf.usermodel.*, java.sql.*,
java.io.*,javax.naming.InitialContext"%>

Create an Excel stylesheet workbook:

HSSFWorkbook wb=new HSSFWorkbook();

Next, create an Excel spreadsheet:

HSSFSheet sheet1=wb.createSheet("sheet1");

The data for the stylesheet is retrieved from a MySQL database table. Obtain a JDBC connection from the database. The JDBC connection is obtained with the datasource JNDI MySqlDS.

InitialContext initialContext = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)
initialContext.lookup("MySqlDS");
java.sql.Connection conn = ds.getConnection();

Create a java.sql.Statement and get a result set from the example table Catalog:

Statement stmt=conn.createStatement();
ResultSet resultSet=stmt.executeQuery("Select * from Catalog");

Create a header row for the Excel spreadsheet. The rows in an Excel spreadsheet are "0" based.

HSSFRow row=sheet1.createRow(0);

Set the header row cell values corresponding to the table columns. The row cells are also "0" based. For example, the value for the first cell in the row is set with the setCellValue method to CatalogId.

row.createCell((short)0).setCellValue("CatalogId");

To add rows to the spreadsheet, iterate over the result set and add a row for each of the table rows. Retrieve the column values from the ResultSet and set the values in the row cells.

More Stories By Deepak Vohra

Deepak Vohra is a Sun Certified Java 1.4 Programmer and a Web developer.

More Stories By Ajay Vohra

Ajay Vohra is a senior solutions architect with DataSynapse Inc.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
bob kennelly 08/19/06 05:49:54 PM EDT

Hello this is just what i've been looking for, it's a very helpfull article!

In the article there is a statement saying "see the Reference area" to find the JSP code, can anyone tell me where i can find the reference section please?

Thanks very much!

SYS-CON Italy News Desk 02/19/06 03:33:36 PM EST

The Apache Jakarta POI project provides components for the access and generation of Excel documents. The POI HSSF API is used to generate Excel Workbooks and to add Excel spreadsheets to a workbook. An Excel spreadsheet consists of rows and cells. The layout and fonts of a spreadsheet are also set with the POI HSSF API.

@ThingsExpo Stories
Can call centers hang up the phones for good? Intuitive Solutions did. WebRTC enabled this contact center provider to eliminate antiquated telephony and desktop phone infrastructure with a pure web-based solution, allowing them to expand beyond brick-and-mortar confines to a home-based agent model. It also ensured scalability and better service for customers, including MUY! Companies, one of the country's largest franchise restaurant companies with 232 Pizza Hut locations. This is one example of WebRTC adoption today, but the potential is limitless when powered by IoT.
SYS-CON Events announced today that Optimal Design, an Internet of Things solution provider, will exhibit at SYS-CON's Internet of @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Optimal Design is an award winning product development firm offering industrial design and engineering services to the consumer, medical, and defense markets.
The list of ‘new paradigm’ technologies that now surrounds us appears to be at an all time high. From cloud computing and Big Data analytics to Bring Your Own Device (BYOD) and the Internet of Things (IoT), today we have to deal with what the industry likes to call ‘paradigm shifts’ at every level of IT. This is disruption; of course, we understand that – change is almost always disruptive.
GENBAND introduced its Real Time Communications (RTC) Client for Lync* to seamlessly combine real-time communications with Lync Instant Messaging (IM) and Presence. “We’re shaking up the economics of delivering Unified Communications (UC) and offering a compelling way to integrate previously bespoke communications technologies,” said Carl Baptiste, GENBAND’s Senior Vice President, Enterprise Solutions. “We’re offering enterprises the best of both worlds by combining our own high availability voice, video and collaboration with Lync’s IM and Presence; creating a single, web centric, client. O...
After making a doctor’s appointment via your mobile device, you receive a calendar invite. The day of your appointment, you get a reminder with the doctor’s location and contact information. As you enter the doctor’s exam room, the medical team is equipped with the latest tablet containing your medical history – he or she makes real time updates to your medical file. At the end of your visit, you receive an electronic prescription to your preferred pharmacy and can schedule your next appointment.
SYS-CON Events announced today that Creative Business Solutions will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Creative Business Solutions is the top stocking authorized HP Renew Distributor in the U.S. Based out of Long Island, NY, Creative Business Solutions offers a one-stop shop for a diverse range of products including Proliant, Blade and Industry Standard Servers, Networking, Server Options and Care Packs. As a trusted supplier, CBS guarantees quality controlled stock levels thanks to an Auto...
SYS-CON Events announced today that SoftLayer, an IBM company, has been named “Gold Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place June 9-11, 2015 at the Javits Center in New York City, NY, and the 17th International Cloud Expo®, which will take place November 3–5, 2015 at the Santa Clara Convention Center in Santa Clara, CA. SoftLayer operates a global cloud infrastructure platform built for Internet scale. With a global footprint of data centers and network points of presence, SoftLayer provides infrastructure as a service to leading-edge customers ranging from ...
@ThingsExpo has been named the Top 5 Most Influential M2M Brand by Onalytica in the ‘Machine to Machine: Top 100 Influencers and Brands.' Onalytica analyzed the online debate on M2M by looking at over 85,000 tweets to provide the most influential individuals and brands that drive the discussion. According to Onalytica the "analysis showed a very engaged community with a lot of interactive tweets. The M2M discussion seems to be more fragmented and driven by some of the major brands present in the M2M space. This really allows some room for influential individuals to create more high value inter...
SYS-CON Events announced today that Cisco, the worldwide leader in IT that transforms how people connect, communicate and collaborate, has been named “Gold Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Cisco makes amazing things happen by connecting the unconnected. Cisco has shaped the future of the Internet by becoming the worldwide leader in transforming how people connect, communicate and collaborate. Cisco and our partners are building the platform for the Internet of Everything by connecting the...
SYS-CON Events announced today that Liaison Technologies, a leading provider of data management and integration cloud services and solutions, has been named "Silver Sponsor" of SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York, NY. Liaison Technologies is a recognized market leader in providing cloud-enabled data integration and data management solutions to break down complex information barriers, enabling enterprises to make smarter decisions, faster.
SYS-CON Events announced today that Windstream, a leading provider of advanced network and cloud communications, has been named “Silver Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9–11, 2015, at the Javits Center in New York, NY. Windstream (Nasdaq: WIN), a FORTUNE 500 and S&P 500 company, is a leading provider of advanced network communications, including cloud computing and managed services, to businesses nationwide. The company also offers broadband, phone and digital TV services to consumers primarily in rural areas.
SYS-CON Events announced today that kintone has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY, and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. kintone promotes cloud-based workgroup productivity, transparency and profitability with a seamless collaboration space, build your own business application (BYOA) platform, and workflow automation system.
Participants will reach the final if their IoT solution is liked. A community vote will determine the best solutions submitted in each country, after which an expert jury will select the national winners and the best international IoT solution. Each country's best solution can win a national marketing campaign worth up to €30,000 and become a partner in Deutsche Telekom's participating markets. The winning international solution can become partner of Deutsche Telekom Group across all eight countries and reach out to a potential of 10,8 million business customers. Deutsche Telekom Group has a...
The best mobile applications are augmented by dedicated servers, the Internet and Cloud services. Mobile developers should focus on one thing: writing the next socially disruptive viral app. Thanks to the cloud, they can focus on the overall solution, not the underlying plumbing. From iOS to Android and Windows, developers can leverage cloud services to create a common cross-platform backend to persist user settings, app data, broadcast notifications, run jobs, etc. This session provides a high level technical overview of many cloud services available to mobile app developers, includi...
Recent technology advances in miniaturization has positioned the wearables as the pinnacle of technology convergence with the human body. We inquire if wearables are mere standard miniaturized devices extended with the connectivity and present our views on considerations like design, applications, performance, efficiency, interoperability, usage scenarios, human device interaction and consequent trade-offs enabling wearables to impart optimal value.
WebRTC Summit has announced today that Peter Dunkley has been named summit chair of WebRTC Summit 2015 New York. The 4th International WebRTC Summit will take place on June 9-11, 2015, at the Javits Center in Manhattan, New York. @ThingsExpo anticipates 90% of WebRTC companies & developers will monetize their products & services through IoT by 2016. Peter Dunkley is Technical Director at Acision. He graduated from The University of Edinburgh in 2000 with a BSc (Hons) in Computer Science. After graduation Peter worked on a PSTN switch developing signalling stacks for SS7, ISDN and simi...
In this session we look at creating interactive communications via the web by adding messaging, file transfer, and group communication (group chat and audio/video conferencing) into the web experience. We will also discuss potential applications of this technology in areas including B2B, B2C, P2P, and gaming. Peter is Technical Director at Acision. He graduated from The University of Edinburgh in 2000 with a BSc (Hons) in Computer Science. After graduation Peter worked on a PSTN switch developing signalling stacks for SS7, ISDN and similar protocols and creating advanced routing and serv...
SYS-CON Events announced today that ProfitBricks, the provider of painless cloud infrastructure, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY., and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. ProfitBricks is the IaaS provider that offers a painless cloud experience for all IT users, with no learning curve. ProfitBricks boasts flexible cloud servers and networking, an integrated Data Center Designer tool f...
SYS-CON Events announced today that Dyn, the worldwide leader in Internet Performance, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Dyn is a cloud-based Internet Performance company. Dyn helps companies monitor, control, and optimize online infrastructure for an exceptional end-user experience. Through a world-class network and unrivaled, objective intelligence into Internet conditions, Dyn ensures traffic gets delivered faster, safer, and more reliably than ever.
SYS-CON Events announced today that Open Data Centers (ODC), a carrier-neutral colocation provider, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. Open Data Centers is a carrier-neutral data center operator in New Jersey and New York City offering alternative connectivity options for carriers, service providers and enterprise customers.