Example usage for org.apache.ibatis.session SqlSession update

List of usage examples for org.apache.ibatis.session SqlSession update

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession update.

Prototype

int update(String statement, Object parameter);

Source Link

Document

Execute an update statement.

Usage

From source file:action.manageParticipatedPost.java

public String exitparticipation() throws IOException {

    String resource = "orm/configuration.xml";
    Reader reader = Resources.getResourceAsReader(resource);
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession session = sessionFactory.openSession();

    da = new driverapply();

    System.out.println("selected passenger's postID in exit=" + postID);
    System.out.println("Driver's email in exit=" + postID);

    try {//  ww w  .j  av  a  2  s .  c o m
        //user's email as a driver
        email = ActionContext.getContext().getSession().get("email").toString();
        int res;
        res = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the participation?", "Confirm",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (res == JOptionPane.YES_OPTION) {
            //select record from postaspassenger table, get its numberofapplieddrivers
            paplist = session.selectList("selectPassengerPostByID", postID);
            numberofapplieddrivers = paplist.get(0).getNumberofapplieddrivers();

            //delete participation record from driverapply table
            da.setDriveremail(email);
            da.setPassengerpostID(postID);
            session.delete("deleteparticipation", da);
            session.commit();

            //update attribute of numberofapplieddrivers in postaspassenger table 
            pap = new postaspassenger();
            pap.setPostID(postID);
            pap.setNumberofapplieddrivers(String.valueOf(Integer.parseInt(numberofapplieddrivers) - 1));
            session.update("updatenumberofapplieddrivers", pap);
            session.commit();

            return "deleteandupdateok";
        } else {
            return "nodeleteandupdate";
        }
    } finally {
        session.close();
    }

}

From source file:action.manageParticipatedPost.java

public String exitparticipation2() throws IOException {

    String resource = "orm/configuration.xml";
    Reader reader = Resources.getResourceAsReader(resource);
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession session = sessionFactory.openSession();

    pa = new passengerapply();

    System.out.println("selected driver's postID in exit2=" + postID);
    System.out.println("passenger's email in exit2=" + postID);

    try {//from  w w  w .j  a v  a 2 s.  c o m
        //user's email as a passenger
        email = ActionContext.getContext().getSession().get("email").toString();
        int res;
        res = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the participation?", "Confirm",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (res == JOptionPane.YES_OPTION) {
            //select record from postasdriver table, get its numberofappliedpassengers
            padlist = session.selectList("selectPostByID", postID);
            numberofappliedpassengers = padlist.get(0).getNumberofappliedpassengers();

            //delete participation record from passengerapply table
            pa.setPassengeremail(email);
            pa.setDriverpostID(postID);
            ;
            session.delete("deleteparticipation2", pa);
            session.commit();

            //update attribute of numberofapplieddrivers in postaspassenger table 
            pad = new postasdriver();
            pad.setPostID(postID);
            pad.setNumberofappliedpassengers(String.valueOf(Integer.parseInt(numberofappliedpassengers) - 1));
            session.update("updatenumberofappliedpassengers", pad);
            session.commit();

            JOptionPane.showMessageDialog(null, "You have exited the participation successfully!", "Message",
                    JOptionPane.INFORMATION_MESSAGE);

            return "deleteandupdateok2";
        } else {

            return "nodeleteandupdate2";
        }
    } finally {
        session.close();
    }

}

From source file:action.register.java

public String change() throws IOException {

    String resource = "orm/configuration.xml";
    Reader reader = Resources.getResourceAsReader(resource);
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession session = sessionFactory.openSession();
    System.out.println("original pw=" + originalpassword);
    System.out.println("pw=" + password);
    System.out.println("confirm pw=" + confirmpassword);

    try {// ww w. j  a va 2  s  . c om
        if (originalpassword.equals("") || password.equals("") || confirmpassword.equals("")) {
            JOptionPane.showMessageDialog(null, "No Empty!", "Message", JOptionPane.INFORMATION_MESSAGE);
            return "fail";
        } else {
            //get user's email address
            email = ActionContext.getContext().getSession().get("email").toString();
            userlist = session.selectList("selectuserbyemail", email);
            if (!userlist.get(0).getPassword().equals(originalpassword)) {
                JOptionPane.showMessageDialog(null, "Your original password is incorrect!", "Message",
                        JOptionPane.INFORMATION_MESSAGE);
                return "fail";
            } else if (!password.equals(confirmpassword)) {
                JOptionPane.showMessageDialog(null, "Two passwords don't match, please type it agian!",
                        "Message", JOptionPane.INFORMATION_MESSAGE);
                return "fail";
            } else {
                newuser = new user();
                newuser.setEmail(email);
                newuser.setPassword(password);
                session.update("updatepassword", newuser);
                session.commit();
                JOptionPane.showMessageDialog(null, "Updated Successfully!", "Message",
                        JOptionPane.INFORMATION_MESSAGE);
                return SUCCESS;

            }
        }

    } finally {
        session.close();
    }
}

From source file:action.reviseProfile.java

public String revise() throws IOException {

    String resource = "orm/configuration.xml";
    Reader reader = Resources.getResourceAsReader(resource);
    SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
    SqlSession session = sessionFactory.openSession();

    try {//  ww  w .j  a  v a  2s  . c om
        email = ActionContext.getContext().getSession().get("email").toString();

        //validate driverlicense
        String checkdl = "[0-9]{8}";
        Pattern regex = Pattern.compile(checkdl);
        java.util.regex.Matcher matcher = regex.matcher(driverlicense);
        boolean isMatched = matcher.matches();

        //check whether the email address is valid

        if (name.equals("") || sex.equals("") || dob.equals("")) {

            JOptionPane.showMessageDialog(null, "Please fill required information!", "Message",
                    JOptionPane.WARNING_MESSAGE);
            return "fail";

        } else if (!driverlicense.equals("")) {
            if (!isMatched) {
                JOptionPane.showMessageDialog(null, "Driver License should only be 8 numeric numbers!",
                        "Message", JOptionPane.ERROR_MESSAGE);
                return "fail";
            }

            else {
                //update user info
                u = new user();
                u.setEmail(email);
                u.setName(name);
                u.setDob(dob);
                u.setDriverlicense(driverlicense);
                u.setSex(sex);
                session.update("updateuserinfo", u);
                session.commit();
                JOptionPane.showMessageDialog(null, "Updated Successfully!", "Message",
                        JOptionPane.INFORMATION_MESSAGE);
                return "updatedsuccessfully";
            }
        }

        else {
            //update user's info
            System.out.println("email in revise=" + email);
            System.out.println("name in revise=" + name);
            System.out.println("dob in revise=" + dob);
            System.out.println("driverlicense in revise=" + driverlicense);

            u = new user();
            u.setEmail(email);
            u.setName(name);
            u.setDob(dob);
            u.setDriverlicense(driverlicense);
            u.setSex(sex);
            session.update("updateuserinfo", u);
            session.commit();
            JOptionPane.showMessageDialog(null, "Updated Successfully!", "Message",
                    JOptionPane.INFORMATION_MESSAGE);
            return SUCCESS;
        }

    } finally {
        session.close();
    }

}

From source file:com.beginner.core.dao.DaoSupport.java

License:Apache License

/** 
 * {@inheritDoc}/*from ww w .j a  v  a2 s  . c o  m*/
 */
@Override
public void batchUpdate(String str, List objs) throws Exception {
    SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
    //?
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    try {
        if (objs != null) {
            for (int i = 0, size = objs.size(); i < size; i++) {
                sqlSession.update(str, objs.get(i));
            }
            sqlSession.flushStatements();
            sqlSession.commit();
            sqlSession.clearCache();
        }
    } finally {
        sqlSession.close();
    }
}

From source file:com.company.project.service.UserMapperImpl.java

License:Apache License

public int update(Map user) {
    SqlSession session = sqlSessionFactory.openSession();
    try {/*from   ww  w.  j ava 2 s. c  o  m*/
        return session.update("com.company.project.persistence.UserMapper.update", user);
    } catch (Exception e) {
        e.printStackTrace();
        session.rollback();
    } finally {
        session.close();
    }

    return 0;
}

From source file:com.glaf.core.db.TableDataManager.java

License:Apache License

/**
 * ?JSON?//from  w ww  .  jav  a2 s.c  o  m
 * 
 * @param tableName
 * @param jsonObject
 */
public void saveTableData(SqlSession sqlSession, String systemName, String tableName, JSONObject jsonObject) {
    com.glaf.core.config.Environment.setCurrentSystemName(systemName);
    TableDefinition tableDefinition = getTableDefinitionService().getTableDefinition(tableName);
    if (tableDefinition != null && tableDefinition.getIdColumn() != null) {
        if (tableDefinition.getTableName() != null) {
            tableDefinition.setTableName(tableDefinition.getTableName().toUpperCase());
        }
        ColumnDefinition idColumn = tableDefinition.getIdColumn();

        boolean insertData = true;
        Object primaryKey = null;
        if (idColumn.getColumnName() != null) {
            primaryKey = jsonObject.get(idColumn.getColumnName());
        } else if (idColumn.getName() != null) {
            primaryKey = jsonObject.get(idColumn.getName());
        }
        if (primaryKey != null) {
            insertData = false;
        }

        TableModel tableModel = new TableModel();
        tableModel.setTableName(tableName);

        List<ColumnDefinition> columns = tableDefinition.getColumns();
        if (columns != null && !columns.isEmpty()) {
            for (ColumnDefinition col : columns) {
                if (StringUtils.equalsIgnoreCase(idColumn.getColumnName(), col.getColumnName())) {
                    continue;
                }
                String javaType = col.getJavaType();
                String columnName = col.getColumnName();
                String name = col.getName();
                ColumnModel cm = new ColumnModel();
                cm.setJavaType(javaType);
                cm.setColumnName(col.getColumnName());
                Object value = null;

                if (jsonObject.containsKey(columnName)) {
                    value = jsonObject.get(columnName);
                    if (StringUtils.equalsIgnoreCase("Integer", javaType)) {
                        if (value instanceof Integer) {
                            cm.setValue(jsonObject.getInteger(columnName));
                        } else {
                            cm.setValue(Integer.parseInt(value.toString()));
                        }
                    } else if (StringUtils.equalsIgnoreCase("Long", javaType)) {
                        if (value instanceof Long) {
                            cm.setValue(jsonObject.getLong(columnName));
                        } else {
                            cm.setValue(Long.parseLong(value.toString()));
                        }
                    } else if (StringUtils.equalsIgnoreCase("Double", javaType)) {
                        if (value instanceof Double) {
                            cm.setValue(jsonObject.getDouble(columnName));
                        } else {
                            cm.setValue(Double.parseDouble(value.toString()));
                        }
                    } else if (StringUtils.equalsIgnoreCase("Date", javaType)) {
                        if (value instanceof Date) {
                            cm.setValue(jsonObject.getDate(columnName));
                        } else if (value instanceof Long) {
                            Long t = jsonObject.getLong(columnName);
                            cm.setValue(new Date(t));
                        } else {
                            cm.setValue(DateUtils.toDate(value.toString()));
                        }
                    } else if (StringUtils.equalsIgnoreCase("String", javaType)) {
                        if (value instanceof String) {
                            cm.setValue(jsonObject.getString(columnName));
                        } else {
                            cm.setValue(value.toString());
                        }
                    } else {
                        cm.setValue(value);
                    }
                } else if (jsonObject.containsKey(name)) {
                    value = jsonObject.get(name);
                    if (StringUtils.equalsIgnoreCase("Integer", javaType)) {
                        if (value instanceof Integer) {
                            cm.setValue(jsonObject.getInteger(name));
                        } else {
                            cm.setValue(Integer.parseInt(value.toString()));
                        }
                    } else if (StringUtils.equalsIgnoreCase("Long", javaType)) {
                        if (value instanceof Long) {
                            cm.setValue(jsonObject.getLong(name));
                        } else {
                            cm.setValue(Long.parseLong(value.toString()));
                        }
                    } else if (StringUtils.equalsIgnoreCase("Double", javaType)) {
                        if (value instanceof Double) {
                            cm.setValue(jsonObject.getDouble(name));
                        } else {
                            cm.setValue(Double.parseDouble(value.toString()));
                        }
                    } else if (StringUtils.equalsIgnoreCase("Date", javaType)) {
                        if (value instanceof Date) {
                            cm.setValue(jsonObject.getDate(name));
                        } else if (value instanceof Long) {
                            Long t = jsonObject.getLong(name);
                            cm.setValue(new Date(t));
                        } else {
                            cm.setValue(DateUtils.toDate(value.toString()));
                        }
                    } else if (StringUtils.equalsIgnoreCase("String", javaType)) {
                        if (value instanceof String) {
                            cm.setValue(jsonObject.getString(name));
                        } else {
                            cm.setValue(value.toString());
                        }
                    } else {
                        cm.setValue(value);
                    }
                }
                tableModel.addColumn(cm);
            }
        }

        if (insertData) {
            ColumnModel idCol = new ColumnModel();
            idCol.setJavaType(idColumn.getJavaType());
            if (StringUtils.equalsIgnoreCase("Integer", idColumn.getJavaType())) {
                idCol.setValue(getEntityService().nextId().intValue());
            } else if (StringUtils.equalsIgnoreCase("Long", idColumn.getJavaType())) {
                idCol.setValue(getEntityService().nextId());
            } else {
                idCol.setValue(getEntityService().getNextId());
            }
            tableModel.setIdColumn(idCol);

            sqlSession.insert("insertTableData", tableModel);

        } else {
            ColumnModel idCol = new ColumnModel();
            idCol.setJavaType(idColumn.getJavaType());
            idCol.setValue(primaryKey);
            tableModel.setIdColumn(idCol);
            sqlSession.update("updateTableDataByPrimaryKey", tableModel);
        }
    }
}

From source file:com.glaf.core.db.TableDataManager.java

License:Apache License

public void updateTableData(String systemName, List<TableModel> rows) {
    if (rows != null && !rows.isEmpty()) {
        SqlSession sqlSession = null;
        Connection conn = null;//from  w ww  . ja v a2s  .  co m
        try {
            conn = DBConnectionFactory.getConnection(systemName);
            conn.setAutoCommit(false);
            sqlSession = getSqlSessionFactory().openSession(ExecutorType.BATCH, conn);
            for (TableModel t : rows) {
                if (t.getTableName() != null) {
                    t.setTableName(t.getTableName().toUpperCase());
                }
                sqlSession.update("updateTableDataByPrimaryKey", t);
            }
            sqlSession.commit();
            conn.commit();
        } catch (Exception ex) {
            JdbcUtils.rollback(conn);
            logger.error(ex);
            ex.printStackTrace();
            throw new RuntimeException(ex);
        } finally {
            JdbcUtils.close(sqlSession);
            JdbcUtils.close(conn);
        }
    }
}

From source file:com.glaf.core.db.TableDataManager.java

License:Apache License

public void updateTableData(String systemName, String tableName, List<Map<String, Object>> rows) {
    com.glaf.core.config.Environment.setCurrentSystemName(systemName);
    TableDefinition tableDefinition = getTableDefinitionService().getTableDefinition(tableName);
    if (tableDefinition != null) {
        if (tableDefinition.getTableName() != null) {
            tableDefinition.setTableName(tableDefinition.getTableName().toUpperCase());
        }/* w w w  . ja va2s .  co m*/
        List<ColumnDefinition> columns = getTableDefinitionService().getColumnDefinitionsByTableName(tableName);
        if (columns != null && !columns.isEmpty()) {
            SqlSession sqlSession = null;
            Connection conn = null;
            try {
                conn = DBConnectionFactory.getConnection(systemName);
                conn.setAutoCommit(false);
                sqlSession = getSqlSessionFactory().openSession(ExecutorType.BATCH, conn);
                Iterator<Map<String, Object>> iterator = rows.iterator();
                while (iterator.hasNext()) {
                    TableModel table = new TableModel();
                    table.setTableName(tableName);
                    Map<String, Object> dataMap = iterator.next();
                    for (ColumnDefinition column : columns) {
                        String javaType = column.getJavaType();
                        String name = column.getColumnName();
                        ColumnModel c = new ColumnModel();
                        c.setColumnName(name);
                        c.setJavaType(javaType);
                        Object value = dataMap.get(name);
                        if (value == null) {
                            value = dataMap.get(name.toLowerCase());
                        }
                        if (value == null) {
                            if (column.getName() != null) {
                                value = dataMap.get(column.getName());
                                if (value == null) {
                                    value = dataMap.get(column.getName().toLowerCase());
                                }
                            }
                        }
                        if (value != null) {
                            if ("Integer".equals(javaType)) {
                                value = ParamUtils.getInt(dataMap, name);
                            } else if ("Long".equals(javaType)) {
                                value = ParamUtils.getLong(dataMap, name);
                            } else if ("Double".equals(javaType)) {
                                value = ParamUtils.getDouble(dataMap, name);
                            } else if ("Date".equals(javaType)) {
                                value = ParamUtils.getTimestamp(dataMap, name);
                            } else if ("String".equals(javaType)) {
                                value = ParamUtils.getString(dataMap, name);
                            } else if ("Clob".equals(javaType)) {
                                value = ParamUtils.getString(dataMap, name);
                            }
                            c.setValue(value);
                            if (column.isPrimaryKey()) {
                                table.setIdColumn(c);
                            } else {
                                table.addColumn(c);
                            }
                        }
                    }
                    sqlSession.update("updateTableDataByPrimaryKey", table);
                }
                sqlSession.commit();
                conn.commit();
            } catch (Exception ex) {
                JdbcUtils.rollback(conn);
                logger.error(ex);
                ex.printStackTrace();
                throw new RuntimeException(ex);
            } finally {
                JdbcUtils.close(sqlSession);
                JdbcUtils.close(conn);
            }
        }
    }
}

From source file:com.glaf.core.db.TableDataManager.java

License:Apache License

public void updateTableData(String systemName, TableDefinition tableDefinition,
        List<Map<String, Object>> rows) {
    if (tableDefinition.getTableName() != null) {
        tableDefinition.setTableName(tableDefinition.getTableName().toUpperCase());
    }// w  ww  .  j  ava  2 s.  c  o  m
    List<ColumnDefinition> columns = tableDefinition.getColumns();
    if (columns != null && !columns.isEmpty()) {
        SqlSession sqlSession = null;
        Connection conn = null;
        try {
            conn = DBConnectionFactory.getConnection(systemName);
            conn.setAutoCommit(false);
            sqlSession = getSqlSessionFactory().openSession(ExecutorType.BATCH, conn);
            Iterator<Map<String, Object>> iterator = rows.iterator();
            while (iterator.hasNext()) {
                TableModel table = new TableModel();
                table.setTableName(tableDefinition.getTableName());
                Map<String, Object> dataMap = iterator.next();
                for (ColumnDefinition column : columns) {
                    String javaType = column.getJavaType();
                    String name = column.getColumnName();
                    ColumnModel c = new ColumnModel();
                    c.setColumnName(name);
                    c.setJavaType(javaType);
                    Object value = dataMap.get(name);
                    if (value == null) {
                        value = dataMap.get(name.toLowerCase());
                    }
                    if (value == null) {
                        if (column.getName() != null) {
                            value = dataMap.get(column.getName());
                            if (value == null) {
                                value = dataMap.get(column.getName().toLowerCase());
                            }
                        }
                    }
                    if (value != null) {
                        if ("Integer".equals(javaType)) {
                            value = ParamUtils.getInt(dataMap, name);
                        } else if ("Long".equals(javaType)) {
                            value = ParamUtils.getLong(dataMap, name);
                        } else if ("Double".equals(javaType)) {
                            value = ParamUtils.getDouble(dataMap, name);
                        } else if ("Date".equals(javaType)) {
                            value = ParamUtils.getTimestamp(dataMap, name);
                        } else if ("String".equals(javaType)) {
                            value = ParamUtils.getString(dataMap, name);
                        } else if ("Clob".equals(javaType)) {
                            value = ParamUtils.getString(dataMap, name);
                        }
                        c.setValue(value);
                        if (column.isPrimaryKey()) {
                            table.setIdColumn(c);
                        } else {
                            table.addColumn(c);
                        }
                    }
                }
                sqlSession.update("updateTableDataByPrimaryKey", table);
            }
            sqlSession.commit();
            conn.commit();
        } catch (Exception ex) {
            JdbcUtils.rollback(conn);
            logger.error(ex);
            ex.printStackTrace();
            throw new RuntimeException(ex);
        } finally {
            JdbcUtils.close(sqlSession);
            JdbcUtils.close(conn);
        }
    }
}