티스토리 뷰

JDBC Driver 다운로드



mySQL 버전 8.0 이상에서 사용 할 수 있는 JDBC Driver를 다운로드 해야한다.


다운로드 사이트


https://dev.mysql.com/downloads/connector/j/



여기서 각자 플랫폼에 맞는 JDBC 다운로드


* jar 파일만 필요하기 때문에 msi파일은 다운로드 하지 않았다.




Windows를 쓰고 있어서 Platform Independent로 다운로드 





No thanks, just start my download. 클릭.




다운로드 받은 파일에서 jar 파일만 적당한 경로에 붙여넣기.





이클립스에서 JDBC 파일 추가하기



진행중인 프로젝트에서 오른쪽 버튼 클릭


Build => Configure Build Path...





Libraries 탭 => Add External JARs...






적당한 경로에 넣어둔 'mysql-connector-java-8.0.12.jar' 선택 이후 열기






추가 된것을 확인하고 APPLY 버튼으로 적용. => Apply and Close 로 확인.






소스 코드 작성



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

public class MysqlConnection {
	
	public static void main(String[] args) {
		
		Connection conn = null;
		
		try {
			String url = "jdbc:mysql://IP주소:포트번호?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
			
			conn = DriverManager.getConnection(url, "USERID", "PW");
		    System.out.println(conn.toString());

		} catch (SQLException e) {
			System.out.println("SQLException: " + e.getMessage());
		    System.out.println("SQLState: " + e.getSQLState());
		    System.out.println("VendorError: " + e.getErrorCode());
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}


* 주의 사항 

mysql 8.0 이상이므로 serverTimezone=UTC 를 설정해줘야 에러가 나지 않는다.



만일 서버타임을 설정하지 않으면 다음과 같은 예외처리가 발생한다.


SQLException: The server time zone value '´???¹?±¹ ???ؽ?' 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.

SQLState: 01S00

VendorError: 0


결과


com.mysql.cj.jdbc.ConnectionImpl@2d6d8735

위 처럼 객체를 반환하면 성공.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함