Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting an Office Open XML (OOXML) file into MySQL as a blob

I have a lot of Office Open XML (OOXML) files saved with the extension XML and I'm trying to insert these files into a MySQL database. I can connect okay, and I've been able to insert strings into different databases with the same syntax. But when I try to insert an XML file into a blob field in the database it tells me I have a problem with my syntax. Is there something special I should be doing because of the format of the files?

public Insertion(Connection conn) throws SQLException, FileNotFoundException{

    System.out.println("Trying to insert Data..");

    String filePath1 = "C:/Users/SAVAGD05/Documents/RMP/Section1.XML";
    InputStream inputStream1 = new FileInputStream(new File(filePath1));
    String filePath2 = "C:/Users/SAVAGD05/Documents/RMP/Section1.XML";
    InputStream inputStream2 = new FileInputStream(new File(filePath2));
    String filePath3 = "C:/Users/SAVAGD05/Documents/RMP/Section1.XML";
    InputStream inputStream3 = new FileInputStream(new File(filePath3));

    System.out.println("It did this part");

    String SQL = "INSERT INTO (1,2,3) values(?,?,?)";
    PreparedStatement statement = conn.prepareStatement(SQL);


    statement.setBlob(1, inputStream1);
    statement.setBlob(2, inputStream2);
    statement.setBlob(3, inputStream3);
    statement.executeUpdate();


    System.out.println("Data inserted.");
    conn.close();
    System.out.println("Connection Closed");
    System.out.println("Have a Nice Day and Goodbye.");
    }


}

This is the error:

"Exception in thread "main"
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(1,2,3) values(_binary'PK\0\0\0\0\0!\0?RH?\0\0?\0\0\0[Content_Types].' at line 1".

On my console, a few of the 0s come out as a question mark in a box instead.

like image 315
Daniel Savage Avatar asked Oct 19 '22 05:10

Daniel Savage


1 Answers

Okay wow I've realised it was just a small syntax error. This:

String SQL = "INSERT INTO (1,2,3) values(?,?,?)";

Needed to be:

 String SQL = "INSERT INTO sections(idSections,Section1,Section2,Section3) values(?,?,?,?)";

as I needed to state the table name as well as give the id field a value.

There isn't a problem with the ooxml data in an xml extension as it turns out since the resulting query can be opened in MS Word (Which is what I aimed for/wanted)

like image 66
Daniel Savage Avatar answered Oct 22 '22 01:10

Daniel Savage