How to separate records from a query through ResultSet

0

I have the following problem, I try that, from a query that is saved in a ResultSet enter the information of the query (returns a table) in a file txt , the problem that arises is that keeps as it all the query, does not respect or spaces, or if they are different record or not.

My code is as follows

 ResultSet rs;
 rs =statement.executeQuery("select *from empleado;");
 while(rs.next()){
   for(i=1;i<11;i++){
      linea=rs.getString(i);
      pw.print(linea+"|");
      System.out.print(linea);
   }
 }

I already tried to find a function with the variable ResultSet or manipulate it with cycles but I can not find a way to separate records.

    
asked by Roman 02.01.2018 в 03:15
source

1 answer

0

When you write in the text file, you are writing a line followed by another one, just as you print it by console, an easy and simple solution is to add a tab for each written data and for each written record, add a new jump of line.

The code is left for you:

while (rs.next()) {
    // Escribir campos de registro.
    for (int i = 1; i < 11; i++) {
        linea = rs.getString(i);
        ps.print(linea + "\t");
        System.out.print(linea+ "\t");
    }
    // Fin de registro.
    ps.println();
    System.out.println();
}

As you can see, ps corresponds to:

PrintStream ps = new PrintStream("C:\Users\Fernando Pacheco\Desktop\temp.txt");
    
answered by 02.01.2018 в 11:22