2

我有疑问 - 如何将结果/数据库选择写入 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); - 我在控制台中看到输出,但是如何将 Text 设置为 JTextArea?

4

1 回答 1

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

请参阅文档

于 2013-04-16T18:49:22.387 回答