I have a problem with my result set. I can't get a fixed length of the columns.
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
for(int i=1; i<=cols; i++)
System.out.print(rsmd.getColumnLabel(i)+"\t");
System.out.println("\n--------------------------------------------------------------");
while(rs.next())
{
for(int i=1; i<=cols; i++)
System.out.print(rs.getString(i)+"\t");
System.out.println();
}
The table look like this
http://www.bilder-upload.eu/show.php?file=bf21f6-1527061884.png "Table"
The Column name aren't exactly with the data.
Just use print with format (adjust to desired length):
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
//format begins after 20 characters.
String format = "%-20s";
for(int i=1; i<=cols; i++)
System.out.printf(format, rsmd.getColumnLabel(i));
System.out.println("\n--------------------------------------------------------------");
while(rs.next())
{
for(int i=1; i<=cols; i++)
System.out.printf(format, rs.getString(i));
System.out.println();
}
For example:
public class FixedColumn {
public static void main(String[] args) {
String[] test = {"13333","2qweqwe","3dddd"};
String[] test2 = {"dsds4","6","6e"};
String format = "%-20s";
for (String s : test) {
System.out.printf(format, s);
}
System.out.println();
for (String s : test2) {
System.out.printf(format, s);
}
}
}
Would return: