Share on Google+Share on Google+

Retrieve data from the database and write into ppt file

Advertisement
In this section, you will learn how to retrieve data from the database and write into the .ppt file.

Java Retrieve data from the database & write into ppt file

In this section, we are going to retrieve data from the database and write into the .ppt file. For this purpose, we have used Jakarta POI api to create a .ppt file. The class SlideShow creates a powerpoint documentation. The Slide class represents a slide in a PowerPoint Document which allows to access the text and the layout of the slide. The class TextBox is responsible for the text as well as the properties and methods that control the alignment and anchoring of the text.With the use of setText() method, we have inserted database values into it.

Here is the code:

import java.io.*;
import java.awt.*;
import java.sql.*;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.model.TextBox;

public class createNewPresentation {
	public static void main(String a[]) {
		try {
			SlideShow slideShow = new SlideShow();
			Slide slide = slideShow.createSlide();
			TextBox txt = new TextBox();
			Slide slide1 = slideShow.createSlide();
			TextBox txt1 = new TextBox();
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/register", "root", "root");
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from data");
			String name = "";
			String address = "";
			while (rs.next()) {
				name += rs.getString("name") + "\n";
				address += rs.getString("address") + "\n";
			}
			txt.setText(name + " ");
			slide.addShape(txt);
			txt1.setText(address + " ");
			slide1.addShape(txt1);
			FileOutputStream out = new FileOutputStream("data.ppt");
			slideShow.write(out);
			out.close();
		} catch (Exception e) {
		}
	}
}

Advertisements

Posted on: October 29, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.