要通过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表的内容。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: Java人脸识别如何进行实时跟踪