Example usage for java.sql PreparedStatement getUpdateCount

List of usage examples for java.sql PreparedStatement getUpdateCount

Introduction

In this page you can find the example usage for java.sql PreparedStatement getUpdateCount.

Prototype

int getUpdateCount() throws SQLException;

Source Link

Document

Retrieves the current result as an update count; if the result is a ResultSet object or there are no more results, -1 is returned.

Usage

From source file:com.portfolio.data.provider.MysqlDataProvider.java

private int updateMysqlNodeChildren(String nodeUuid) {
    PreparedStatement st = null;
    String sql;/*w ww  .j av  a2s  . c o m*/
    int status = -1;

    try {
        /// Re-numrote les noeud (on commence  0)
        sql = "UPDATE node SET node_order=@ii:=@ii+1 " + // La 1re valeur va tre 0
                "WHERE node_parent_uuid=uuid2bin(?) AND (@ii:=-1) " + // Pour tromper la requte parce qu'on veut commencer  0
                "ORDER by node_order";
        if (dbserveur.equals("oracle")) {
            sql = "UPDATE node n1 SET n1.node_order=(SELECT (rnum-1) FROM (SELECT node_uuid, row_number() OVER (ORDER BY node_order ASC) rnum, node_parent_uuid FROM node WHERE node_parent_uuid=uuid2bin(?)) n2 WHERE n1.node_uuid= n2.node_uuid) WHERE n1.node_parent_uuid=?";
        }
        st = connection.prepareStatement(sql);
        st.setString(1, nodeUuid);
        if (dbserveur.equals("oracle")) {
            st.setString(2, nodeUuid);
        }
        st.executeUpdate();

        /// Met  jour les enfants
        if (dbserveur.equals("mysql")) {
            sql = "UPDATE node n1, "
                    + "(SELECT GROUP_CONCAT(bin2uuid(COALESCE(n2.shared_node_uuid,n2.node_uuid)) ORDER BY n2.node_order) AS value "
                    + "FROM node n2 " + "WHERE n2.node_parent_uuid=uuid2bin(?) "
                    + "GROUP BY n2.node_parent_uuid) tmp " + "SET n1.node_children_uuid=tmp.value "
                    + "WHERE n1.node_uuid=uuid2bin(?)";
        } else if (dbserveur.equals("oracle")) {
            sql = "UPDATE node SET node_children_uuid=(SELECT LISTAGG(bin2uuid(COALESCE(n2.shared_node_uuid,n2.node_uuid)), ',') WITHIN GROUP (ORDER BY n2.node_order) AS value FROM node n2 WHERE n2.node_parent_uuid=uuid2bin(?) GROUP BY n2.node_parent_uuid) WHERE node_uuid=uuid2bin(?)";
        }
        st = connection.prepareStatement(sql);
        st.setString(1, nodeUuid);
        st.setString(2, nodeUuid);
        st.executeUpdate();
        int changes = st.getUpdateCount();

        if (changes == 0) // Specific case when there's no children left in parent
        {
            sql = "UPDATE node n " + "SET n.node_children_uuid=NULL " + "WHERE n.node_uuid=uuid2bin(?)";
            st = connection.prepareStatement(sql);
            st.setString(1, nodeUuid);
            st.executeUpdate();
            changes = st.getUpdateCount();
        }

        status = changes;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (st != null)
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }
    return status;
}

From source file:com.portfolio.data.provider.MysqlDataProvider.java

private int insertMysqlResource(String uuid, String parentUuid, String xsiType, String content,
        String portfolioModelId, int sharedNodeRes, int sharedRes, int userId) {
    String sql = "";
    PreparedStatement st = null;
    int status = -1;

    try {/*from   ww  w .  jav a  2s . c om*/
        if (((xsiType.equals("nodeRes") && sharedNodeRes == 1)
                || (!xsiType.equals("context") && !xsiType.equals("nodeRes") && sharedRes == 1))
                && portfolioModelId != null) {
            // On ne fait rien

        } else {
            if (dbserveur.equals("mysql")) {
                sql = "REPLACE INTO resource_table(node_uuid,xsi_type,content,user_id,modif_user_id,modif_date) ";
                sql += "VALUES(uuid2bin(?),?,?,?,?,?)";
            } else if (dbserveur.equals("oracle")) {
                sql = "MERGE INTO resource_table d USING (SELECT uuid2bin(?) node_uuid,? xsi_type,? content,? user_id,? modif_user_id,? modif_date FROM DUAL) s ON (d.node_uuid = s.node_uuid) WHEN MATCHED THEN UPDATE SET d.xsi_type = s.xsi_type, d.content = s.content, d.user_id = s.user_id, d.modif_user_id = s.modif_user_id, d.modif_date = s.modif_date WHEN NOT MATCHED THEN INSERT (d.node_uuid, d.xsi_type, d.content, d.user_id, d.modif_user_id, d.modif_date) VALUES (s.node_uuid, s.xsi_type, s.content, s.user_id, s.modif_user_id, s.modif_date)";
            }
            st = connection.prepareStatement(sql);
            st.setString(1, uuid);
            st.setString(2, xsiType);
            st.setString(3, content);
            st.setInt(4, userId);
            st.setInt(5, userId);
            if (dbserveur.equals("mysql")) {
                st.setString(6, SqlUtils.getCurrentTimeStamp());
            } else if (dbserveur.equals("oracle")) {
                st.setTimestamp(6, SqlUtils.getCurrentTimeStamp2());
            }

            st.executeUpdate();
            if (st != null)
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
        // Ensuite on met  jour les id ressource au niveau du noeud parent
        if (xsiType.equals("nodeRes")) {
            sql = " UPDATE node SET res_res_node_uuid =uuid2bin(?), shared_node_res_uuid=uuid2bin(?) ";
            sql += " WHERE node_uuid = uuid2bin(?) ";
            st = connection.prepareStatement(sql);
            st.setString(1, uuid);
            if (sharedNodeRes == 1 && portfolioModelId != null)
                st.setString(2, uuid);
            else
                st.setString(2, null);
            st.setString(3, parentUuid);
        } else if (xsiType.equals("context")) {
            sql = " UPDATE node SET res_context_node_uuid=uuid2bin(?)";
            sql += " WHERE node_uuid = uuid2bin(?) ";
            st = connection.prepareStatement(sql);
            st.setString(1, uuid);
            st.setString(2, parentUuid);
        } else {
            sql = " UPDATE node SET res_node_uuid=uuid2bin(?), shared_res_uuid=uuid2bin(?) ";
            sql += " WHERE node_uuid = uuid2bin(?) ";
            st = connection.prepareStatement(sql);
            st.setString(1, uuid);
            if (sharedRes == 1 && portfolioModelId != null)
                st.setString(2, uuid);
            else
                st.setString(2, null);
            st.setString(3, parentUuid);
        }

        //         return st.executeUpdate();
        st.executeUpdate();
        status = st.getUpdateCount();
    } catch (Exception ex) {
        //System.out.println("root_node_uuid : "+uuid);
        ex.printStackTrace();
        status = -1;
    } finally {
        if (st != null)
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }
    return status;
}