본문 바로가기

Android[JAVA]/JDBC

JDBC 오류 [con 오류:Field 'chatNum' doesn't have a default value]

C:\Users\.jdks\adopt-openjdk-14.0.2\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2021.1.1\lib\idea_rt.jar=64375:C:\Program Files\JetBrains\IntelliJ IDEA Educational Edition 2021.1.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\leebbr\IdeaProjects\chat\out\production\chat;C:\Users\leebbr\Downloads\mysql-connector-java-8.0.26\mysql-connector-java-8.0.26.jar JDBC
정상적으로 연결되었습니다.
con 오류:Field 'chatNum' doesn't have a default value
java.sql.SQLException: Field 'chatNum' doesn't have a default value
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1337)
	at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2112)
	at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1247)
	at JDBC.main(JDBC.java:39)

Process finished with exit code 0

--> 이런 오류가 남.. 

import javax.swing.plaf.nimbus.State;
import java.sql.*;

public class JDBC {
    public static void main(String[] args) throws SQLException {
        Connection con = null;


        String server = ""; // MySQL 서버 주소
        String database = ""; // MySQL DATABASE 이름
        String user_name = ""; //  MySQL 서버 아이디
        String password = ""; // MySQL 서버 비밀번호

        // 1.드라이버 로딩
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.err.println(" !! <JDBC 오류> Driver load 오류: " + e.getMessage());
            e.printStackTrace();
        }

        // 2.연결
        try {
            // jdbc:mysql://IP:3306/DBNAME?characterEncoding=utf8&autoReconnect=true&serverTimezone=KST
            //
            //
           // con = DriverManager.getConnection("jdbc:mysql://" + server + "/" + database + "?useSSL=false", user_name, password);
            con = DriverManager.getConnection("jdbc:mysql://" + server + "/" + database + "?serverTimezone=Asia/Seoul", user_name, password);
            System.out.println("정상적으로 연결되었습니다.");
            Statement statement = con.createStatement();

            //            String sql = "INSERT INTO chat";
//            sql += "VALUES('1', '24', '1', '사과','2', 'WWW', 'msg','2021-08-15- 21:00:00')";

            //String sql = "insert into chattingMessageTable(chatNum, chatMsg) " + "values('age')";
            String SQL = "insert into chattingMessageTable(roomNum, chatMsg) values('age','age')";

            statement.executeUpdate(SQL);

            System.out.println("insert 성공");


        } catch(SQLException e) {
            System.err.println("con 오류:" + e.getMessage());
            e.printStackTrace();
        }

        // 3.해제
        try {
            if(con != null)
                con.close();
        } catch (SQLException e) {}
    }

}

sql모드를 바꿔보라는 걸 보고 mysql 조회함.

  System load:  0.0               Processes:             112
  Usage of /:   66.3% of 7.69GB   Users logged in:       0
  Memory usage: 62%               IPv4 address for eth0: 172.31.36.138
  Swap usage:   0%

 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.

   https://ubuntu.com/blog/microk8s-memory-optimisation

58 updates can be applied immediately.
To see these additional updates run: apt list --upgradable


Last login: Mon Aug 16 12:19:28 2021 from 1.220.248.206
ubuntu@ip-172-31-36-138:~$ mysql -u leebbr -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'leebbr'@'localhost' (using password: YES)
ubuntu@ip-172-31-36-138:~$ mysql -u leebbr -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 889
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'sql_mode';
+---------------+-----------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value                                                                                                                 |
+---------------+-----------------------------------------------------------------------------------------------------------------------+
| sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+---------------+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

모드를 바꿔보려고했는데,, ㅋㅋ

mysql에서 notNull 이면 Auto Increment 가 체크되어야 하는 것이었음.

 

 

이렇게 하니 해결되었음!