ChatGPTを使ってみる

ChatGPTを使ってみる

AIによってプログラマがいらなくなるなど言われて久しい今日この頃、ようやくそのような現実になりそうなものが出てきたということで試してみました。

登録して始める

この時点で俺が受け持ってきた新人よりいい印象になりますね。挨拶は大事。

コーディング依頼をしてみる

出てきたコードはこちら
基本アクセスクラス

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class BaseAccess {
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/mydb";
    private static final String DB_USER = "username";
    private static final String DB_PASS = "password";

    private Connection connection;

    public BaseAccess() throws SQLException {
        this.connection = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS);
    }

    public void close() throws SQLException {
        if (this.connection != null) {
            this.connection.close();
        }
    }

    protected Connection getConnection() {
        return this.connection;
    }
}

商品テーブルアクセスクラス

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ProductAccess extends BaseAccess {
    public ProductAccess() throws SQLException {
        super();
    }

    public void getProductById(int productId) throws SQLException {
        String sql = "SELECT * FROM products WHERE id = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, productId);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    // データベースから取得した値を使用する処理を記述する
                }
            }
        }
    }
}

意外と使える…まぁこの辺はよくあるコードだしまぁまぁ

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class InventoryAccess extends BaseAccess {
    public InventoryAccess() throws SQLException {
        super();
    }

    public int getInventoryByProductId(int productId) throws SQLException {
        String sql = "SELECT inventory FROM inventory WHERE product_id = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, productId);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt("inventory");
                }
            }
        }
        return 0; // データが取得できなかった場合は0を返す
    }

    public boolean updateInventoryByProductId(int productId, int newInventory) throws SQLException {
        String sql = "UPDATE inventory SET inventory = ? WHERE product_id = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, newInventory);
            stmt.setInt(2, productId);
            int result = stmt.executeUpdate();
            return result > 0; // 更新件数が1件以上の場合はtrueを返す
        }
    }
}

新人より使えるのではないか?

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class InventoryAccess extends BaseAccess {
    public InventoryAccess() throws SQLException {
        super();
    }

    public int getInventoryByProductId(int productId) throws SQLException {
        String sql = "SELECT inventory FROM inventory WHERE product_id = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, productId);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt("inventory");
                } else {
                    throw new DataNotFoundException("在庫情報が見つかりませんでした。");
                }
            }
        }
    }

    public boolean updateInventoryByProductId(int productId, int newInventory) throws SQLException {
        String sql = "UPDATE inventory SET inventory = ? WHERE product_id = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, newInventory);
            stmt.setInt(2, productId);
            int result = stmt.executeUpdate();
            return result > 0; // 更新件数が1件以上の場合はtrueを返す
        }
    }
}

2~3年目より使えるな(確信)

すごいと思ったところ

前の会話を覚えていて、流れで修正を依頼できるというのがよかったですね
細かい仕様を指定すればその通りに作成してくれました
interface作成してimplimentして~とかメンバーに名前と値段などをもって~等も対応してくれる
設計書を書いてそのテキスト張り付ければ普通にシステム作れそうですね… また、JavaScriptPythonLuaC言語C++といろいろ試したところ問題なくコードが出力されたのでいろいろと使えそうということが分かりました。

願わくば

ChatGPTさんを使う立場で居続けたいものですね…