Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Can I Use Mysql datatsource instead of ODA datasource?
Mylsamy
Hi All ,
I want to print the dynamic data from mysql instead of Custom data from ODA data source in birt report using Servlet?
Many of forums give only the examples of ODA dataaccess using servlet in birt report...
Can anybody help me to solve this problem?
Find more posts tagged with
Comments
bhanley
You can directly query MySQL from a BIRT report. What role is the servlet playing in the process?
Mylsamy
How can i access my MySql database through jdbc datasource in servlet?
bhanley
I am still a bit confused. What role does the servlet play in this? You do not need to go through a servlet to access a MySQL DB in BIRT, you can just construct the JDBC connection right on the report. The only reason I can see for doing it is to have some work done against the data before the report gets it.
Can you describe your process in a bit more detail?
Mylsamy
First Sorry for my poor English, <br />
<br />
Actually my problem is print the data in pdf format in rpttemplate using eclipse IDE.<br />
<br />
<br />
Here is my full servlet code :<br />
<br />
public void doGet(HttpServletRequest req, HttpServletResponse resp)<br />
throws ServletException, IOException {<br />
<br />
resp.setContentType( "application/pdf" ); <br />
resp.setHeader ("Content-Disposition","inline; filename=test.pdf"); <br />
String reportName = req.getParameter("ReportName");<br />
ServletContext sc = req.getSession().getServletContext();<br />
String[] cols = (String[])req.getParameterMap().get("dyna1");<br />
this.birtReportEngine = BirtEngine.getBirtEngine(sc);<br />
<br />
HTMLRenderContext renderContext = new HTMLRenderContext();<br />
renderContext.setBaseImageURL(req.getContextPath()+"/images");<br />
renderContext.setImageDirectory(sc.getRealPath("/images"));<br />
HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>();<br />
contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );<br />
<br />
<br />
IReportRunnable design;<br />
try<br />
{<br />
design = birtReportEngine.openReportDesign("G:/Birt/WebReportGlassFish/WebContent/Reports/"+reportName );<br />
ReportDesignHandle report = (ReportDesignHandle) design.getDesignHandle( ); <br />
buildReport( cols, report );<br />
<br />
IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design ); <br />
//task.getAppContext().put("BIRT_VIEWER_HTTPSERVLET_REQUEST", req ); <br />
task.setAppContext( contextMap );<br />
<br />
PDFRenderOption options = new PDFRenderOption();<br />
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);<br />
resp.setHeader( "Content-Disposition", "inline; filename="test.pdf"" ); <br />
options.setOutputStream(resp.getOutputStream()); <br />
task.setRenderOption(options);<br />
<br />
<br />
task.run();<br />
task.close();<br />
}catch (Exception e){<br />
String psXml=e.getMessage();<br />
<br />
<br />
String psXml1=psXml; <br />
e.printStackTrace();<br />
throw new ServletException( e );<br />
}<br />
}<br />
<br />
public void buildReport(String[] cols, ReportDesignHandle designHandle){<br />
<br />
<br />
try{<br />
ElementFactory designFactory = designHandle.getElementFactory( );<br />
<br />
buildDataSource(designFactory, designHandle);<br />
<br />
<br />
buildDataSet(cols, "From Offices", designFactory, designHandle);<br />
<br />
TableHandle table = designFactory.newTableItem( "table", cols.length );<br />
table.setWidth( "100%" );<br />
table.setDataSet( designHandle.findDataSet( "ds" ) );<br />
<br />
<br />
PropertyHandle computedSet = table.getColumnBindings( ); <br />
ComputedColumn cs1 = null;<br />
<br />
for( int i=0; i < cols.length; i++){<br />
cs1 = StructureFactory.createComputedColumn();<br />
cs1.setName((String)cols
);<br />
cs1.setExpression("dataSetRow["" + (String)cols[i] + ""]");<br />
computedSet.addItem(cs1);<br />
}<br />
<br />
<br />
RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );<br />
<br />
<br />
for( int i=0; i < cols.length; i++){<br />
LabelHandle label1 = designFactory.newLabel( (String)cols
); <br />
label1.setText((String)cols
);<br />
CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );<br />
cell.getContent( ).add( label1 );<br />
} <br />
<br />
RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );<br />
for( int i=0; i < cols.length; i++){<br />
CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i );<br />
DataItemHandle data = designFactory.newDataItem( "data_"+(String)cols
);<br />
data.setResultSetColumn( (String)cols
);<br />
cell.getContent( ).add( data );<br />
}<br />
<br />
<br />
<br />
designHandle.getBody( ).add( table );<br />
}catch(Exception e){<br />
e.printStackTrace();<br />
}<br />
<br />
}<br />
<br />
void buildDataSource( ElementFactory designFactory, ReportDesignHandle designHandle ) throws SemanticException,Exception<br />
{<br />
<br />
OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource(<br />
"Data Source", "org.eclipse.birt.report.data.oda.jdbc" );<br />
//designFactory.<br />
dsHandle.setProperty( "odaDriverClass",<br />
"org.eclipse.birt.report.data.oda.sampledb.Driver" );<br />
dsHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" );<br />
dsHandle.setProperty( "odaUser", "ClassicModels" );<br />
dsHandle.setProperty( "odaPassword", "" );<br />
designHandle.getDataSources( ).add( dsHandle );<br />
}<br />
<br />
<br />
void buildDataSet(String[] cols, String fromClause, ElementFactory designFactory, ReportDesignHandle designHandle ) throws SemanticException<br />
{<br />
<br />
OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "ds",<br />
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );<br />
dsHandle.setDataSource( "dbcrs0" );<br />
String qry = "Select ";<br />
for( int i=0; i < cols.length; i++){<br />
qry += " " + cols
;<br />
if( i != (cols.length -1) ){<br />
qry += ",";<br />
}<br />
<br />
}<br />
qry += " " + fromClause;<br />
<br />
dsHandle.setQueryText( qry );<br />
<br />
designHandle.getDataSets( ).add( dsHandle );<br />
<br />
<br />
} <br />
<br />
<br />
This program demonstrate using the DE API to add a table to an existing report or template while running it within a servlet.<br />
<br />
In the buildDataSource method connects the Classic modell DB inODA Data Source . That is inbuilt database in birt report. Now i want to change the mySql for Jdbc Data source instead of Classic model sample db in ODA Datasource.<br />
<br />
This code prints the values that are in Offices table in Classic Model DB. I want to print the jdbc datasource database value..<br />
<br />
<br />
Can i do this?
Mylsamy
First Sorry for my poor English,
Actually my problem is print the data in pdf format in rpttemplate using eclipse IDE.
And i attached my Servlet ,
This program demonstrate using the DE API to add a table to an existing report or template while running it within a servlet.
In the buildDataSource method connects the Classic modell DB inODA Data Source . That is inbuilt database in birt report. Now i want to change the mySql for Jdbc Data source instead of Classic model sample db in ODA Datasource.
This code prints the values that are in Offices table in Classic Model DB. I want to print the jdbc datasource database value..
Can i do this?
Mylsamy
First Sorry for my poor English,
Actually my problem is print the data in pdf format in rpttemplate using eclipse IDE.
I attacehed the Servlet Code,
This program demonstrate using the DE API to add a table to an existing template while running it within a servlet.
In the buildDataSource method connects the Classic modell DB inODA Data Source . That is inbuilt database in birt report.
This code prints the values that are in Offices table in Classic Model DB. I want to print the jdbc datasource database value..
Now i want to change the mySql for Jdbc Data source instead of Classic model sample db in ODA Datasource.
Can i do this?