author | František Kučera <franta-hg@frantovo.cz> |
Sat, 28 Feb 2009 21:31:02 +0100 | |
changeset 1 | 639991d0808a |
permissions | -rw-r--r-- |
franta-hg@1 | 1 |
package de.laures.cewolf.dp; |
franta-hg@1 | 2 |
|
franta-hg@1 | 3 |
import java.io.Serializable; |
franta-hg@1 | 4 |
import java.sql.Connection; |
franta-hg@1 | 5 |
import java.sql.ResultSet; |
franta-hg@1 | 6 |
import java.util.Map; |
franta-hg@1 | 7 |
|
franta-hg@1 | 8 |
import javax.naming.Context; |
franta-hg@1 | 9 |
import javax.naming.InitialContext; |
franta-hg@1 | 10 |
import javax.naming.NamingException; |
franta-hg@1 | 11 |
import javax.sql.DataSource; |
franta-hg@1 | 12 |
|
franta-hg@1 | 13 |
import org.jfree.data.xy.XYSeries; |
franta-hg@1 | 14 |
|
franta-hg@1 | 15 |
import de.laures.cewolf.DatasetProduceException; |
franta-hg@1 | 16 |
|
franta-hg@1 | 17 |
/** |
franta-hg@1 | 18 |
* @author glaures |
franta-hg@1 | 19 |
* |
franta-hg@1 | 20 |
* To change this generated comment edit the template variable "typecomment": |
franta-hg@1 | 21 |
* Window>Preferences>Java>Templates. |
franta-hg@1 | 22 |
* To enable and disable the creation of type comments go to |
franta-hg@1 | 23 |
* Window>Preferences>Java>Code Generation. |
franta-hg@1 | 24 |
*/ |
franta-hg@1 | 25 |
public class DataSourceXYSeries implements Serializable { |
franta-hg@1 | 26 |
|
franta-hg@1 | 27 |
private String dataSourceName; |
franta-hg@1 | 28 |
private String query; |
franta-hg@1 | 29 |
private String xCol = "x"; |
franta-hg@1 | 30 |
private String yCol = "y"; |
franta-hg@1 | 31 |
private String seriesName = "name"; |
franta-hg@1 | 32 |
|
franta-hg@1 | 33 |
/** |
franta-hg@1 | 34 |
* Constructor for DataSourceXYSeries. |
franta-hg@1 | 35 |
*/ |
franta-hg@1 | 36 |
public DataSourceXYSeries(String dataSourceName, String query) { |
franta-hg@1 | 37 |
this.dataSourceName = dataSourceName; |
franta-hg@1 | 38 |
this.query = query; |
franta-hg@1 | 39 |
} |
franta-hg@1 | 40 |
|
franta-hg@1 | 41 |
protected DataSource getDataSource() throws NamingException { |
franta-hg@1 | 42 |
Context initCtx = new InitialContext(); |
franta-hg@1 | 43 |
Context envCtx = (Context) initCtx.lookup("java:comp/env"); |
franta-hg@1 | 44 |
return (DataSource) envCtx.lookup(dataSourceName); |
franta-hg@1 | 45 |
} |
franta-hg@1 | 46 |
|
franta-hg@1 | 47 |
/** |
franta-hg@1 | 48 |
* @see de.laures.cewolf.DatasetProducer#produceDataset(Map) |
franta-hg@1 | 49 |
*/ |
franta-hg@1 | 50 |
public XYSeries produceXYSeries() throws DatasetProduceException { |
franta-hg@1 | 51 |
XYSeries series = new XYSeries(seriesName); |
franta-hg@1 | 52 |
try { |
franta-hg@1 | 53 |
DataSource ds = getDataSource(); |
franta-hg@1 | 54 |
Connection con = ds.getConnection(); |
franta-hg@1 | 55 |
ResultSet rs = con.createStatement().executeQuery(query); |
franta-hg@1 | 56 |
int xColIndex = rs.findColumn(xCol); |
franta-hg@1 | 57 |
int yColIndex = rs.findColumn(yCol); |
franta-hg@1 | 58 |
while(rs.next()){ |
franta-hg@1 | 59 |
series.add((Number)rs.getObject(xColIndex), (Number)rs.getObject(yColIndex)); |
franta-hg@1 | 60 |
} |
franta-hg@1 | 61 |
} catch (Exception namingEx) { |
franta-hg@1 | 62 |
namingEx.printStackTrace(); |
franta-hg@1 | 63 |
throw new DatasetProduceException(namingEx.getMessage()); |
franta-hg@1 | 64 |
} |
franta-hg@1 | 65 |
return series; |
franta-hg@1 | 66 |
} |
franta-hg@1 | 67 |
} |