خرید بک لینک

Vote count: 0

I'm trying to write an object into a database as a blob, but I'm getting java.sql.SQLException: [SQLITE_CONSTRAINT] Abort due to constraint violation (NOT NULL constraint failed: table_name.blob).

Initially, I tried creating the blob as a SerialBlob and setting it on a PreparedStatement with setBlob, but I got java.sql.SQLException: not implemented by SQLite JDBC driver. So, guided by this answer, I tried to create an inputStream for the object, and set that as the binary stream.

I don't know why the database believes that the constraint is being violated - in the tests below, testStreamContentNotNull passes, but testDatabaseWrite throws the aforementioned SQLException.

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Coection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

import org.junit.Test;

public class StackOverflowTest {

    private static final String testStringToBeWrittenAsBlob = "abcdef";

    @Test
    public void testStreamContentNotNull() {
        try {
            try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
                    oos.writeObject(testStringToBeWrittenAsBlob);
                    oos.flush();
                    oos.close();
                    try (ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray())) {
                        try (ObjectInputStream ois = new ObjectInputStream(bis)) {
                            assertThat((String)ois.readObject()).isEqualTo(testStringToBeWrittenAsBlob);
                        }
                    }
                }
            }
        } catch (Exception e) {
            fail();
        }
    }

    @Test
    public void testDatabaseWrite() {
        Coection c = null;
        Statement stmt = null;
        String sql = null;

        try {
            // Initialize the coection
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getCoection("jdbc:sqlite::memory:");
            c.setAutoCommit(false);

            // Create the table
            stmt = c.createStatement();
            sql = "CREATE TABLE table_name " +
                    "(id INT PRIMARY KEY NOT NULL," +
                    "blob BLOB NOT NULL)";
            stmt.executeUpdate(sql);
            c.commit();
            stmt.close();

            // Table has been created - now write to it

            sql = "INSERT INTO table_name (id, blob) VALUES (?, ?)";
            PreparedStatement p_stmt = c.prepareStatement(sql);
            try(ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                try(ObjectOutputStream oos = new ObjectOutputStream(bos)) {
                    oos.writeObject(testStringToBeWrittenAsBlob);
                    oos.flush();
                    oos.close();
                    try (ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray())) {
                        try (ObjectInputStream ois = new ObjectInputStream(bis)) {
                            p_stmt.setString(1, "test-id");
                            p_stmt.setBinaryStream(2, ois);
                            p_stmt.execute(); // <--- This is where the exception is thrown
                            c.commit();
                        }
                    }

                }
            }


            c.close();


        } catch (Exception e) {
            fail();
        }
    }
}

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 10 مرداد 1395 ساعت: 15:05

صفحه بندی