HSQL Datei in Tabelle speichern

Status
Nicht offen für weitere Antworten.

alexpetri

Aktives Mitglied
Hi,

ich habe eine Tabelle
Code:
    create table ATTACHEMENT (
        ID INTEGER IDENTITY not null,
        Name VARCHAR(256) not null,
        Description VARCHAR(1024) not null,
        CreateDate timestamp not null,
        MimeType VARCHAR(128) not null,
        content BINARY,
        primary key (ID)
    );
wie kann ich das Feld Attachment befüllen?
ich lese eine Datei als byteArray ein
Code:
    byte[] fileAsByteArray = getBytesFromFile(new File("test.jpg"));
   ...
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        long length = file.length();
        byte[] bytes = new byte[(int)length];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
        is.close();
        return bytes;
    }

wie übergebe ich das jetzt an meine datenbank?
so sicher nicht:
Code:
String sql = "INSERT INTO attachment (content) VALUES('"+fileAsByteArray+"')"
	public synchronized int update(String expression) throws SQLException
	{
		Statement st = null;
		st = conn.createStatement();
		int i = st.executeUpdate(expression);
		if (i == -1)
		{
			System.out.println("db error : " + expression);
		}

		st.close();
		return i;
	}
Kann mir jemand einen hinweis geben?
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben