Bibi's DevLog ๐Ÿค“๐ŸŽ

[TIL] JDBC๋กœ DB(MySQL) ์—ฐ๊ฒฐํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ - SELECT, INSERT ์˜ˆ์ œ / java.sql.SQLException: Column count doesn't match value count at row ์ˆซ์ž. ๋ณธ๋ฌธ

TIL

[TIL] JDBC๋กœ DB(MySQL) ์—ฐ๊ฒฐํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ - SELECT, INSERT ์˜ˆ์ œ / java.sql.SQLException: Column count doesn't match value count at row ์ˆซ์ž.

๋น„๋น„ bibi 2021. 1. 31. 23:43

[210131]

์˜ค๋Š˜ ์ €๋…์—๋Š” ์–ด์ œ์— ์ด์–ด์„œ JDBC๋กœ MySQL DB ์—ฐ๊ฒฐ์„ ์—ฐ์Šตํ•ด ๋ณด์•˜๋‹ค!

(SELECT ์™€ INSERT)

TIL

JDBC๋ฅผ ์ด์šฉํ•œ DB(MySQL) ์—ฐ๊ฒฐํ•ด ์‚ฌ์šฉํ•˜๊ธฐ

- SELECT, INSERT๋ฌธ

*๋งํฌ1, ๋งํฌ2๋ฅผ ์ฐธ์กฐํ•˜๋ฉฐ ์—ฐ์Šตํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค๐Ÿ™‡โ€โ™‚๏ธ
ํŠนํžˆ ๋งํฌ1 ์ค‘์‹ฌ์œผ๋กœ ๋”ฐ๋ผํ•œ ๊ฒƒ์ด ๋งŽ์€ ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

(MySQL connector๊ฐ€ ์ค€๋น„๋˜์–ด ์žˆ๋‹ค๋Š” ์ „์ œ ํ•˜์— ์‹คํ–‰๊ฐ€๋Šฅํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค)

JDBC๋กœ DB(MySQL) ์—ฐ๊ฒฐํ•ด ์‚ฌ์šฉํ•˜๊ธฐ

  1. import java.sql.*;

  2. ๋“œ๋ผ์ด๋ฒ„ ๊ฐ€์ ธ์˜ค๊ธฐ - MySQL Connector
    Class.forName("com.mysql.jdbc.Driver");

  3. MySQL์—ฐ๊ฒฐ์„ ์œ„ํ•œ Connection๊ฐ์ฒด ์ƒ์„ฑ
    String url = "jdbc:mysql://localhost/DB๋ช…";
    connection = DriverManager.getConnection(url, "DB์‚ฌ์šฉ์žID", "DB์‚ฌ์šฉ์žPW");

  4. Statement / PreparedStatement ๊ฐ์ฒด ์ƒ์„ฑ

    (์•„๋ž˜ connection์€ Connection๊ฐ์ฒด)

    • Statement statement = connection.createStatement();

      • ์ •์ ์ธ ์ฟผ๋ฆฌ๋ฌธ, ์ฆ‰ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์ง€ ์•Š๋Š” ์ฟผ๋ฆฌ๋ฌธ์ผ ๋•Œ ์‚ฌ์šฉ

      ์ฃผ๋กœ SELECT ์ฟผ๋ฆฌ์— ์‚ฌ์šฉํ•จ.

    • PreparedStatement preparedstatement = connection.prepareStatement();

      • ๋™์ ์ธ ์ฟผ๋ฆฌ๋ฌธ, ์ฆ‰ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” ์ฟผ๋ฆฌ๋ฌธ์ผ ๋•Œ ์‚ฌ์šฉ

      ์ฃผ๋กœ INSERT, UPDATE, DELETE ์ฟผ๋ฆฌ์— ์‚ฌ์šฉํ•จ.

      (*PreparedStatement : ์ฟผ๋ฆฌ๋ฅผ ์ค€๋น„ํ•˜๋Š” Statement๋ผ๋Š” ์˜๋ฏธ. ์ž…๋ ฅ๊ฐ’์„ ํ•„์š”๋กœ ํ•˜๋ฏ€๋กœ.)

  5. SQL์ฟผ๋ฆฌ ์ˆ˜ํ–‰ (execute)

    • executeQuery(SQL๋ฌธ) : SELECT ์ฒ˜๋Ÿผ ๋ฆฌํ„ด๊ฐ’์ด ํ•„์š”ํ•œ ์ฟผ๋ฆฌ๋ฌธ์ผ ๋•Œ ์‚ฌ์šฉ
      • Statement.executeQuery()
      • PreparedStatement.executeQuery()
      • ๋ฆฌํ„ดํƒ€์ž… : ResultSet ๊ฐ์ฒด - ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋กœ ๋ฆฌํ„ด๋œ DB ๋ ˆ์ฝ”๋“œ๋“ค์ด ๋‹ด๊ฒจ ์žˆ๋‹ค.
        ResultSet = statement.executeQuery(sql);์™€ ๊ฐ™์ด ์‚ฌ์šฉ.
        resultSet.next() : boolean์„ ๋ฆฌํ„ดํ•จ - ๋‹ค์Œ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜๋ฉด true ๋ฆฌํ„ด.
    • executeUpdate() : INSERT, UPDATE, DELETE ์ฒ˜๋Ÿผ ๋ฆฌํ„ด๊ฐ’์ด ํ•„์š” ์—†๋Š” ์ฟผ๋ฆฌ๋ฌธ์ผ ๋•Œ ์‚ฌ์šฉ
      • Statement.executeUpdate()
      • PreparedStatement.executeUpdate
      • ๋ฆฌํ„ด๊ฐ’ ์—†์Œ
  6. ๋ฆฌ์†Œ์Šค ๋ฐ˜๋‚ฉ

    ํ•„์š”ํ•œ ๋‚˜๋จธ์ง€ ๋กœ์ง๋“ค์„ ์ˆ˜ํ–‰ํ•œ ํ›„,
    finally๋ธ”๋ก์—์„œ .close()๋กœ JDBC ์—ฐ๊ฒฐ ๊ณผ์ •์— ํ•„์š”ํ–ˆ๋˜ ๊ฐ์ฒด๋“ค์„ ๋ฐ˜๋‚ฉํ•จ.
    (๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ์—ญ์ˆœ์œผ๋กœ ๋ฐ˜๋‚ฉํ•จ)

    • Connection.close()
    • Statement.close()
    • PreparedStatement.close()
    • ResultSet.close()

SELECT๋ฌธ ์˜ˆ์ œ

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

public class SelectMySQL {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 1. connector (๋“œ๋ผ์ด๋ฒ„) ๋กœ๋”ฉ
            // ClassNotFoundException ๋ฐœ์ƒ ๊ฐ€๋Šฅ - try-catch ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•„์š”.
            Class.forName("com.mysql.jdbc.Driver");

            // 2. DB(MySQL)์™€ ์—ฐ๊ฒฐ
            // SQLException ๋ฐœ์ƒ ๊ฐ€๋Šฅ - try-catch ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•„์š”.
            String url = "jdbc:mysql://localhost/DB๋ช…";
            connection = DriverManager.getConnection(url, "DB์‚ฌ์šฉ์žID", "DB์‚ฌ์šฉ์žPW");

            // 3. ์ฟผ๋ฆฌ ์ˆ˜ํ–‰์„ ์œ„ํ•œ Statement ๊ฐ์ฒด ์ƒ์„ฑ
            // (Statement ๊ฐ์ฒด : ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด)
            // *์ •์ ์ธ ์ฟผ๋ฆฌ๋ฌธ์€ Statement statement = conn.createStatement();
            // *๋™์ ์ธ ์ฟผ๋ฆฌ๋ฌธ์€ PreparedStatement preparedstatement = conn.prepareStatement();
            // (๋™์ ์ธ ์ฟผ๋ฆฌ๋ฌธ = ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ์ฟผ๋ฆฌ๋ฌธ)
            statement = connection.createStatement();

            // 4. SQL ์ฟผ๋ฆฌ ์ž‘์„ฑ
            // ์ฃผ์˜ 1. JDBC์—์„œ ์ฟผ๋ฆฌ ์ž‘์„ฑ์‹œ ;๋ฅผ ๋นผ๊ณ  ์ž‘์„ฑํ•จ.
            // ์ฃผ์˜ 2. SELECT ํ•  ๋•Œ *๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ๋ณด๋‹ค, ๊ฐ€์ ธ์˜ฌ ์ปฌ๋Ÿผ์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ.
            // ์ฃผ์˜ 3. ์ฟผ๋ฆฌ๋กœ ๊ฒฐ๊ณผ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ค๊ณ , java์ฝ”๋“œ๋กœ ํ›„์ž‘์—…ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์ง€ ์•Š์Œ.
            // ์ฃผ์˜ 4. ์ฟผ๋ฆฌ๋Š” ๊ฐ€๊ธ‰์  ํ•œ ์ค„๋กœ ์ž‘์„ฑ. ์ค„์„ ๋‚˜๋ˆŒ ์‹œ, ๋„์–ด์“ฐ๊ธฐ์— ์œ ์˜.
            String sql = "SELECT nickname, money FROM user_log";

            // 5. SQL ์ฟผ๋ฆฌ ์ˆ˜ํ–‰
            // ์ž‘์„ฑํ•œ SQL ์ฟผ๋ฆฌ๋ฅผ Statement.executeQuery()์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•ด ํ˜ธ์ถœํ•˜๋ฉด ์ฟผ๋ฆฌ๊ฐ€ ์‹คํ–‰๋จ.
            // ๋ฆฌํ„ดํƒ€์ž… : ResultSet ๊ฐ์ฒด - ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์˜ ๋ ˆ์ฝ”๋“œ๋“ค์ด ๋‹ด๊ฒจ ์žˆ๋‹ค.
            // DB์˜ ๋ ˆ์ฝ”๋“œ๋“ค์€ ResultSet ๊ฐ์ฒด์— ์ถ”๊ฐ€๋œ๋‹ค.
            resultSet = statement.executeQuery(sql);

            // 6. ์‹คํ–‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
            // (์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒฐ๊ณผ ์กฐํšŒ๋ฅผ ์œ„ํ•ด์„œ๋Š” VO๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•จ(ํ‚ค์›Œ๋“œ - DAO, VO)
            while (resultSet.next()) {
                // ์ฃผ์˜ - DB ๋ ˆ์ฝ”๋“œ๋“ค์˜ ์นผ๋Ÿผ์€ 0์ด ์•„๋‹Œ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.
                // resultSet.next()๋Š” boolean์„ ๋ฆฌํ„ดํ•จ - ๋‹ค์Œ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ•˜๋ฉด true ๋ฆฌํ„ด

                String nickname = resultSet.getString(1);
                int money = resultSet.getInt(2);
                // DB์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋งž๊ฒŒ getString() ๋˜๋Š” getInt() ๋“ฑ์„ ํ˜ธ์ถœํ•œ๋‹ค.

                System.out.println(nickname + " : " + money);
            }
            // ๊ฒฐ๊ณผ : MySQL์˜ user ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, user_log ํ…Œ์ด๋ธ”์—์„œ nickname(String)๊ณผ money(int)๋ฅผ ๊ฐ€์ ธ์™€ ์ถœ๋ ฅํ•จ.
        } catch (ClassNotFoundException e) {
            System.out.println("๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ ์‹คํŒจ");
        } catch (SQLException e) {
            System.out.println("์—๋Ÿฌ " + e);
        }
        finally {
            // Connection, Statement, ResultSet ๊ฐ์ฒด๋ฅผ ๋‹ซ๋Š” ์ฝ”๋“œ.
            // isClosed(), close()๋Š” SQLException ๋ฐœ์ƒ ๊ฐ€๋Šฅ - try-catch๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ.
            try {
                if (connection != null && !connection.isClosed()) {
                    // Connection์ด ์กด์žฌํ•˜๊ณ , ๋‹ซํ˜€์žˆ์ง€ ์•Š๋‹ค๋ฉด
                    connection.close();
                }
                if (statement != null && !statement.isClosed()) {
                    statement.close();
                }
                if (resultSet != null && !resultSet.isClosed()) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

INSERT๋ฌธ ์˜ˆ์ œ

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

public class InsertMySQL {
    public static void main(String[] args) {
        insert(13 ,"robbins", 3100, "2021-01-31 00:00:00");
    }

    public static void insert(int id, String nickname, int money, String last_visit) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            // 1. connector(๋“œ๋ผ์ด๋ฒ„) ๋กœ๋”ฉ.
            // ClassNotFoundException ๋ฐœ์ƒ ๊ฐ€๋Šฅ - ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•„์š”.
            Class.forName("com.mysql.jdbc.Driver");

            // 2. DB์™€ ์—ฐ๊ฒฐํ•˜๊ธฐ -  connection = DriverManager.getConnection()
            // SQLException ๋ฐœ์ƒ ๊ฐ€๋Šฅ - ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•„์š”.
            String url = "jdbc:mysql://localhost/DB๋ช…";
            connection = DriverManager.getConnection(url, "DB์‚ฌ์šฉ์žID", "DB์‚ฌ์šฉ์žPW");

            // 3. ์ฟผ๋ฆฌ ์ˆ˜ํ–‰์„ ์œ„ํ•œ PreparedStatement ๊ฐ์ฒด ์ƒ์„ฑ
            // INSERT๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ฐ’์€ ๋ณดํ†ต ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ†ตํ•ด ๋™์  ํ• ๋‹น๋˜๋Š” ๊ฐ’์ด๋ฏ€๋กœ,
            // Statement.createStatement()๊ฐ€ ์•„๋‹Œ PreparedStatement.prepareStatement()๋ฅผ ์‚ฌ์šฉํ•จ.
            // (์ฟผ๋ฆฌ๋ฅผ ์ค€๋น„ํ•˜๋Š” statement๋ผ๋Š” ์˜๋ฏธ)
            // prepareStatement()๋Š” PreparedStatement ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ,
            // ์ด ๊ฐ์ฒด์—๋Š” PreparedStatement.setString()์œผ๋กœ ๋™์ ์œผ๋กœ ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ.
            // PreparedStatement ๋ฉ”์†Œ๋“œ๋“ค์€ SQLException ๋ฐœ์ƒ ๊ฐ€๋Šฅ - ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•„์š”.
            String sql = "INSERT INTO user_log VALUES (?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);

            // 4. ๋ฐ์ดํ„ฐ binding - sql๋ฌธ์˜ ๊ฐ ์š”์†Œ์— ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ ํ• ๋‹นํ•ด์คŒ
            preparedStatement.setInt(1, id);
            preparedStatement.setString(2, nickname);
            preparedStatement.setInt(3, money);
            preparedStatement.setString(4, last_visit);

            // 5. ์ฟผ๋ฆฌ ์‹คํ–‰ ๋ฐ ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ
            // SELECT์™€ ๋‹ฌ๋ฆฌ INSERT๋Š” ๋ฐ˜ํ™˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ResultSet ๊ฐ์ฒด๊ฐ€ ํ•„์š” ์—†์Œ.
            // ๋”ฐ๋ผ์„œ ๋ฐ”๋กœ PreparedStatement.executeUpdate()๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•จ.
            // INSERT, UPDATE, DELETE ์ฟผ๋ฆฌ : PreparedStatement.executeUpdate()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ,
            //   (์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ, ์ฆ‰ ๋ฆฌํ„ด๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ)
            // SELECT ์ฟผ๋ฆฌ : Statement.executeQuery()๋ฅผ ์‚ฌ์šฉํ•จ.
            //   (์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ, ์ฆ‰ ๋ฆฌํ„ด๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ)
            // ๋ฆฌํ„ดํƒ€์ž… : int (๋ช‡ ๊ฐœ์˜ row์— ์˜ํ–ฅ์„ ๋ฏธ์ณค๋Š”์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•จ)
            int count = preparedStatement.executeUpdate();
            if (count == 0) {
                System.out.println("๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์‹คํŒจ");
            } else {
                System.out.println("๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์„ฑ๊ณต");
            }
        } catch (ClassNotFoundException e) {
            System.out.println("๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ ์‹คํŒจ");
        } catch (SQLException e) {
            System.out.println("์—๋Ÿฌ " + e);
        }
        finally {
            // Connection, PreparedStatement ๊ฐ์ฒด๋ฅผ ๋‹ซ๋Š” ์ฝ”๋“œ.
            // isClosed(), close()๋Š” SQLException ๋ฐœ์ƒ ๊ฐ€๋Šฅ - try-catch๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ.
            try {
                if (connection != null && !connection.isClosed()) {
                    // Connection์ด ์กด์žฌํ•˜๊ณ , ๋‹ซํ˜€์žˆ์ง€ ์•Š๋‹ค๋ฉด
                    connection.close();
                }
                if (preparedStatement != null && !preparedStatement.isClosed()) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

java.sql.SQLException: Column count doesn't match value count at row ์ˆซ์ž

์œ„์˜ INSERT๋ฌธ ์˜ˆ์ œ๋ฅผ ํ•˜๋‹ค๊ฐ€ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ.

MySQL ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ๊ณผ INSERTํ•œ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๊ฐ€ ๋งž์ง€ ์•Š์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ๋ผ๊ณ  ํ•œ๋‹ค.

ํ•ด๊ฒฐ ) MySQL๋‚ด์—์„œ AUTO-INCREMENT ๋กœ ์„ค์ •ํ•œ ์ปฌ๋Ÿผ์— ๋Œ€ํ•ด์„œ๋„ INSERT ๊ฐ’์„ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผ ํ–ˆ๋‹ค.