【MapSheep】
[好记性不如烂笔头]
import java.sql.*;
 
public class CheckTableExistence {
    public static void main(String[] args) throws SQLException {
        String url = "jdbc:mysql://localhost:3306/mydatabase"; // MySQL服务器地址及数据库名称
        String username = "root"; // MySQL登录用户名
        String password = "password"; // MySQL登录密码
        
        try (Connection conn = DriverManager.getConnection(url, username, password)) {
            DatabaseMetaData metaData = conn.getMetaData();
            
            ResultSet rs = metaData.getTables(null, null, "tableName", new String[]{});
            
            if (!rs.next()) {
                System.out.println("表不存在");
            } else {
                System.out.println("表存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
posted on 2024-01-23 16:18  (Play)  阅读(39)  评论(0编辑  收藏  举报