Java sql wyswietlanie w kolumnach

witam, chciałem wyświetlić zawartość bazy danych w trzech kolumnach w wierszu poleceń, baza natomiast wyświetla się w jednej kolumnie i chciałem prosić o wskazanie błędu, o to kod:

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package javasqluzytkownicy;


import java.sql.*;


/**

 *

 * @author marcin

 */

public class WyswietlanieUzytkownikow {


    static final String ADRES_BAZY = "jdbc:mysql://localhost/uzytkownicy";

    Connection connection = null;

    Statement statement = null;

    ResultSet resultSet = null;


    public WyswietlanieUzytkownikow() {

        try{

            connection = DriverManager.getConnection(ADRES_BAZY, "root", "qwerty12nm");


            statement = connection.createStatement();


            resultSet = statement.executeQuery("select id, imie, nazwisko from dane");


            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();

            int numerId = resultSetMetaData.getColumnCount();

            System.out.println("baza danych uzytkownicy\n");


            for(int i = 1; i <= numerId; ++i){

                System.out.printf("%-8s\t", resultSetMetaData.getColumnName(i));

                System.out.println();


                while(resultSet.next()){

                    for(i = 1; i <= numerId; ++i){

                        System.out.printf("%-8s\t", resultSet.getObject(i));

                        System.out.println();

                    }

                }

            }


        }catch(SQLException sQLException){

            sQLException.printStackTrace();

        }

        finally{

            try{

                resultSet.close();

                statement.close();

                connection.close();

            }catch(Exception exception){

                exception.printStackTrace();

            }

        }

    }


}

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package javasqluzytkownicy;


/**

 *

 * @author marcin

 */

public class JavaSqlUzytkownicy {


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        WyswietlanieUzytkownikow wyswietlanieUzytkownikow = new WyswietlanieUzytkownikow();

    }

}
for(int i = 1; i <= numerId; ++i){

                System.out.printf("%-8s\t", resultSetMetaData.getColumnName(i));

                System.out.println();


                while(resultSet.next()){

                    for(i = 1; i <= numerId; ++i){

                        System.out.printf("%-8s\t", resultSet.getObject(i));

                        System.out.println();

                    }

                }

            }

Dziwny fragment, w zagnieżdżonej pętli wyjdzie na warunku, który jest identyczny jak warunek zewnętrznej pętli, czyli drugi raz nie wejdzie w tę pętlę.

Popatrz tutaj: http://docs.oracle.com/javase/1.4.2/doc … tml#997942 a dokładnie w sekcji 6.1.4 i 6.1.5.

Tak poza tym w javie numeruje się od 0, czyli jak masz tablicę/listę to pierwszy element pobierasz w taki sposób: tabela[0] lub dla listy list.get(0).

Źle formatujesz.

Najpierw wypisz nazwy kolumn.

for(int i = 0; i <= numerId; ++i){

                System.out.println("%-8s\t", resultSetMetaData.getColumnName(i));

 }

Potem wiersze:

while(resultSet.next()){

       for(i = 0; i <= numerId; ++i){

                        System.out.printf("%-8s\t", resultSet.getObject(i));

        }

        System.out.println();

 }

poprawiłem kod, działa teraz jak trzeba

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package javasqluzytkownicy;


import java.sql.*;


/**

 *

 * @author marcin

 */

public class WyswietlanieUzytkownikow {


    static final String ADRES_BAZY = "jdbc:mysql://localhost/urzytkownicy";

    Connection connection = null;

    Statement statement = null;

    ResultSet resultSet = null;


    public WyswietlanieUzytkownikow() {

        try{

            connection = DriverManager.getConnection(ADRES_BAZY, "root", "qwerty12nm");


            statement = connection.createStatement();


            resultSet = statement.executeQuery("select * from dane");


            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();

            int numerId = resultSetMetaData.getColumnCount();

            System.out.println("baza danych uzytkownicy\n");


            for(int i = 1; i <= numerId; ++i){

                System.out.printf("%-8s\t", resultSetMetaData.getColumnName(i));

            }   

            System.out.println();


            while(resultSet.next()){

                for(int i = 1; i <= numerId; ++i){

                    System.out.printf("%-8s\t", resultSet.getObject(i));

                }

                System.out.println();

                }


        }catch(SQLException sQLException){

            sQLException.printStackTrace();

        }

        finally{

            try{

                resultSet.close();

                statement.close();

                connection.close();

            }catch(Exception exception){

                exception.printStackTrace();

            }

        }

    }

 /*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package javasqluzytkownicy;


/**

 *

 * @author marcin

 */

public class JavaSqlUzytkownicy {


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        WyswietlanieUzytkownikow wyswietlanieUzytkownikow = new WyswietlanieUzytkownikow();

    }

}


}
for(int i = 1; i <= numerId; ++i){

Jak dla mnie powinno być tak:

for(int i = 0; i < numerId; i++){

numeracja w sql chyba zaczyna sie od 1

Faktycznie, mój błąd.