Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.candy.db; import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author ruizhou */ public class StockEodProc extends HibernateBase { private static class Holder { static final StockEodProc INSTANCE = new StockEodProc(); } public static StockEodProc getInstance() { return Holder.INSTANCE; } private final StockeodId dataKey = new StockeodId(); /** * insert the records into database * @param stockEodLst * @return */ public boolean writeHisStockData(Collection<Stockeod> stockEodLst) { try { Session session = sf.openSession(); Transaction tx = session.beginTransaction(); int count = 0; for (Stockeod stockEod : stockEodLst) { session.save(stockEod); if (++count % 20 == 0) { session.flush(); session.clear(); } } tx.commit(); session.close(); return true; } catch (HibernateException e) { e.printStackTrace(); return false; } } /** * query and return ordered by date historical data * @param symbol * @param startCal * @param endCal * @return */ public List<Stockeod> getHisStockData(String symbol, Calendar startCal, Calendar endCal) { try { begin(); Query q = getSession().createQuery( "from Stockeod where symbol = :symbol and sdate <= :endDate and sdate >= :startDate order by sdate asc"); q.setString("symbol", symbol); q.setDate("endDate", new java.sql.Date(endCal.getTimeInMillis())); q.setDate("startDate", new java.sql.Date(startCal.getTimeInMillis())); List<Stockeod> results = q.list(); commit(); if (!results.isEmpty()) { return results; } else { return null; } } catch (HibernateException e) { return null; } } /** * get last available record's date * @param symbol * @return */ public Calendar getHisStockDataLastAvaDate(String symbol) { try { begin(); Query q = getSession().createQuery("from Stockeod where symbol = :symbol order by sdate desc"); q.setString("symbol", symbol); q.setFirstResult(0); q.setMaxResults(1); Stockeod stockEod = (Stockeod) q.uniqueResult(); commit(); if (stockEod != null) { Date date = stockEod.getId().getSdate(); Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } else { return null; } } catch (HibernateException e) { e.printStackTrace(); return null; } } } /** * * @param symbol */ // public HisStockData getLastRecord(String symbol) { // try { // begin(); // Query q = getSession().createQuery("from Stockeod where symbol = :symbol order by sdate desc"); // q.setString("symbol", symbol); // q.setFirstResult(0); // q.setMaxResults(1); // // Stockeod stdata = (Stockeod)q.uniqueResult(); // commit(); // // if (stdata == null) // return null; // else { // HisStockData hisStockData = new HisStockData(); // StockeodId key = stdata.getId(); // hisStockData.addPriceData(key.getSdate(), stdata.getSopen(), stdata.getSclose(), stdata.getShigh(), data.getSlow(), data.getSadjclose()); // hisStockData.setSymbol(key.getSymbol()); // // } // if (!results.isEmpty()) { // HisStockData hisStockData = new HisStockData(); // for (Stockeod data : results) { // StockeodId key = data.getId(); // hisStockData.addPriceData(key.getSdate(), data.getSopen(), data.getSclose(), data.getShigh(), data.getSlow(), data.getSadjclose()); // } // return hisStockData; // } else { // return null; // } // } catch (HibernateException e) { // return null; // } // }