要通過Java程序輸出MySQL表的內容,你需要使用JDBC連接到MySQL數據庫,并執行相應的查詢語句。以下是一個示例代碼,可以輸出指定表的所有內容:
import java.sql.*; public class Main {????public?static?void?main(String[]?args)?{
????????String?url?=?“jdbc:mysql://localhost:3306/your_database_name”;
????????String?username?=?“your_username”;
????????String?password?=?“your_password”;
????????
????????try?{
????????????Connection?conn?=?DriverManager.getConnection(url,?username,?password);
????????????Statement?stmt?=?conn.createStatement();
????????????String?query?=?“SELECT?*?FROM?your_table_name”;
????????????ResultSet?rs?=?stmt.executeQuery(query);
????????????
????????????while?(rs.next())?{
????????????????int?id?=?rs.getInt(“id”);
????????????????String?name?=?rs.getString(“name”);
????????????????int?age?=?rs.getInt(“age”);
????????????????
????????????????System.out.println("ID:?"?+?id);
????????????????System.out.println("Name:?"?+?name);
????????????????System.out.println("Age:?"?+?age);
????????????????System.out.println();
????????????}
????????????
????????????rs.close();
????????????stmt.close();
????????????conn.close();
????????}?catch?(SQLException?e)?{
????????????e.printStackTrace();
????????}
????} }
將your_database_name
替換為你的數據庫名稱,your_username
和your_password
分別替換為你的數據庫用戶名和密碼,your_table_name
替換為你要輸出內容的表名。運行該程序,就可以輸出MySQL表的內容。