0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[第3回] Javaでクリニック向け予約・問診アプリを一から作ってみる 〜データベース作成編〜

Posted at

✅ はじめに

こんにちは!前回(https://qiita.com/Leonhard/items/9326912ebc1253161b4b)はJava Swingを使って「予約フォーム」の作成を進めました。
今回はその続きとして、予約データを保存するためのデータベース(SQLite)の作成と、
予約フォームで入力した内容をデータベースに保存する処理を実装していきます!

🎯 今回のゴール

・SQLiteを使ったデータベースの作成
・Javaからのデータベース接続(JDBC)
・フォームで入力したデータをデータベースに保存する

💾 データベースの作成

今回はSQLiteを使います!
SQLiteは「軽量」「シンプル」「ファイル1つで完結」という特徴があり、小規模なアプリ開発にピッタリのデータベースです。

まずはデータベースファイル(reservation.db)を作り、予約情報を保存するテーブルを作成していきます。

🛠️ データベース作成用コード

以下のコードをCreateDatabase.javaとして作成し、実行するとreservation.dbを生成します。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class CreateDatabase {
    public static void main(String[] args) {
        try {
            // SQLite JDBCドライバの読み込み
            Class.forName("org.sqlite.JDBC");

            // データベースに接続(なければ作成される)
            Connection conn = DriverManager.getConnection("jdbc:sqlite:resources/db/reservation.db");

            // テーブル作成SQL
            String sql = "CREATE TABLE IF NOT EXISTS Reservation ("
                       + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
                       + "name TEXT NOT NULL, "
                       + "date TEXT NOT NULL, "
                       + "time_slot TEXT, "
                       + "symptoms TEXT;

            // 実行
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(sql);

            stmt.close();
            conn.close();

            System.out.println("データベースとテーブルを作成しました。");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

📝フォームの入力をデータベースに保存する

次に、前回作成した予約フォーム()からデータを取得し、reservation.dbに保存する処理を追加します。

保存処理のコード

以下は、フォーム送信時の保存処理です(saveReservation()メソッドの例)。

// データベース保存処理
public static void saveReservation() {
    try {
        // 入力値の取得
        String name = nameField.getText();
        String gender = maleButton.isSelected() ? "男性" : (femaleButton.isSelected() ? "女性" : "");
        String department = (String) cb.getSelectedItem();
        String symptoms = syusoField.getText();
        String date = yearBox.getSelectedItem() + "-" + monthBox.getSelectedItem() + "-" + dayBox.getSelectedItem();

        // データベース接続
        Connection conn = DriverManager.getConnection("jdbc:sqlite:resources/db/reservation.db");
        String sql = "INSERT INTO Reservation (name, date, time_slot, symptoms, phone_number) VALUES (?, ?, ?, ?, ?)";

        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setString(2, date);
        pstmt.setString(3, department);
        pstmt.setString(4, symptoms);
        pstmt.setString(5, ""); // 電話番号は今回は未入力

        pstmt.executeUpdate();
        pstmt.close();
        conn.close();

        JOptionPane.showMessageDialog(null, "予約が送信されました!");

    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, "エラーが発生しました: " + e.getMessage());
    }
}

💡 ポイント解説

部分 内容
nameField.getText() 名前の入力値取得
cb.getSelectedItem() 部署(診療科)の選択値取得
yearBox.getSelectedItem() など 年・月・日を連結して日付を作成
Connection conn = DriverManager.getConnection(...) SQLiteデータベースに接続
PreparedStatement pstmt = conn.prepareStatement(...) SQLのプレースホルダに値をセット
JOptionPane.showMessageDialog(...) 完了メッセージ表示

🚀 おわりに

ここまでで、データベースを作成し、フォームからデータを保存するところまでできました!
次回は、この保存したデータを管理者画面で一覧表示してみたいと思います!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?