0

I'm trying to upload a file to oracle database but I got this error:

java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested

This is my code to insert the file:

b2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        try {
            Connection conn;
            FileInputStream fis = new FileInputStream(file);//From JFileChooser

            String url = "*******************";
            String u = "***********";
            String p = "**********";
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url, u, p);
            System.out.println("Ligação efectuada com sucesso");

            PreparedStatement pstmt = conn.prepareStatement(
                "insert into tableX(file, name) values ( ?, ?)");
            pstmt.setString(2, file.getName());
            pstmt.setBinaryStream(1, fis, (int) file.length());;
            pstmt.executeUpdate();
            JOptionPane.showMessageDialog(null, "Uploaded successfully to database");
            pstmt.close();
            conn.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
});

The file source is from JFileChooser:

b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            text.setText(file.getPath());
        }
    }
});

This error is problem of the code or a problem from Oracle JDBC driver?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
StReeTzZz
  • 17
  • 1
  • 1
  • 6
  • 3
    Unrelated to the error: it is a bad idea to do the connection on the Event Dispatch Thread. You will block the UI until the connection is completed. It would be better to start a worker thread. See the [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) tutorial for more info – Robin May 30 '13 at 13:33
  • What is the data type of the field FILE in table TABLEX? – Olaf May 30 '13 at 15:02
  • @StReeTzZz: Have you tried passing file length to the JDBC driver: pstmt.setString(2, file.getName(), file.length()); ? – Olaf May 30 '13 at 20:01
  • So, PreparedStatement pstmt = conn.prepareStatement("insert into tableX(file, name) values ( ?, ?)"); and pstmt.setString(2, file.getName(), file.length()); ? – StReeTzZz May 31 '13 at 09:17
  • @StReeTzZz: I haven't tried that, but based on the Oracle JDBC docs, this should work. – Olaf May 31 '13 at 18:44
  • Solved. Just converted the file to blob. http://stackoverflow.com/questions/9430008/inserting-blob-data-in-java-using-preparedstatement – StReeTzZz Jun 03 '13 at 09:54

0 Answers0