file upload in oracle-db über Jdeveloper

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • file upload in oracle-db über Jdeveloper

    Hallo Leute,
    ich selbst bin blutiger Anfänger im Application-Development und gerade im Praxissemester. Meine Aufgabe ist es, eine Javabasierte Applikation zu erweitern, ergänzen. Entwicklungsumgebung ist der JDeveloper 10g. Die Applikation läuft auf einem Oracle AS 10g. Mein aktuelles Problem besteht darin, dass ich einen fileupload über ein "inputFile" Feld realisieren soll, sodass BLOBS IN der Datenbank gespeichert werden. In deshalb, weil über OracleText eine Velltextsuche durch die Dokumente möglich sein soll. Leider ist dieses Projekt ein Stoß ins kalte Wasser für mich. Ausser Grundlagen DB und Java kann ich nicht viel mehr vorweisen. Ist es möglich direkt sqö-statements in den Javacode einzubinden?, benutze ich besser interMedia dafür und wie realisiere ich überhaupt meine Problemstellung? Ich bin für alle Anregungen, Hilfestellungen oder auch Codeschnipsel sehr dankbar.
  • Hello again!
    Mittlerweile bin ich etwas weitergekommen. Allerdings wird das BLOB selber noch nicht in der DB gespeichert. SQL-Developer meldet mir für den Wert des BLOB-Feldes "null". Kann dies daran liegen, dass vorher kein leeres BLOB erzeugt wurde? Vielleicht kann jemand mal meine Methode zum upload überfliegen und mir Feedback geben. Danke vorab.



    Quellcode

    1. public IfcobDocumentsIndicated createIfcobDocumentsIndicated(Integer level1, Integer level2,
    2. Integer level3, Integer level4, String lastUploadedFileName,
    3. Integer idDocumentsIndicated,
    4. String name, String shortName, String mimeType,
    5. String info, Date createDate, Date lastUpdate,
    6. String userId) {
    7. UnitOfWork uow = getSessionFactory().acquireUnitOfWork();
    8. IfcobDocumentsIndicated ifcobdocumentsindicated =
    9. (IfcobDocumentsIndicated)uow.newInstance(IfcobDocumentsIndicated.class);
    10. System.out.println("filename in bean:" + lastUploadedFileName);
    11. if (level1 != null && level2 != null && level3 != null &&
    12. level4 != null) {
    13. IfcobLevel1 ifcobLevel1 =
    14. (IfcobLevel1)uow.executeQuery("findLevel1ById",
    15. IfcobLevel1.class, level1);
    16. IfcobLevel2 ifcobLevel2 =
    17. (IfcobLevel2)uow.executeQuery("findLevel2ById",
    18. IfcobLevel2.class, level2);
    19. IfcobLevel3 ifcobLevel3 =
    20. (IfcobLevel3)uow.executeQuery("findLevel3ById",
    21. IfcobLevel3.class, level3);
    22. IfcobLevel4 ifcobLevel4 =
    23. (IfcobLevel4)uow.executeQuery("findLevel4ById",
    24. IfcobLevel4.class, level4);
    25. ifcobLevel1.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    26. ifcobLevel2.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    27. ifcobLevel3.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    28. ifcobLevel4.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    29. }
    30. else if (level1 != null && level2 != null && level3 != null &&
    31. level4 == null) {
    32. IfcobLevel1 ifcobLevel1 =
    33. (IfcobLevel1)uow.executeQuery("findLevel1ById",
    34. IfcobLevel1.class, level1);
    35. IfcobLevel2 ifcobLevel2 =
    36. (IfcobLevel2)uow.executeQuery("findLevel2ById",
    37. IfcobLevel2.class, level2);
    38. IfcobLevel3 ifcobLevel3 =
    39. (IfcobLevel3)uow.executeQuery("findLevel3ById",
    40. IfcobLevel3.class, level3);
    41. ifcobLevel1.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    42. ifcobLevel2.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    43. ifcobLevel3.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    44. }
    45. else if (level1 != null && level2 != null && level3 == null &&
    46. level4 == null) {
    47. IfcobLevel1 ifcobLevel1 =
    48. (IfcobLevel1)uow.executeQuery("findLevel1ById",
    49. IfcobLevel1.class, level1);
    50. IfcobLevel2 ifcobLevel2 =
    51. (IfcobLevel2)uow.executeQuery("findLevel2ById",
    52. IfcobLevel2.class, level2);
    53. ifcobLevel1.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    54. ifcobLevel2.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    55. }
    56. else if (level1 != null && level2 == null && level3 == null &&
    57. level4 == null) {
    58. IfcobLevel1 ifcobLevel1 =
    59. (IfcobLevel1)uow.executeQuery("findLevel1ById",
    60. IfcobLevel1.class, level1);
    61. ifcobLevel1.addIfcobDocumentsIndicated(ifcobdocumentsindicated);
    62. }
    63. ifcobdocumentsindicated.setIdDocumentsIndicated(idDocumentsIndicated);
    64. //ifcobdocumentsindicated.setBlobContent(blobContent);
    65. ifcobdocumentsindicated.setName(name);
    66. ifcobdocumentsindicated.setShortName(shortName);
    67. ifcobdocumentsindicated.setMimeType(mimeType);
    68. ifcobdocumentsindicated.setInfo(info);
    69. ifcobdocumentsindicated.setCreateDate(createDate);
    70. ifcobdocumentsindicated.setLastUpdate(lastUpdate);
    71. ifcobdocumentsindicated.setUserId(userId);
    72. //Save that away
    73. uow.commit();
    74. Connection conn;
    75. Statement stmt;
    76. ResultSet rset;
    77. String query;
    78. Integer newIdDocuments=0;
    79. //FileInputStream istream;
    80. try{
    81. FileInputStream istream=null;
    82. try{
    83. istream = new FileInputStream(lastUploadedFileName);
    84. } catch (FileNotFoundException e){
    85. // To Do
    86. }
    87. try{
    88. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    89. conn = DriverManager.getConnection("jdbc:oracle:thin:@****************:****", "******", "********");
    90. stmt=conn.createStatement();
    91. query="SELECT max(ID_DOCUMENTS_INDICATED) from IFCOB_DOCUMENTS_INDICATED";
    92. try{
    93. rset=stmt.executeQuery(query);
    94. rset.next();
    95. newIdDocuments=rset.getInt(1);
    96. }catch(Exception co){
    97. String msgError=co.getMessage();
    98. }
    99. /*
    100. stmt = conn.createStatement ();
    101. rset=stmt.executeQuery("INSERT INTO IFCOB_DOCUMENTS_INDICATED(BLOB_CONTENT) VALUES(EMPTY_blob() ");
    102. try
    103. {
    104. stmt.execute(query);
    105. }catch(Exception co)
    106. {
    107. String msgError=co.getMessage();
    108. }
    109. */
    110. stmt=conn.createStatement();
    111. rset = stmt.executeQuery ("SELECT BLOB_CONTENT FROM IFCOB_DOCUMENTS_INDICATED WHERE ID_DOCUMENTS_INDICATED="+newIdDocuments+"FOR UPDATE");
    112. if (rset.next ()){
    113. BLOB blob = ((OracleResultSet)rset).getBLOB(1);
    114. // Create an OutputStram object to write the blob as a stream
    115. OutputStream ostream = blob.getBinaryOutputStream ();
    116. // Create a tempory buffer
    117. byte[] buffer = new byte[1024];
    118. int length = 0;
    119. // Use the read() method to read the GIF file to the byte
    120. // array buffer, then use the write() method to write it to
    121. // the blob.
    122. while ((length = istream.read(buffer)) != -1){
    123. ostream.write(buffer, 0, length);
    124. }
    125. // Close the inputstream and outputstream
    126. istream.close();
    127. ostream.close();
    128. }
    129. conn.commit();
    130. if(rset!=null) rset.close();
    131. if(stmt!=null) stmt.close();
    132. if(conn!=null) conn.close();
    133. }catch(Exception e){
    134. e.printStackTrace();
    135. }
    136. }catch(Exception e){
    137. e.printStackTrace();
    138. }
    139. return ifcobdocumentsindicated;
    140. }
    Alles anzeigen