Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. private static String url = "jdbc:mysql://localhost/test";
  2. private static String username = "root";
  3. private static String password = "root";
  4.  
  5.  
  6.  
  7.  
  8. public static void main( String[] args) throws ClassNotFoundException, SQLException, IOException
  9. {
  10. Connection conn = null;
  11.  
  12. Class.forName("com.mysql.jdbc.Driver");
  13. conn = DriverManager.getConnection(url, username, password);
  14.  
  15. String sql = "SELECT name, description, data FROM documents ";
  16. PreparedStatement stmt = conn.prepareStatement(sql);
  17. ResultSet resultSet = stmt.executeQuery();
  18. while (resultSet.next()) {
  19. String name = resultSet.getString(1);
  20. System.out.println("Name = " + name);
  21. String description = resultSet.getString(2);
  22. System.out.println("Description = " + description);
  23.  
  24. //
  25. // Get the character stream of our CLOB data
  26. //
  27. Blob blob = resultSet.getBlob(3);
  28. // System.out.println(convertLOB(blob));//convertLOB(blob).toString());
  29. OutputStream fwriter = new FileOutputStream("C:\The Appfuce Primer.docx");
  30. readFromBlob(blob,fwriter);
  31.  
  32. String target = "C:\The Appfuce Primer.docx";
  33.  
  34. File document = new File(target);
  35. Parser parser = new AutoDetectParser();
  36.  
  37. ContentHandler handler = new BodyContentHandler();
  38. Metadata metadata = new Metadata();
  39.  
  40. try {
  41. parser.parse(new FileInputStream(document), handler, metadata, new ParseContext());
  42.  
  43.  
  44. } catch (FileNotFoundException e) {
  45. e.printStackTrace();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. } catch (SAXException e) {
  49. e.printStackTrace();
  50. } catch (TikaException e) {
  51. e.printStackTrace();
  52. }
  53.  
  54. System.out.println(metadata);
  55. System.out.println(handler.toString());
  56. }
  57. }
  58. final static int bBufLen = 4 * 8192;
  59. public static long readFromBlob(Blob blob, OutputStream out)
  60. throws SQLException, IOException {
  61. InputStream in = blob.getBinaryStream();
  62. int length = -1;
  63. long read = 0;
  64. byte[] buf = new byte[bBufLen];
  65. while ((length = in.read(buf)) != -1) {
  66. out.write(buf, 0, length);
  67. read += length;
  68. }
  69. in.close();
  70. return read;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement