How to use MySQL and Java to implement a simple subscription function
With the development of the Internet, the subscription function has become a common feature of many websites and applications. The subscription feature allows users to get updates and notifications on content that interests them. In this article, I will show you how to use MySQL and Java to implement a simple subscription function.
In order to implement the subscription function, we need to consider the following key points:
CREATE DATABASE subscription_db;
USE subscription_db;
-- Create User table
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(100) NOT NULL,
subscribe_to VARCHAR(100) NOT NULL
);
--Create subscription content table
CREATE TABLE content (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
content VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
In the above database design, we created two tables: users and content. The user table is used to store the user's subscription information, including the user's ID, email address and the content they subscribe to. The content table is used to store published content, including its title, content, and publication time.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SubscriptionApp extends Application {
private TextField emailField; private ComboBox<String> contentComboBox; @Override public void start(Stage primaryStage) { Label emailLabel = new Label("Email:"); emailField = new TextField(); Label contentLabel = new Label("Content:"); contentComboBox = new ComboBox<>(); contentComboBox.getItems().addAll("Content 1", "Content 2", "Content 3"); Button subscribeButton = new Button("Subscribe"); subscribeButton.setOnAction(e -> subscribe()); VBox vBox = new VBox(10, emailLabel, emailField, contentLabel, contentComboBox, subscribeButton); vBox.setAlignment(Pos.CENTER); vBox.setPadding(new Insets(10)); primaryStage.setTitle("Subscription App"); primaryStage.setScene(new Scene(vBox, 300, 200)); primaryStage.show(); } private void subscribe() { String email = emailField.getText(); String content = contentComboBox.getSelectionModel().getSelectedItem(); // 在这里编写订阅的逻辑,将用户的订阅信息保存到数据库中 System.out.println("Subscribed: " + email + " to " + content); } public static void main(String[] args) { launch(args); }
}
In the above code , we created a simple user interface with a mailbox text box and a content drop-down list box. When the user clicks the "Subscribe" button, we will get the user's email address and subscription content from the text box and drop-down list box.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class SubscriptionService {
public void subscribe(String email, String content) { try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/subscription_db", "username", "password")) { String sql = "INSERT INTO users (email, subscribe_to) VALUES (?, ?)"; PreparedStatement statement = conn.prepareStatement(sql); statement.setString(1, email); statement.setString(2, content); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { SubscriptionService service = new SubscriptionService(); service.subscribe("example@email.com", "Content 1"); }
}
In the above code, we created a SubscriptionService class, which has a subscribe method that can save the user's subscription information to the database. Remember to replace "username" and "password" with your own username and password for your MySQL database.
The above is a specific code example using MySQL and Java to implement a simple subscription function. With this simple subscription feature, you can extend it to suit your own needs, such as adding the ability to unsubscribe, send notifications, and more. Hope this article helps you!
The above is the detailed content of How to implement a simple subscription function using MySQL and Java. For more information, please follow other related articles on the PHP Chinese website!