Send text file to Pgadmin

Asked

Viewed 88 times

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 n worked. I don’t know the right way to do this. the table loads Numeric columns, date and 6 more Nouric. In that order I 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 display 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!");
    }
}

}

  • Question, do you want to send what to the database? File name only? To then fetch it from the bank to then open the file from a specific directory?

  • Hello, I got it. It was a file with several names and numbers. I just needed to create a loop to then distribute to the columns of my table.Vlw

  • It is interesting that you post an answer to how you managed to solve your problem so that the next one who has this same difficulty, is helped by your answer :)

1 answer

0

Well, in my File class, we created the method to load File, 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 Data class, I call the File method, so I can send it to the bank. Before sending to the database, a repetition loop was created for each line of the text file, so I include each one to 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 am a beginner. This was the most logical way I found to solve att.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.