⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enginepersistentstore.java

📁 实现了SyncML无线同步协议
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        } catch (SQLException e) {            throw new PersistentStoreException("Error storing the source " + s, e);        } finally {            DBTools.close(conn, stmt, null);        }    }    /**     * Read the source from the data store.     *     * @param s the source     *     * @throws PersistentException in case of error reading the data store     */    private void readSource(Sync4jSource s)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        ResultSet rs = null;        try {            conn = dataSource.getConnection();            stmt = conn.prepareStatement(sql[SQL_GET_SOURCE]);            stmt.setString(1, s.getUri());            rs = stmt.executeQuery();            if (rs.next() == false) {                throw new NotFoundException("Source not found for "                + s.getUri()                );            }            s.setUri   (rs.getString(1));            s.setConfig(rs.getString(2));        } catch (SQLException e) {            throw new PersistentStoreException("Error reading the source " + s, e);        } finally {            DBTools.close(conn, stmt, rs);        }    }    private Object[] readSource(Sync4jSource s,Clause clause)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        ResultSet rs = null;        ArrayList ret = new ArrayList();        try {            conn = dataSource.getConnection();            PreparedWhere where = clause.getPreparedWhere();            String query = sql[SQL_SELECT_ALL_SOURCES];            if (where.sql.length() > 0) {                query += " where " + where.sql;            }            stmt = conn.prepareStatement(query);            for (int i=0; i<where.parameters.length; ++i) {                stmt.setObject(i+1, where.parameters[i]);            }            rs = stmt.executeQuery();            while (rs.next()) {                ret.add(                    new Sync4jSource(                        rs.getString(1),                        rs.getString(2)                    )                );            }            return ret.toArray(new Sync4jSource[ret.size()]);        } catch (SQLException e) {            throw new PersistentStoreException("Error reading sources", e);        } finally {            DBTools.close(conn, stmt, rs);        }    }    /**     * Read all principals     *     * @return an array with the principals (empty if no objects are found)     *     * @throws PersistentException in case of error reading the data store     */    private Object[] readAllSources()    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        ResultSet rs = null;        ArrayList ret = new ArrayList();        try {            conn = dataSource.getConnection();            stmt = conn.prepareStatement(sql[SQL_SELECT_ALL_SOURCES]);            rs = stmt.executeQuery();            while (rs.next()) {                ret.add(                    new Sync4jSource(                        rs.getString(1),                        rs.getString(2)                    )                );            }            return ret.toArray(new Sync4jSource[ret.size()]);        } catch (SQLException e) {            throw new PersistentStoreException("Error reading sources", e);        } finally {            DBTools.close(conn, stmt, rs);        }    }    /**     * Read the device from the data store.     *     * @param d the device     *     * @throws PersistentException in case of error reading the data store     */    private void readDevice(Sync4jDevice d)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        ResultSet rs = null;        try {            conn = dataSource.getConnection();            stmt = conn.prepareStatement(sql[SQL_GET_DEVICE]);            stmt.setString(1, d.getDeviceId());            rs = stmt.executeQuery();            if (rs.next() == false) {                throw new NotFoundException("Device not found for "                + d.getDeviceId()                );            }            d.setDescription(rs.getString(1));            d.setType       (rs.getString(2));        } catch (SQLException e) {            throw new PersistentStoreException("Error reading the device " + d, e);        } finally {            DBTools.close(conn, stmt, rs);        }    }    /**     * Insert or Update the device from the data store.     *     * @param d the device     *     * @throws PersistentException in case of error reading the data store     */    private void storeDevice(Sync4jDevice d)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        int n = 0;        try {            conn = dataSource.getConnection();            if (d.getDeviceId() != null && !d.getDeviceId().equals("")) {                stmt = conn.prepareStatement(sql[SQL_UPDATE_DEVICE]);                stmt.setString(1, d.getDescription());                stmt.setString(2, d.getType());                stmt.setString(3, d.getDeviceId());                n = stmt.executeUpdate();                stmt.close();            }            if (n == 0) {                //                // The first time!!!                //                if (d.getDeviceId() == null || d.getDeviceId().equals("")) {                    int deviceId = this.readCounter("device");                    d.setDeviceId("" + deviceId);                }                stmt = conn.prepareStatement(sql[SQL_INSERT_DEVICE]);                stmt.setString(1, d.getDeviceId());                stmt.setString(2, d.getDescription());                stmt.setString(3, d.getType());                stmt.executeUpdate();            }        } catch (SQLException e) {            throw new PersistentStoreException("Error storing the device " + d, e);        } finally {            DBTools.close(conn, stmt, null);        }    }    /**     * Delete the device from the data store.     *     * @param d the device     *     * @throws PersistentException in case of error reading the data store     */    private void deleteDevice(Sync4jDevice d)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        try {            conn = dataSource.getConnection();            //select and delete all principal for this device            WhereClause c = new WhereClause("device", new String[] {d.getDeviceId()}, WhereClause.OPT_EQ, true);            Sync4jPrincipal[] sp = (Sync4jPrincipal[])readAllPrincipals(c);            for (int i=0; (sp != null) && i<sp.length; i++) {                Sync4jPrincipal syncPrincipal = sp[i];                this.deletePrincipal(syncPrincipal);            }            stmt = conn.prepareStatement(sql[SQL_DELETE_DEVICE]);            stmt.setString(1, d.getDeviceId());            stmt.executeUpdate();        } catch (SQLException e) {            throw new PersistentStoreException("Error deleting the device " + d, e);        } finally {            DBTools.close(conn, stmt, null);        }    }    /**     * Select the number of devices that satisfy the conditions specified in input     *     * @return int number of devices     */    public Object[] readCountDevices(Clause clause) throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        ResultSet rs = null;        int n = 0;        try {            conn = dataSource.getConnection();            PreparedWhere where = clause.getPreparedWhere();            String query = SQL_COUNT_DEVICES;            if (where.sql.length()>0) {                query += " where " + where.sql;            }            stmt = conn.prepareStatement(query);            for (int i=0; i<where.parameters.length; ++i) {                stmt.setObject(i+1, where.parameters[i]);            }            rs = stmt.executeQuery();            while (rs.next()) {                n = rs.getInt(1);            }            String[] s = new String[1];            s[0] = "" + n;            return s;        } catch (SQLException e) {            throw new PersistentStoreException("Error reading count devices ", e);        } finally {            DBTools.close(conn, stmt, rs);        }    }    /**     * Read the counter of the specific id in order to use it like primary key     * in case of inserting a new record into data store.     *     * @param idSpace the idSpace     *     * @throws PersistentException in case of error reading the data store     */    private int readCounter(String idSpace)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        ResultSet rs = null;        int counter = 0;        try {            conn = dataSource.getConnection();            java.sql.DatabaseMetaData dmd = conn.getMetaData();            if ( dmd.supportsSelectForUpdate() ) {                stmt = conn.prepareStatement(sql[SQL_GET_COUNTER]);            } else {                stmt = conn.prepareStatement("select counter from sync4j_id where idspace=? ");            }            stmt.setString(1, idSpace);            rs = stmt.executeQuery();            if (rs.next() == false) {                throw new NotFoundException("Counter not found for "                + idSpace                );            }            counter = rs.getInt(1) + 1;            stmt.close();            stmt = conn.prepareStatement(sql[SQL_UPDATE_COUNTER]);            stmt.setInt(1, counter);            stmt.setString(2, idSpace);            stmt.executeUpdate();        } catch (SQLException e) {            e.printStackTrace();            throw new PersistentStoreException("Error reading the counter " + counter, e);        } finally {            DBTools.close(conn, stmt, rs);        }        return counter;    }    private Object[] readAllDevices(Clause clause)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        ResultSet rs = null;        ArrayList ret = new ArrayList();        try {            conn = dataSource.getConnection();            PreparedWhere where = clause.getPreparedWhere();            String query = sql[SQL_SELECT_ALL_DEVICES];            if (where.sql.length() > 0) {                query += " where " + where.sql;            }            stmt = conn.prepareStatement(query);            for (int i=0; i<where.parameters.length; ++i) {                stmt.setObject(i+1, where.parameters[i]);            }            rs = stmt.executeQuery();            while (rs.next()) {                ret.add(                    new Sync4jDevice(                        rs.getString(1),                        rs.getString(2),                        rs.getString(3)                    )                );            }            return ret.toArray(new Sync4jDevice[ret.size()]);        } catch (SQLException e) {            throw new PersistentStoreException("Error reading devices", e);        } finally {            DBTools.close(conn, stmt, rs);        }    }    private void deleteSource(Sync4jSource s)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        try {            conn = dataSource.getConnection();            stmt = conn.prepareStatement(sql[SQL_DELETE_SOURCE_CLIENT_MAPPING]);            stmt.setString(1, s.getUri());            stmt.executeUpdate();            stmt.close();            stmt = conn.prepareStatement(sql[SQL_DELETE_SOURCE_LAST_SYNC]);            stmt.setString(1, s.getUri());            stmt.executeUpdate();            stmt.close();            stmt = conn.prepareStatement(sql[SQL_DELETE_SOURCE]);            stmt.setString(1, s.getUri());            stmt.executeUpdate();            stmt.close();        } catch (SQLException e) {            throw new PersistentStoreException("Error deleting the source " + s, e);        } finally {            DBTools.close(conn, stmt, null);        }    }    /**     * Insert a new SyncSource     */    private void insertSyncSource(String sourcetypeid, Sync4jSource ss)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        try {            conn = dataSource.getConnection();            stmt = conn.prepareStatement(sql[SQL_INSERT_SYNCSOURCE]);            stmt.setString(1, ss.getUri());            stmt.setString(2, ss.getConfig());            stmt.setString(3, ss.getSourceName());            stmt.setString(4, sourcetypeid);            stmt.executeUpdate();            stmt.close();        } catch (SQLException e) {            throw new PersistentStoreException("Error storing the syncsource " + ss, e);        } finally {            DBTools.close(conn, stmt, null);        }    }    /**     * Update an existing SyncSource     */    private void updateSyncSource(String sourcetypeid, Sync4jSource ss)    throws PersistentStoreException {        Connection conn = null;        PreparedStatement stmt = null;        try {            conn = dataSource.getConnection();            stmt = conn.prepareStatement(sql[SQL_UPDATE_SYNCSOURCE]);            stmt.setString(1, ss.getConfig());            stmt.setString(2, ss.getSourceName());            stmt.setString(3, ss.getSourceTypeId());            stmt.setString(4, ss.getUri()   );            stmt.executeUpdate();            stmt.close();        } catch (SQLException e) {            throw new PersistentStoreException("Error updating the syncsource " + ss, e);        } finally {            DBTools.close(conn, stmt, null);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -