for ( int i = 1; i <= rsmd.getColumnCount(); ++i ) switch( rsmd.getColumnType( i ) ) { case Types.VARCHAR: currentRow.addElement( rs.getString( i ) ); break; case Types.INTEGER: currentRow.addElement( new Long( rs.getLong( i ) ) ); break; default: } return currentRow; }
public static void main( String args[] ) { final Reporting app = new Reporting();
app.addWindowListener(new WindowAdapter() { public void windowClosing( WindowEvent e ) { app.shutDown(); System.exit( 0 );
} } ); } }
///**************/// and to print and print priview with the code: import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import java.text.*; import java.util.Date; import java.sql.*; import java.awt.print.*; // NEW
protected JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar();
JMenu mFile = new JMenu("File"); mFile.setMnemonic('f');
JMenuItem mData = new JMenuItem("Retrieve Data..."); mData.setMnemonic('r'); ActionListener lstData = new ActionListener() { public void actionPerformed(ActionEvent e) { retrieveData(); } }; mData.addActionListener(lstData); mFile.add(mData); mFil e.addSeparator();
// NEW JMenuItem mPrint = new JMenuItem("Print..."); mPrint.setMnemonic('p'); ActionListener lstPrint = new ActionListener() { public void actionPerformed(ActionEvent e) { Thread runner = new Thread() { public void run() { printData(); } }; runner.start(); } }; mPrint.addActionListener(lstPrint); mF ile.add(mPrint);
JMenuItem mPreview = new JMenuItem("Print Preview"); mPreview.setMnemonic('v'); ActionListener lstPreview = new ActionListener() { public void actionPerformed(ActionEvent e) { Thread runner = new Thread() { public void run() { setCursor(Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR)); PrintPreview preview = new PrintPreview( StocksTable.this, m_title.getText()+" Preview"); preview.setVisible(true); setCursor(Cursor.getPredefinedCursor( Curs or.DEFAULT_CURSOR)); } }; runner.start(); } }; mPreview.addActionListener(lstPre view); mFile.add(mPreview); mFile.addSeparator();
JMenuItem mExit = new JMenuItem("Exit"); mExit.setMnemonic('x'); ActionListener lstExit = new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }; mExit.addActionListener(lstExit); mFile.add(mExit); menu Bar.add(mFile);
JMenu mView = new JMenu("View"); mView.setMnemonic('v'); TableColumnModel model = m_table.getColumnModel(); for (int k = 0; k < m_data.getColumnCount(); k++) { JCheckBoxMenuItem item = new JCheckBoxMenuItem( StockTableData.m_columns[k].m_title); item.setSelected(true) ; TableColumn column = model.getColumn(k); item.addActionListener(new ColumnKeeper(column, StockTableData.m_columns[k])); mView.add(item); } menuBar. add(mView);
return menuBar; }
// NEW public void printData() { try { PrinterJob prnJob = PrinterJob.getPrinterJob(); prnJob.setPrintable(this); if (!prnJob.printDialog()) return; m_maxNumPage = 1; prnJob.print(); } catch (PrinterException e) { e.printStackTrace(); } }
// NEW public int print(Graphics pg, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex >= m_maxNumPage) return NO_SUCH_PAGE;
int y = 0; pg.setFont(m_title.getFont()); pg.setColor(Color.black); Font fn = pg.getFont(); FontMetrics fm = pg.getFontMetrics(); y += fm.getAscent(); pg.drawString(m_title.getText(), 0, y); y += 20; // space between title and table headers
Font headerFont = m_table.getFont().deriveFont(Font.BOLD); pg.setFont(headerFont); fm = pg.getFontMetrics();
TableColumnModel colModel = m_table.getColumnModel(); int nColumns = colModel.getColumnCount(); int x[] = new int[nColumns]; x[0] = 0;
int h = fm.getAscent(); y += h; // add ascent of header font because of baseline // positioning (see figure 2.10)
int nRow, nCol; for (nCol=0; nCol<nColumns; nCol++) { TableColumn tk = colModel.getColumn(nCol); int width = tk.getWidth(); if (x[nCol] + width > wPage) { nColumns = nCol; break; } if (nCol+1<nColumns) x[nCol+1] = x[nCol] + width; String title = (String)tk.getIdentifier(); pg.drawString(title, x[nCol], y); }
pg.setFont(m_table.getFont()); fm = pg.getFontMetrics();
int header = y; h = fm.getHeight(); int rowH = Math.max((int)(h*1.5), 10); int rowPerPage = (hPage-header)/rowH; m_maxNumPage = Math.max((int)Math.ceil(m_table.getRowCount()/ (double)rowPerPage), 1);
TableModel tblModel = m_table.getModel(); int iniRow = pageIndex*rowPerPage; int endRow = Math.min(m_table.getRowCount(), iniRow+rowPerPage);
for (nRow=iniRow; nRow<endRow; nRow++) { y += h; for (nCol=0; nCol<nColumns; nCol++) { int col = m_table.getColumnModel().getColumn(nCol).getModelIndex(); Object obj = m_data.getValueAt(nRow, col); String str = obj.toString(); if (obj instanceof ColorData) pg.setColor(((ColorData)obj).m_color); else pg.setColor(Color.black) ; pg.drawString(str, x[nCol], y); } }
System.gc(); return PAGE_EXISTS; }
public void retrieveData() { Runnable updater = new Runnable() { public void run() { SimpleDateFormat frm = new SimpleDateFormat("MM/dd/yyyy"); String currentDate = frm.format(m_data.m_date); String result = (String)JOptionPane.showInputDialog(StocksTable.this, "Please enter date in form mm/dd/yyyy:", "Input", JOptionPane.INFORMATION_MESSAGE, null, null, currentDate); if (result==null) return;
java.util.Date date = null; try { date = frm.parse(result); } catch (java.text.ParseException ex) { date = null; }
if (date == null) { JOptionPane.showMessageDialog(StocksTable.this, result+" is not a valid date", "Warning", JOptionPane.WARNING_MESSAGE); return; }
public String toString() { return m_data.toString(); } }
class StockData {
public static ImageIcon ICON_UP = new ImageIcon("ArrUp.gif"); public static ImageIcon ICON_DOWN = new ImageIcon("ArrDown.gif"); public static ImageIcon ICON_BLANK = new ImageIcon("blank.gif");
public IconData m_symbol; public String m_name; public Double m_last; public Double m_open; public ColorData m_change; public ColorData m_changePr; public Long m_volume;
public StockData(String symbol, String name, double last, double open, double change, double changePr, long volume) { m_symbol = new IconData(getIcon(change), symbol); m_name = name; m_last = new Double(last); m_open = new Double(open); m_change = new ColorData(new Double(change)); m_changePr = new ColorData(new Double(changePr)); m_volume = new Long(volume); }
public int getRowCount() { return m_vector==null ? 0 : m_vector.size(); }
public int getColumnCount() { return m_columnsCount; }
public String getColumnName(int column) { return m_columns[column].m_title; }
public Icon getColumnIcon(int column) { if (column==m_sortCol) return m_sortAsc ? COLUMN_UP : COLUMN_DOWN; return null; }
public boolean isCellEditable(int nRow, int nCol) { return false; }
public Object getValueAt(int nRow, int nCol) { if (nRow < 0 || nRow>=getRowCount()) return ""; StockData row = (StockData)m_vector.elementAt(nRow); switch (nCol) { case 0: return row.m_symbol; case 1: return row.m_name; case 2: return row.m_last; case 3: return row.m_open; case 4: return row.m_change; case 5: return row.m_changePr; case 6: return m_volumeFormat.format(row.m_volume); } return ""; }
public String getTitle() { if (m_date==null) return "Stock Quotes"; return "Stock Quotes at "+m_frm.format(m_date); }
public void sortData() { Collections.sort(m_vector, new StockComparator(m_sortCol, m_sortAsc)); }
static final String QUERY = "SELECT data.symbol, symbols.name, "+ "data.last, data.open, data.change, data.changeproc, "+ "data.volume FROM DATA INNER JOIN SYMBOLS "+ "ON DATA.symbol = SYMBOLS.symbol WHERE "+ "month(data.date1)=? AND day(data.date1)=?"+ " AND year(data.date1)=?";
public void retrieveData(Date date) throws SQLException, ClassNotFoundException {
GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(date); int month = calendar.get(Calendar.MONTH)+1; int day = calendar.get(Calendar.DAY_OF_MONTH); int year = calendar.get(Calendar.YEAR);