Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.viralpatel.java;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import org.postgresql.copy.CopyManager;
- import org.postgresql.core.BaseConnection;
- public class BackUpBancoEmCSVSemCabecalho {
- private static String JDBC_CONNECTION_URL = "jdbc:postgresql://localhost:5432/NomeDoBanco";
- public static void main(String[] args) throws SQLException, FileNotFoundException, IOException {
- CopyManager copyManager = new CopyManager((BaseConnection) getCon());
- File file = new File("C:\temp\produtos.csv");
- FileOutputStream fileOutputStream = new FileOutputStream(file);
- String query = "Select * from Produto";
- //and finally execute the COPY command to the file with this method:
- copyManager.copyOut("COPY (" + query + ") TO STDOUT WITH (FORMAT CSV)", fileOutputStream);
- }
- private static Connection getCon() {
- Connection connection = null;
- try {
- Class.forName("org.postgresql.Driver");
- connection = DriverManager.getConnection(JDBC_CONNECTION_URL, "postgres", "123");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.getNextException();
- e.getMessage();
- }
- return connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement