Jdbc Example

To display records in table:
barchart-table.png

package gdf.qa.util;
 
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
import javax.portlet.*;
 
import org.jfree.chart.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.jdbc.JDBCCategoryDataset;
 
public class PortletChartUtilities {
 
    private static String MYSQL_JDBC_DRIVER = "com.mysql.jdbc.Driver";
 
    private static String JDBC_URL = "jdbc:mysql://localhost/chart?user=chart&password=chart";
 
    private static String sql = "SELECT category, series1, series2, series3 FROM barchart";
 
    private static Connection getDBConnection() throws SQLException {
        try {
            Class.forName(MYSQL_JDBC_DRIVER);
        } catch (ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }
 
        return DriverManager.getConnection(JDBC_URL);
 
    }
 
    public static void writeChartImageData(ResourceRequest request, ResourceResponse response) throws IOException
    {
        response.setContentType("image/png");
        OutputStream out = response.getPortletOutputStream();
 
        try {
            JDBCCategoryDataset dataset = (JDBCCategoryDataset) generateBarChartDataset(getDBConnection(), sql );
            JFreeChart chart = ChartFactory.createBarChart("Bar Chart", "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false);
 
            if (chart != null) {
                CategoryPlot plot = (CategoryPlot) chart.getPlot();
                BarRenderer renderer = (BarRenderer) plot.getRenderer();
                renderer.setItemMargin(0.0);
                ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
            }
        } catch (Exception e) {
            System.err.println(e.toString());
        } finally {
            out.close();
        }
 
    }
 
    private static JDBCCategoryDataset generateBarChartDataset(
            Connection dbConnection, String sql) {
        JDBCCategoryDataset dataset = null;
 
        try {
            dataset = new JDBCCategoryDataset(dbConnection);
            dataset.executeQuery(sql);
        } catch (SQLException e) {
            System.err.print("SQLException: ");
            System.err.println(e.getMessage());
        } catch (Exception e) {
            System.err.print("Exception: ");
            System.err.println(e.getMessage());
        }
 
        return dataset;
    }
 
}

Portlet:

package gdf.qa.portlets;
 
import java.io.IOException;
 
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceURL;
 
import com.liferay.util.bridges.mvc.MVCPortlet;
import gdf.qa.util.PortletChartUtilities;
 
/**
 * Portlet implementation class QAMetrics
 */
public class QAMetrics extends MVCPortlet {
    public void serveResource(ResourceRequest request, ResourceResponse response)
            throws PortletException, IOException {
        String resourceID = request.getResourceID();
 
        if (resourceID.equals("chart-image")) {
            PortletChartUtilities.writeChartImageData(request, response);
        }
    }
 
    @Override
    public void doView(RenderRequest renderRequest,
            RenderResponse renderResponse) throws IOException, PortletException {
 
        ResourceURL chartUrl = renderResponse.createResourceURL();
        chartUrl.setResourceID("chart-image");
        String resourceUrl = chartUrl.toString();
        renderRequest.setAttribute("resourceUrl", resourceUrl);
        include(viewJSP, renderRequest, renderResponse);
 
    }
 
}

View:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ page import="javax.portlet.*"%>
<portlet:defineObjects />
 
<img src="${resourceUrl}"/>
JDBC-example.png
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License