Example usage for org.apache.commons.dbutils QueryRunner QueryRunner

List of usage examples for org.apache.commons.dbutils QueryRunner QueryRunner

Introduction

In this page you can find the example usage for org.apache.commons.dbutils QueryRunner QueryRunner.

Prototype

public QueryRunner(DataSource ds) 

Source Link

Document

Constructor for QueryRunner that takes a DataSource to use.

Usage

From source file:com.pinterest.deployservice.db.DBDataDAOImpl.java

@Override
public void delete(String id) throws Exception {
    new QueryRunner(dataSource).update(DELETE_DATA, id);
}

From source file:com.neu.controller.MessageController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    DataSource ds = (DataSource) this.getApplicationContext().getBean("myDataSource");

    String action = request.getParameter("action");
    ModelAndView mv = new ModelAndView();

    HttpSession session = request.getSession();
    String userName = (String) session.getAttribute("userName");

    if (action.equalsIgnoreCase("reply")) {

        try {//from w w  w  .  jav a  2  s .  c om

            String receiver = request.getParameter("to");
            System.out.println("Printing receiver in reply case: " + receiver);

            QueryRunner run = new QueryRunner(ds);
            ResultSetHandler<UsersBean> user = new BeanHandler<UsersBean>(UsersBean.class);
            UsersBean ub = run.query("select * from userstable where userName =?", user, receiver);
            if (ub != null) {
                System.out.println("printing userEmail received from DB: " + ub.getUserEmail());

                mv.addObject("toEmail", ub.getUserEmail());
                mv.addObject("to", receiver);
            }

            mv.setViewName("reply");

        } catch (SQLException e) {
            System.out.println(e);

        }

    } else if (action.equalsIgnoreCase("sent")) {
        System.out.println("In sent case");

        try {
            String receiver = request.getParameter("to");
            String receiverEmail = request.getParameter("toEmail");

            System.out.println("printing receiver email: " + receiverEmail);

            QueryRunner run = new QueryRunner(ds);
            ResultSetHandler<UsersBean> user = new BeanHandler<UsersBean>(UsersBean.class);
            UsersBean ub = run.query("select * from userstable where userName =?", user, userName);
            if (ub != null) {
                String senderEmail = ub.getUserEmail();
                System.out.println("printing senderemail: " + senderEmail);

                ResultSetHandler<MessageBean> msg = new BeanHandler<MessageBean>(MessageBean.class);
                Object[] params = new Object[4];
                params[0] = userName;
                params[1] = request.getParameter("message");
                Date d = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
                String messageDate = format.format(d);
                params[2] = messageDate;
                params[3] = receiver;
                int inserts = run.update(
                        "Insert into messages (fromUser,message,messageDate,userName) values(?,?,?,?)", params);//Logic to send the email

                try {
                    Email email = new SimpleEmail();
                    email.setHostName("smtp.googlemail.com");//If a server is capable of sending email, then you don't need the authentication. In this case, an email server needs to be running on that machine. Since we are running this application on the localhost and we don't have a email server, we are simply asking gmail to relay this email.
                    email.setSmtpPort(465);
                    email.setAuthenticator(
                            new DefaultAuthenticator("contactapplication2017@gmail.com", "springmvc"));
                    email.setSSLOnConnect(true);
                    email.setFrom(senderEmail);//This email will appear in the from field of the sending email. It doesn't have to be a real email address.This could be used for phishing/spoofing!
                    email.setSubject("Thanks for Signing Up!");
                    email.setMsg("Welcome to Web tools Lab 5 Spring Application sign up email test!");
                    email.addTo(receiverEmail);//Will come from the database
                    email.send();
                } catch (Exception e) {
                    System.out.println("Email Exception" + e.getMessage());
                    e.printStackTrace();
                }
                mv.setViewName("messageSent");
            } else {
                mv.addObject("error", "true");
                mv.setViewName("index");

            }

        } catch (Exception ex) {
            System.out.println("Error Message" + ex.getMessage());
            ex.printStackTrace();

        }

    }

    return mv;
}

From source file:com.pinterest.deployservice.db.DBConfigHistoryDAOImpl.java

@Override
public void insert(ConfigHistoryBean bean) throws Exception {
    SetClause setClause = bean.genSetClause();
    String clause = String.format(INSERT_CONFIG_HISTORY, setClause.getClause());
    new QueryRunner(dataSource).update(clause, setClause.getValueArray());
}

From source file:com.pinterest.deployservice.db.DBRatingsDAOImpl.java

@Override
public void delete(String id) throws Exception {
    new QueryRunner(dataSource).update(DELETE_RATING, id);
}

From source file:de.iritgo.aktario.jdbc.LoadAllUsers.java

/**
 * Perform the command./* ww w  . j a  v  a2s.c o  m*/
 */
public void perform() {
    JDBCManager jdbcManager = (JDBCManager) Engine.instance().getManager("persist.JDBCManager");
    DataSource dataSource = jdbcManager.getDefaultDataSource();

    final UserRegistry userRegistry = Server.instance().getUserRegistry();

    try {
        QueryRunner query = new QueryRunner(dataSource);
        List userIds = (List) query.query("select id from IritgoUser", new ArrayListHandler());

        for (Iterator i = userIds.iterator(); i.hasNext();) {
            Long userId = (Long) ((Object[]) i.next())[0];

            Properties props = new Properties();

            props.put("id", userId);
            CommandTools.performSimple("persist.LoadUser", props);
        }

        Log.logVerbose("persist", "LoadAllUsers", "Successfully loaded " + userIds.size() + " users");
    } catch (Exception x) {
        Log.logError("persist", "LoadAllUsers", "Error while loading the users: " + x);
    }
}

From source file:com.pinterest.deployservice.db.DBPromoteDAOImpl.java

@Override
public List<String> getAutoPromoteEnvIds() throws Exception {
    return new QueryRunner(dataSource).query(GET_AUTO_PROMOTE_ENV_IDS,
            SingleResultSetHandlerFactory.<String>newListObjectHandler());
}

From source file:com.pinterest.deployservice.db.DBScheduleDAOImpl.java

@Override
public void insert(ScheduleBean scheduleBean) throws Exception {
    SetClause setClause = scheduleBean.genSetClause();
    String clause = String.format(INSERT_SCHEDULE, setClause.getClause());
    new QueryRunner(dataSource).update(clause, setClause.getValueArray());
}

From source file:com.pinterest.arcee.db.DBNewInstanceReportDAOImpl.java

@Override
public NewInstanceReportBean getByIds(String hostId, String envId) throws Exception {
    ResultSetHandler<NewInstanceReportBean> h = new BeanHandler<>(NewInstanceReportBean.class);
    return new QueryRunner(dataSource).query(GET_BY_IDS, h, hostId, envId);
}

From source file:gr.osmosis.rcpsamples.contact.db.derby.DerbyContactsDAO.java

public boolean deleteContact(int id) {

    int rows = 0;

    try {/*from  w  ww  . jav  a 2s  .c o m*/
        StringBuffer sbDelete = new StringBuffer();

        sbDelete.append("DELETE FROM ");
        sbDelete.append(ContactsConstants.CONTACTS_TABLE_NAME);
        sbDelete.append(" WHERE ");
        sbDelete.append(ContactsConstants.CONTACTS_COL_ID + " = " + id);

        DataSource d = DerbyDAOFactory.getDataSource();
        QueryRunner run = new QueryRunner(d);

        rows = run.update(sbDelete.toString());

        if (rows != 1) {
            throw new SQLException("executeUpdate return value: " + rows);
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
        return false;
    }

    return true;

}

From source file:com.pinterest.deployservice.db.DBAgentErrorDAOImpl.java

@Override
public void insert(AgentErrorBean agentErrorBean) throws Exception {
    SetClause setClause = agentErrorBean.genSetClause();
    String clause = String.format(INSERT_ERROR_TEMPLATE, setClause.getClause());
    new QueryRunner(dataSource).update(clause, setClause.getValueArray());
}