KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > laures > cewolf > dp > DataSourceXYSeries


1 package de.laures.cewolf.dp;
2
3 import java.io.Serializable JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import javax.naming.Context JavaDoc;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.naming.NamingException JavaDoc;
11 import javax.sql.DataSource JavaDoc;
12
13 import org.jfree.data.xy.XYSeries;
14
15 import de.laures.cewolf.DatasetProduceException;
16
17 /**
18  * @author glaures
19  *
20  * To change this generated comment edit the template variable "typecomment":
21  * Window>Preferences>Java>Templates.
22  * To enable and disable the creation of type comments go to
23  * Window>Preferences>Java>Code Generation.
24  */

25 public class DataSourceXYSeries implements Serializable JavaDoc {
26
27     private String JavaDoc dataSourceName;
28     private String JavaDoc query;
29     private String JavaDoc xCol = "x";
30     private String JavaDoc yCol = "y";
31     private String JavaDoc seriesName = "name";
32     
33     /**
34      * Constructor for DataSourceXYSeries.
35      */

36     public DataSourceXYSeries(String JavaDoc dataSourceName, String JavaDoc query) {
37         this.dataSourceName = dataSourceName;
38         this.query = query;
39     }
40
41     protected DataSource JavaDoc getDataSource() throws NamingException JavaDoc {
42         Context JavaDoc initCtx = new InitialContext JavaDoc();
43         Context JavaDoc envCtx = (Context JavaDoc) initCtx.lookup("java:comp/env");
44         return (DataSource JavaDoc) envCtx.lookup(dataSourceName);
45     }
46
47     /**
48      * @see de.laures.cewolf.DatasetProducer#produceDataset(Map)
49      */

50     public XYSeries produceXYSeries() throws DatasetProduceException {
51         XYSeries series = new XYSeries(seriesName);
52         try {
53             DataSource JavaDoc ds = getDataSource();
54             Connection JavaDoc con = ds.getConnection();
55             ResultSet JavaDoc rs = con.createStatement().executeQuery(query);
56             int xColIndex = rs.findColumn(xCol);
57             int yColIndex = rs.findColumn(yCol);
58             while(rs.next()){
59                 series.add((Number JavaDoc)rs.getObject(xColIndex), (Number JavaDoc)rs.getObject(yColIndex));
60             }
61         } catch (Exception JavaDoc namingEx) {
62             namingEx.printStackTrace();
63             throw new DatasetProduceException(namingEx.getMessage());
64         }
65         return series;
66     }
67 }
68
Popular Tags