2020-12-03-TIL
Connect mysql to JDBC
Java 애플리케이션에서 MySQL에 JDBC로 연결하는 방법을 찾아봤다. Class.forName()으로 드라이버 클래스를 로딩하면 드라이버 객체가 만들어져 DriverManager에 등록되고, 그다음 DriverManager.getConnection()으로 커넥션을 얻는다. MySQL 서버 쪽 설정은 my.cnf 파일에서 바꾸므로, 이 파일의 역할과 macOS에서의 위치도 함께 확인했다.
MySQL TimeZone problem
MySQL Connector/J로 연결할 때 다음과 같은 에러가 날 수 있다.
The server time zone value ‘KST’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
서버의 타임존 값을 드라이버가 해석하지 못해서 생기는 문제이고, 서버 설정을 바꾸거나 JDBC URL에 serverTimezone을 지정해 해결한다. 참고한 글은 URL 뒤에 ?characterEncoding=UTF-8&serverTimezone=UTC를 붙이는 방법을 보여 준다. XML 설정 파일에 URL을 적는 경우 &를 &로 써야 한다는 점도 함께 짚는다.
java.sql.SQLException: Before start of result set
ResultSet의 커서는 처음에 첫 번째 행 앞에 있다. next()를 처음 호출해야 첫 번째 행이 현재 행이 되므로, next()를 부르기 전에 값을 읽으면 이 예외가 난다.
로그인 프로세스에서 ResultSet에 대한 null 체크과정과 로그인 시도할 때 마다 User Bean 객체의 초기화가 필요하다.
테스트 데이터
1
2
3
4
5
6
7
8
9
10
11
12
13
{"2020-11-11", "Food", "Pizza", "Seoul", 0, 8000, 0},
{"2020-11-24", "Beverage", "Coffee", "Seoul", 0, 5000, 0},
{"2020-11-30", "Public", "Mobile Phone fee", "Seoul", 0, 2000000, 0},
{"2020-11-30", "Financial", "Insurance premium", "Seoul", 0, 300000, 0},
{"2020-11-25", "Service", "Mac A/S", "Seoul", 0, 12000, 0},
{"2020-11-30", "Medical", "Dental care", "Dongtan", 0, 45000, 0},
{"2020-11-30", "Income", "Nov salary", "Seoul", 10000000, 0, 0},
{"2020-12-01", "Clothes", "A.P.C shirt", "Daegu", 0, 185000, 0},
{"2020-12-07", "Hobby", "Steam game", "Daegu", 0, 36000, 0},
{"2020-12-12", "Business", "Meeting expenses", "Busan", 0, 12000, 0},
{"2020-12-17", "Study", "Online study", "Daegu", 0, 330000, 0},
{"2020-12-24", "Vehicle", "Fuel", "Busan", 0, 60000, 0},
{"2020-12-24", "Etc", "Lost wallet", "Busan", 0, 100000, 0}
1
INSERT INTO ledger (uid, date, method, type, item, description, location, credit, debit, balance) VALUES (1, '2020-11-25', 'Cash', 'Food', 'Tomato', null, 'Seoul', 0, 5200, 0)
java.awt.IllegalComponentStateException
java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
Component.getLocationOnScreen()은 컴포넌트의 왼쪽 위 모서리를 화면 좌표로 돌려주는데, 컴포넌트가 화면에 보이지 않는 상태면 IllegalComponentStateException을 던진다. 창이 표시되기 전에 화면 위치를 묻는 코드에서 이 예외가 난다.
JTable 다루기
How to get selected row in JTable?
To get multiple selected rows in JTable
JTable.getSelectedRow()는 선택된 첫 번째 행의 인덱스(선택이 없으면 -1)를, getSelectedRows()는 선택된 모든 행의 인덱스를 돌려준다.
How to hide column in JTable?
JTable.removeColumn()은 JTable의 컬럼 배열에서 컬럼을 빼지만 모델의 데이터는 그대로 둔다. 그래서 화면에서는 숨기고 모델에서는 값을 계속 읽을 수 있다.
java.sql.SQLException: Can not issue data manipulation statements with executeQuery()
SELECT를 제외한 나머지 쿼리문은 리턴값으로 int의 값을 반환하는 executeUpdate() 사용해야 한다.
Statement Javadoc 기준으로 executeQuery()는 ResultSet 하나를 돌려주는 SQL을 실행하고, executeUpdate()는 INSERT, UPDATE, DELETE나 DDL처럼 결과 집합이 없는 SQL을 실행한다.
Java Calendar
Swing에서 쓸 달력 컴포넌트로 MiG Calendar를 찾아봤다.
참고한 자료외부 출처 20
외부 출처
- hyeonstorage.tistory.com/110현재 열리지 않음
- [JDBC] JDBC 드라이버 로딩과 Connection 생성
- macOS에서 my.cnf 파일의 위치
- JDBC MySQL 연동하기 (2)
- butgrin.tistory.com/70현재 열리지 않음
- MySQL 설정파일 my.cnf제타위키
- CentOS 7 / Fedora 23 / R..[
- java mysql 연동시 발생하는 에러 모음
- Mysql timezone 변경현재 열리지 않음
- [MySQL] The server time zone 에러 해결
- [ JAVA ] java.sql.SQLException: Before start of result set 오류 처리
- ResultSet (Java Platform SE 8)
- Component (Java Platform SE 8)
- Java JTable getting the data of the selected row
- Get selected rows in JTable
- JTable getSelectedRows현재 열리지 않음
- How to make a columns in JTable Invisible for Swing Java
- JTable (Java Platform SE 8)
- Statement (Java Platform SE 8)
- MiG Calendar v6.6 Released접속이 막혀 내용을 확인하지 못함
댓글
아직 댓글이 없습니다