Example usage for java.sql SQLDataException SQLDataException

List of usage examples for java.sql SQLDataException SQLDataException

Introduction

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

Prototype

public SQLDataException() 

Source Link

Document

Constructs a SQLDataException object.

Usage

From source file:de.stadtrallye.rallyesoft.model.chat.Chatroom.java

/**
 * Save ChatEntries to DB// w ww  .  j a v a2 s .c o m
 *
 * @param entries All entries that have a higher chatID than this.newestID will be saved to DB
 */
private void saveChats(List<ChatEntry> entries) {
    //KEY_ID, KEY_TIME, FOREIGN_GROUP, FOREIGN_USER, KEY_MESSAGE, KEY_PICTURE, FOREIGN_ROOM
    SQLiteStatement s = getDb().compileStatement("INSERT INTO " + DatabaseHelper.Chats.TABLE + " ("
            + DatabaseHelper.strStr(DatabaseHelper.Chats.COLS) + ") VALUES (?, ?, ?, ?, ?, ?, " + chatroomID
            + ")");

    int chatId;
    List<ChatEntry> update = new ArrayList<>();

    stateLock.writeLock().lock();
    try {
        ChatEntry c;
        for (Iterator<ChatEntry> i = entries.iterator(); i.hasNext();) {
            c = i.next();

            if (c.chatID <= newestID) { // Already seen this entry
                if (c.timestamp > lastUpdateTime) { // Entry has changed since last seen
                    update.add(c);
                }
                i.remove(); // ignore
                continue;

            }

            try {
                //            Log.d(THIS, "Inserted "+c+" in Messages");

                s.bindLong(1, c.chatID);
                s.bindLong(2, c.timestamp);
                s.bindLong(3, c.groupID);
                s.bindLong(4, c.userID);
                s.bindString(5, c.message);

                if (c.pictureHash != null)
                    s.bindString(6, c.pictureHash);
                else
                    s.bindNull(6);

                chatId = (int) s.executeInsert();

                //            Log.d(THIS, "Inserted "+c+" in Chats");

                if (chatId != c.chatID)
                    throw new SQLDataException();

            } catch (Exception e) {
                Log.e(THIS, "Single Insert failed", e);
            }
        }

        if (entries.size() > 0) {
            ChatEntry last = entries.get(entries.size() - 1);

            setLast(last.timestamp, last.chatID);
        }

        Log.i(THIS, "Received " + entries.size() + " new Chats in Chatroom " + chatroomID + " since "
                + lastUpdateTime);

    } catch (Exception e) {
        Log.e(THIS, "All Inserts failed", e);
    } finally {
        stateLock.writeLock().unlock();
        s.close();
    }

    if (update.size() > 0) {
        Log.w(THIS, "Chat entries were changed on Server: " + update);
        for (ChatEntry c : update) {
            editChat(c);
        }
    }

    checkForNewUsers();
    checkForNewGroups();

    notifyChatsChanged();
}