如何将结果写入JTextArea

时间:2013-04-16 18:47:31

标签: java swing jdbc jtextarea

我有疑问 - 如何编写结果/ DB选择到JTextArea。我的JButton的方法是:

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);
}

    }

如果我使用System.out.printf("Name: %s\tSurname: %s\t\n", Name, Surname); - 我在Console中看到输出但是,如何将Text设置为JTextArea?

1 个答案:

答案 0 :(得分:2)

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

请参阅Documentation