/* * ------------------------------------------------------------------------- * $Id: SampleJDBC.java,v 1.5 2004/05/26 20:30:01 estewart Exp $ * ------------------------------------------------------------------------- * Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved. * * This software is confidential information which is proprietary to * and a trade secret of Visual Numerics, Inc. Use, duplication or * disclosure is subject to the terms of an appropriate license * agreement. * * VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL * NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR * ITS DERIVATIVES. *-------------------------------------------------------------------------- */ package com.imsl.demo.SampleJDBC; /** * Use a simple select statement to read all values in a MS Access * database using the JDBC-ODBC bridge. * The results are printed to the screen, but can be collected and * sent to a IMSL Java Library chart type for display * * @author Jerry Mariani * @created October 23, 2002 */ public class SampleJDBC extends com.imsl.chart.JFrameChart { static final int MAXROWS = 50; public SampleJDBC(boolean exitOnClose) { if (!exitOnClose) { Object l[] = getListeners(java.awt.event.WindowListener.class); for (int k = 0; k < l.length; k++) { removeWindowListener((java.awt.event.WindowListener) l[k]); } } com.imsl.demo.gallery.Describe des = new com.imsl.demo.gallery.Describe(this, "/com/imsl/demo/SampleJDBC/JDBC.html"); des.show(); java.awt.Dimension dess = des.getSize(); java.awt.Dimension ss = getToolkit().getScreenSize(); int w = Math.min(ss.width/2, ss.height-dess.height-32); setSize(w, w); setLocation(ss.width-dess.width, dess.height); setTitle("JDBC Query Results"); int id; java.sql.Date dt; float open, high, low, close; int cnt = 0; StringBuffer result; // set up the chart com.imsl.chart.Chart chart = getChart(); com.imsl.chart.AxisXY axis = new com.imsl.chart.AxisXY(chart); axis.getAxisX().getAxisTitle().setTitle(" Date"); axis.getAxisX().getAxisLabel().setTextFormat(new java.text.SimpleDateFormat("MM/dd/yy")); axis.getAxisX().setCustomTransform(new com.imsl.chart.TransformDate()); axis.getAxisX().setTransform(com.imsl.chart.AxisXY.TRANSFORM_CUSTOM); axis.getAxisX().getAxisLabel().setTextAngle(90); axis.getAxisX().setSkipWeekends(true); axis.getAxisY().getAxisTitle().setTitle("Price of Gold per Ounce"); axis.getAxisY().getAxisLabel().setTextFormat(new java.text.DecimalFormat("$ #0")); // set up collector java.util.Date startDate = new java.util.Date(); double listOpen[] = new double[MAXROWS]; double listHigh[] = new double[MAXROWS]; double listLow[] = new double[MAXROWS]; double listClose[] = new double[MAXROWS]; final java.text.DecimalFormat df = new java.text.DecimalFormat("##0.00"); // the included xau.mdb must be registered in the ODBC Manager as "xau" // see the JDBC.html docs for more information. try { // connect to ODBC database Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:xau"; // connect java.sql.Connection con = java.sql.DriverManager.getConnection(url, "xau", "sql"); // create Statement object java.sql.Statement stmt = con.createStatement(); stmt.setMaxRows(MAXROWS); String sqlselect = "Select id, date, open, high, low, close from xau"; // run query java.sql.ResultSet rs = stmt.executeQuery(sqlselect); // process results while(rs.next()) { result = new StringBuffer(); id = rs.getInt(1); dt = rs.getDate(2); open = rs.getFloat(3); high = rs.getFloat(4); low = rs.getFloat(5); close = rs.getFloat(6); result.append(id + " "); result.append(dt.toString() + " "); result.append(df.format(open) + " "); result.append(df.format(high) + " "); result.append(df.format(low) + " "); result.append(df.format(close)); System.out.println("Values are: " + result); // collect data into something to pass to the charting routines here if (cnt == 0) { startDate = dt; } listOpen[cnt] = open; listHigh[cnt] = high; listLow[cnt] = low; listClose[cnt] = close; cnt++; } //close connection con.close(); } catch (Exception e) { String name = e.toString().substring(0,e.toString().indexOf(':')); String text = e.getMessage() + "\nPlease review the documentation JDBC.html for more information"; javax.swing.JOptionPane.showMessageDialog(this, e.getMessage(), name, javax.swing.JOptionPane.ERROR_MESSAGE); e.printStackTrace(); return; } // define data for the chart com.imsl.chart.Candlestick cs = new com.imsl.chart.Candlestick(axis, startDate, listHigh, listLow, listClose, listOpen); cs.getUp().setFillColor("green"); cs.getDown().setFillColor("red"); } public static void main(String args[]) { boolean exitOnClose = true; if (args.length > 0 && args[0].equals("-noexit")) { exitOnClose = false; } new SampleJDBC(exitOnClose).show(); } }