Example usage for org.hibernate Query setString

List of usage examples for org.hibernate Query setString

Introduction

In this page you can find the example usage for org.hibernate Query setString.

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setString(String name, String val) 

Source Link

Document

Bind a named String-valued parameter.

Usage

From source file:com.candy.db.FundamentalDataProc.java

/**
 * select one record by date(year,quarter) and type TODO year and quarter
 * @param symbol/* www.jav a 2  s .  c  o m*/
 * @param type
 * @param year
 * @param quarter
 * @return 
 */
public FundamentalDataRec readDataByDate(String symbol, int type, int year, int quarter) {
    try {
        begin();
        Query q = getSession().createQuery(
                "from Fundamentaldata where symbol = :symbol and year = :year and quarter = :quarter and type = :type");
        q.setString("symbol", symbol);
        q.setInteger("year", year);
        q.setInteger("quarter", quarter);
        q.setInteger("type", type);
        Fundamentaldata fdata = (Fundamentaldata) q.uniqueResult();
        commit();

        if (fdata == null)
            return null;
        FundamentalDataRec rec = new FundamentalDataRec();
        for (int i = 0; i < methodGetNameLst.length; i++) {
            try {
                Double value = (Double) methodGetValueLst[i].invoke(fdata); // return double
                String name = (String) methodGetNameLst[i].invoke(fdata);
                if (name != null) {
                    rec.setNameValue(name, value);
                }
            } catch (Exception e) {
                // skip it
                System.out.println("ERROR - unable to get column value/name = " + i);
            }
        }
        return rec;
    } catch (HibernateException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.candy.db.FundamentalDataProc.java

/**
 * read records from table/*  w  w w  .  ja v  a  2s  . c  o  m*/
 * @param symbol
 * @param count
 * @param qtv
 * @return 
 */
public ArrayList<FundamentalDataRec> readDataByCount(String symbol, int type, int count, boolean qtv) {
    try {
        begin();
        Query q = null;
        if (qtv)
            q = getSession().createQuery(
                    "from Fundamentaldata where symbol = :symbol and quarter != 0 and type = :type order by year desc");
        else
            q = getSession().createQuery(
                    "from Fundamentaldata where symbol = :symbol and quarter = 0 and type = :type order by year desc");
        q.setString("symbol", symbol);
        q.setInteger("type", type);
        if (count != 0) {
            q.setFirstResult(0);
            q.setMaxResults(count);
        }
        List<Fundamentaldata> results = q.list();
        commit();
        if (results.isEmpty()) {
            return null;
        } else {
            ArrayList<FundamentalDataRec> retLst = new ArrayList();
            for (Fundamentaldata fdata : results) {
                FundamentalDataRec rec = new FundamentalDataRec();
                rec.year = fdata.getId().getYear();
                rec.quarter = fdata.getId().getQuarter();
                rec.setType(fdata.getId().getType());
                for (int i = 0; i < methodGetValueLst.length; i++) {
                    try {
                        Double value = (Double) methodGetValueLst[i].invoke(fdata); // return double
                        String name = (String) methodGetNameLst[i].invoke(fdata);
                        if (name != null) {
                            rec.setNameValue(name, value);
                        }
                        //                            else {  // TODO
                        //                                System.out.println("ERROR - fundamental name is null " + i);
                        //                            }
                    } catch (Exception e) {
                        System.out.println("ERROR - unable to get column value/name = " + i);
                    }
                }
                retLst.add(rec);
            }
            return retLst;
        }
    } catch (HibernateException e) {
        return null;
    }
}

From source file:com.candy.db.FundamentalDataProc.java

/**
 * //from w w w.  j ava 2 s .c  o  m
 * @param symbol
 * @param type
 * @param year
 * @param quarter
 * @return 
 */
//    @Deprecated
//    public boolean isDataExist(String symbol, int type, int year, int quarter) {
//        try {
//            begin();
//            Query q = getSession().createQuery("from Fundamentaldata where symbol = :symbol and year = :year and quarter = :quarter and type = :type");            
//            q.setString("symbol", symbol);
//            q.setInteger("year", year);
//            q.setInteger("quarter", quarter);
//            q.setInteger("type", type);
//            boolean exist = (q.uniqueResult() != null);
//            commit();
//            return exist;
//        }catch (HibernateException e) {
//            return false;
//        }        
//    }

public boolean isDataExistByFileName(String symbol, String fileName, int type) {
    try {
        begin();
        Query q = getSession().createQuery(
                "from Fundamentaldata where symbol = :symbol and filename = :filename and type = :type");
        q.setString("symbol", symbol);
        q.setInteger("type", type);
        q.setString("filename", fileName);
        boolean exist = (q.uniqueResult() != null);
        commit();
        return exist;
    } catch (HibernateException e) {
        return false;
    }
}

From source file:com.candy.db.FundamentalDataProc.java

public ArrayList<FundamentalDataRec> getLastQData(String symbol, int lastQoffset) {
    try {/*  www  .  jav  a2  s .  co  m*/
        Query q = getSession().createQuery(
                "from Fundamentaldata where symbol = :symbol and quarter > :offset order by year desc");
        q.setString("symbol", symbol);
        q.setInteger("offset", lastQoffset);
        List<Fundamentaldata> results = q.list();
        if (results.isEmpty()) {
            return null;
        } else {
            ArrayList<FundamentalDataRec> retLst = new ArrayList();
            for (Fundamentaldata fdata : results) {
                FundamentalDataRec rec = new FundamentalDataRec();
                rec.year = fdata.getId().getYear();
                rec.quarter = fdata.getId().getQuarter() - lastQoffset;
                rec.setType(fdata.getId().getType());
                for (int i = 0; i < methodGetValueLst.length; i++) {
                    try {
                        Double value = (Double) methodGetValueLst[i].invoke(fdata); // return double
                        String name = (String) methodGetNameLst[i].invoke(fdata);
                        if (name != null) {
                            rec.setNameValue(name, value);
                        }
                    } catch (Exception e) {
                        System.out.println("ERROR - unable to get column value/name = " + i);
                    }
                }
                retLst.add(rec);
            }
            return retLst;
        }
    } catch (HibernateException e) {
        return null;
    }
}

From source file:com.candy.db.FundamentalDataProc.java

/**
 * delete UnProcessed Q data//from   www  .  jav a2  s  . c  om
 * @param symbol
 * @param year
 * @param quarter
 * @param type 
 */
public boolean deleteQData(String symbol, int year, int oldQtr, int type) {
    Session session = getSession();
    try {
        begin();
        Query q = session.createQuery(
                "delete from Fundamentaldata where symbol = :symbol and year = :year and quarter =:quarter and type = :type");
        q.setString("symbol", symbol);
        q.setInteger("year", year);
        q.setInteger("quarter", oldQtr);
        q.setInteger("type", type);
        int rowCount = q.executeUpdate();
        commit();
        return (rowCount == 1);
    } catch (HibernateException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.candy.db.StockEodProc.java

/**
 * query and return ordered by date historical data
 * @param symbol/*  w ww  .  j  a v a 2 s  .  c om*/
 * @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;
    }
}

From source file:com.candy.db.StockEodProc.java

/**
 * get last available record's date//from www.  j  a v  a2 s  . c om
 * @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;
    }
}

From source file:com.cantor.ipplan.server.ProfileServiceImpl.java

License:Open Source License

@Override
public boolean checkUser(String name, String email) throws Exception {
    checkLogin();//from   w w  w. j a v  a2s  . c o  m

    SessionFactory sessionFactory = (SessionFactory) getServletContext().getAttribute("sessionFactory");
    Session session = sessionFactory.openSession();
    try {
        try {
            Query q = session.createQuery(
                    "select u from PUser u " + "where u.puserLogin=:login AND u.puserEmail=:email");
            q.setString("login", name);
            q.setString("email", email);
            PUser u = (PUser) q.uniqueResult();
            return u != null;
        } catch (Exception e) {
            Ipplan.error(e);
        }
    } finally {
        session.close();
    }
    return false;
}

From source file:com.cantor.ipplan.server.ProfileServiceImpl.java

License:Open Source License

private PUser getUserByEmail(Session session, String email) {
    Query q = session.createQuery("select u from PUser u " + "where u.puserEmail=:email");
    q.setString("email", email);
    return (PUser) q.uniqueResult();
}

From source file:com.carmanage.dao.impl.BusDaoImpl.java

public void deleteBusByString(String plate) {
    Session session = sessionFactory.openSession();
    Query query = session.createQuery("delete from Car e where e.plate=?");
    query.setString(0, plate);
    query.executeUpdate();// ww  w.j  av  a 2s  .com
    session.flush();
    session.close();
}