用java和sql写一个海洋景点导游系统

时间: 2023-12-31 08:05:23 浏览: 25
好的,下面是一个简单的海洋景点导游系统的Java和SQL代码: 1. 首先,我们需要创建一个数据库并创建两个表格,一个是景点表,一个是用户表。这里我们使用MySQL数据库,创建两个表格的SQL语句如下: ```sql CREATE TABLE `spots` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `description` text NOT NULL, `location` varchar(50) NOT NULL, `price` double NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, PRIMARY KEY (`id`) ); ``` 2. 接下来,我们需要使用Java连接到MySQL数据库,并编写Java代码对数据库进行操作。首先,我们需要创建一个类来表示景点: ```java public class Spot { private int id; private String name; private String description; private String location; private double price; // 构造函数 public Spot(int id, String name, String description, String location, double price) { this.id = id; this.name = name; this.description = description; this.location = location; this.price = price; } // Getter和Setter方法 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } ``` 3. 接下来,我们需要创建一个类来表示用户: ```java public class User { private int id; private String name; private String email; private String password; // 构造函数 public User(int id, String name, String email, String password) { this.id = id; this.name = name; this.email = email; this.password = password; } // Getter和Setter方法 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } ``` 4. 然后,我们需要创建一个类来管理数据库连接和操作: ```java import java.sql.*; public class DatabaseManager { private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/ocean_tourism"; private static final String DATABASE_USER = "root"; private static final String DATABASE_PASSWORD = "password"; // 获取数据库连接 private Connection getConnection() throws SQLException { return DriverManager.getConnection(DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD); } // 查询所有景点 public Spot[] getAllSpots() throws SQLException { Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM spots"); Spot[] spots = new Spot[resultSet.getMetaData().getColumnCount()]; int i = 0; while (resultSet.next()) { spots[i] = new Spot( resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("description"), resultSet.getString("location"), resultSet.getDouble("price") ); i++; } connection.close(); return spots; } // 根据ID查询景点 public Spot getSpotById(int id) throws SQLException { Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM spots WHERE id=?"); statement.setInt(1, id); ResultSet resultSet = statement.executeQuery(); Spot spot = null; if (resultSet.next()) { spot = new Spot( resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("description"), resultSet.getString("location"), resultSet.getDouble("price") ); } connection.close(); return spot; } // 查询所有用户 public User[] getAllUsers() throws SQLException { Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM users"); User[] users = new User[resultSet.getMetaData().getColumnCount()]; int i = 0; while (resultSet.next()) { users[i] = new User( resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("email"), resultSet.getString("password") ); i++; } connection.close(); return users; } // 根据ID查询用户 public User getUserById(int id) throws SQLException { Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE id=?"); statement.setInt(1, id); ResultSet resultSet = statement.executeQuery(); User user = null; if (resultSet.next()) { user = new User( resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("email"), resultSet.getString("password") ); } connection.close(); return user; } // 根据Email查询用户 public User getUserByEmail(String email) throws SQLException { Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM users WHERE email=?"); statement.setString(1, email); ResultSet resultSet = statement.executeQuery(); User user = null; if (resultSet.next()) { user = new User( resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("email"), resultSet.getString("password") ); } connection.close(); return user; } // 添加用户 public void addUser(User user) throws SQLException { Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO users (name, email, password) VALUES (?, ?, ?)"); statement.setString(1, user.getName()); statement.setString(2, user.getEmail()); statement.setString(3, user.getPassword()); statement.executeUpdate(); connection.close(); } // 更新用户 public void updateUser(User user) throws SQLException { Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE users SET name=?, email=?, password=? WHERE id=?"); statement.setString(1, user.getName()); statement.setString(2, user.getEmail()); statement.setString(3, user.getPassword()); statement.setInt(4, user.getId()); statement.executeUpdate(); connection.close(); } // 删除用户 public void deleteUser(int id) throws SQLException { Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement("DELETE FROM users WHERE id=?"); statement.setInt(1, id); statement.executeUpdate(); connection.close(); } } ``` 5. 最后,我们可以在主函数中使用这些类来实现海洋景点导游系统: ```java import java.sql.SQLException; import java.util.Scanner; public class OceanTourismGuide { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); DatabaseManager databaseManager = new DatabaseManager(); while (true) { System.out.println("请选择操作:"); System.out.println("1. 查询所有景点"); System.out.println("2. 查询景点详情"); System.out.println("3. 登录"); System.out.println("4. 注册"); System.out.println("0. 退出"); int choice = scanner.nextInt(); switch (choice) { case 1: try { Spot[] spots = databaseManager.getAllSpots(); System.out.println("所有景点如下:"); for (Spot spot : spots) { System.out.println(spot.getId() + ". " + spot.getName() + " - " + spot.getLocation() + " - " + spot.getPrice() + "元"); } } catch (SQLException e) { e.printStackTrace(); } break; case 2: System.out.println("请输入景点ID:"); int spotId = scanner.nextInt(); try { Spot spot = databaseManager.getSpotById(spotId); if (spot != null) { System.out.println(spot.getName() + " - " + spot.getDescription() + " - " + spot.getLocation() + " - " + spot.getPrice() + "元"); } else { System.out.println("未找到对应景点"); } } catch (SQLException e) { e.printStackTrace(); } break; case 3: System.out.println("请输入Email:"); String email = scanner.next(); System.out.println("请输入密码:"); String password = scanner.next(); try { User user = databaseManager.getUserByEmail(email); if (user != null && user.getPassword().equals(password)) { System.out.println("登录成功,欢迎您," + user.getName() + "!"); } else { System.out.println("登录失败,用户名或密码错误!"); } } catch (SQLException e) { e.printStackTrace(); } break; case 4: System.out.println("请输入姓名:"); String name = scanner.next(); System.out.println("请输入Email:"); email = scanner.next(); System.out.println("请输入密码:"); password = scanner.next(); User user = new User(0, name, email, password); try { databaseManager.addUser(user); System.out.println("注册成功!"); } catch (SQLException e) { e.printStackTrace(); } break; case 0: System.exit(0); break; default: System.out.println("无效的操作!"); break; } } } } ``` 以上是一个简单的海洋景点导游系统的Java和SQL代码,仅供参考。

相关推荐

最新推荐

recommend-type

基于Java+sql教师信息管理系统课设报告

基于Java+sql教师信息管理系统课设报告,教师信息管理系统主要用于实现用户名的登陆,退出,教师信息的添加,查找, ...操作,对sql数据库的连接,关闭等基本操作,以及对JAVA编程设计的的基本功能的实现。
recommend-type

Java执行SQL脚本文件到数据库详解

主要为大家详细介绍了Java执行SQL脚本文件到数据库的相关方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

JAVA使用JDBC技术操作SqlServer数据库实例代码

本篇文章主要介绍了JAVA使用JDBC技术操作SqlServer数据库实例代码,具有一定的参考价值,有兴趣的可以了解一下。
recommend-type

java使用JDBC动态创建数据表及SQL预处理的方法

主要介绍了java使用JDBC动态创建数据表及SQL预处理的方法,涉及JDBC操作数据库的连接、创建表、添加数据、查询等相关实现技巧,需要的朋友可以参考下
recommend-type

java执行SQL语句实现查询的通用方法详解

主要介绍了java执行SQL语句实现查询的通用方法详解,具有一定借鉴价值,需要的朋友可以参考下。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。