Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. package net.viralpatel.java;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.SQLException;
  10. import org.postgresql.copy.CopyManager;
  11. import org.postgresql.core.BaseConnection;
  12.  
  13. public class BackUpBancoEmCSVSemCabecalho {
  14.  
  15. private static String JDBC_CONNECTION_URL = "jdbc:postgresql://localhost:5432/NomeDoBanco";
  16.  
  17. public static void main(String[] args) throws SQLException, FileNotFoundException, IOException {
  18.  
  19. CopyManager copyManager = new CopyManager((BaseConnection) getCon());
  20. File file = new File("C:\temp\produtos.csv");
  21. FileOutputStream fileOutputStream = new FileOutputStream(file);
  22. String query = "Select * from Produto";
  23.  
  24. //and finally execute the COPY command to the file with this method:
  25. copyManager.copyOut("COPY (" + query + ") TO STDOUT WITH (FORMAT CSV)", fileOutputStream);
  26. }
  27.  
  28. private static Connection getCon() {
  29. Connection connection = null;
  30. try {
  31. Class.forName("org.postgresql.Driver");
  32. connection = DriverManager.getConnection(JDBC_CONNECTION_URL, "postgres", "123");
  33.  
  34. } catch (ClassNotFoundException e) {
  35. e.printStackTrace();
  36. } catch (SQLException e) {
  37. e.getNextException();
  38.  
  39. e.getMessage();
  40. }
  41.  
  42. return connection;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement