DRastislav
DRastislav

Reputation: 1882

how to write result to JTextArea

I have question - how to write result/ DB Select to JTextArea. My JButton´s method is:

public void actionPerformed(ActionEvent evt){



    try{
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Connection OK");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/filisng", "root", "passw");
        statement = connection.createStatement();
        result = statement.executeQuery("select * from `filisng`.`names`");

        while(result.next()){

            String nam = result.getString("Name");
            String surnam = result.getString("Surname");

            System.out.printf("Name: %s\tSurname: %s\t\n", Name, Surname);
        }
    }catch(ClassNotFoundException ex){System.out.println("Class Not Found! " +ex);
    }catch(SQLException exception){
        System.out.println("SQL Error " + exception);
}

    }

If I use System.out.printf("Name: %s\tSurname: %s\t\n", Name, Surname); - I see output in Console but, how to set Text to JTextArea?

Upvotes: 2

Views: 3330

Answers (1)

Aubin
Aubin

Reputation: 14853

textArea.append( String.format( "Name: %s\tSurname: %s\t\n", Name, Surname ));

See Documentation.

Upvotes: 2

Related Questions