Send text file to PgAdmin

1

I need a little help, I'm a beginner. I have to send a txt file that I open in netbeans to a PgAdmin table.

If you see the code, you will understand me. I tried to put my method that shows the txt file in INSERT but it did not work. I do not know the right way to do this. the table loads numeric, date, and 6 numeric columns. In that order, thank you.

public class ConexaoTexte {

public static void main(String[] args) {
    try {

        String url = "jdbc:postgresql://localhost:5432/teste";
        String usuario = "postgres";
        String senha = "123456";

        Arquivo arquivo = new Arquivo();

        Class.forName("org.postgresql.Driver");

        Connection con;

        con = DriverManager.getConnection(url, usuario, senha);

        System.out.println("Conexão realizada com sucesso.");

        Statement s = con.createStatement();

       //esse metodo "subirArquivo" mostra o meu arquivo texto na tela.
        arquivo.subirArquivo();

          //porem como fazer para inserir ao banco??
        s.executeUpdate("INSERT INTO resultados VALUES ('1','09-11-2016','1','2';'3')");

        con.close();

    } catch (ClassNotFoundException ex) {
        System.out.println("Não foi possível encontrar a Classe!");
    } catch (SQLException ex) {
    }
}

}

// the class to show my text file.

public class File {

public void subirArquivo() {

    try (Scanner ler = new Scanner(new File("c:/teste.txt"))) {
        while (ler.hasNext()) {
            System.out.println(ler.nextLine());
        }
    } catch (IOException e) {
        System.err.println("Falha ao ler arquivo!");
    }
}

}

    
asked by anonymous 09.11.2016 / 17:53

1 answer

0

Well, in my File class, the loadFile method was created, which will be responsible for sending my txt file to a List.

public class Arquivo {

public static List<String> carregarArquivo() {
    List<String> t = new ArrayList<>();
    try (Scanner ler = new Scanner(new File("c:/steste.txt"))) {

        while (ler.hasNext()) {
            t.add(ler.nextLine());
        }
    } catch (IOException e) {
        System.err.println("Falha ao ler arquivo!");
    }
    return t;
}

}

In the InsertData class, I call the loadFile method, so I can send it to the bank. Before sending to the database, a loop of repetition was created for each line of the text file, so I include each INSERT INTO.

public class InserirDados {

public static void main(String[] args) {
    try {

        String url = "jdbc:postgresql://localhost:5432/teste";
        String usuario = "postgres";
        String senha = "123456";


        Class.forName("org.postgresql.Driver");

        Connection con;

        con = DriverManager.getConnection(url, usuario, senha);

        System.out.println("Conexão realizada com sucesso.");

        Statement s = con.createStatement();
        List<String> arq = Arquivo.carregarArquivo();
        for (String linha : arq) {
            // 1862',01/10/2016',8',49',35',42',56',2
            linha = linha.replaceAll("'", "");
            // 1862,01/10/2016,8,49,35,42,56,2
            String[] p = linha.split(",");

            final String INS = "INSERT INTO resultados (concurso,data,dezena_a) VALUES (%s,'%s',%s)";
            for (int i = 2; i < 8; i++) {
                String script = String.format(INS, p[0], p[1], p[i]);
                s.executeUpdate(script);
            }

        }

        con.close();

    } catch (ClassNotFoundException ex) {
        System.out.println("Não foi possível encontrar a Classe!");
    } catch (SQLException ex) {
    }
}

}

Remember that I'm a beginner. That was the most logical way I found to resolve att.

    
06.12.2016 / 23:36