-
mySQL eclipse 셋업, 라이브러리 추가방법 (1/18)DataBase MySQL 2022. 1. 18. 22:50
1. mySQL Java Connector 다운로드 (Database와 연결하기 위한 API 파일)
mysql.com > downloads > 하단 MySQL Community Downloads > Connector/J > Platform Independent > "Platform Independent (Architecture Independent), Compressed TAR Archive" download
2/ eclipse 내에서 연결하기
위 파일 압축풀고 위치 이동(dev > lib파일생성 후 저장)
오른쪽 상단 perspective에서 JavaEE 선택 > 하단 Data Source Explorer > Database Connections 우클릭 > New > MySQL
Drivers: MySQL JDBC Driver 선택
JAR List > Edit JAR > Connector/J 파일 위치 선택
Mac에서는 Properties 그냥 넘어감. OK.
Database에 DB이름 적기
URL: 로컬호스트 / 뒤에 DB이름 적기
PW 넣은 후 Test Connection 하고 Finish
오른쪽 중간에 Open Scrapbook 버튼 클릭하면 데이터베이스 작업 준비 끝!
+ SQL을 사용하려면 sql driver가 필요한데, 위에서 다운 받은 connector JAR 라이브러리 안에 JDBC package - Driver Class를 가져다 쓰면됨.
library 추가하는 방법: 해당 project > Build Path > Configure Build Path > Libraries > Classpath > Add External JAR > Connector java 추가
Connector 라이브러리 써서 SQL 서버 접속하는 코드
//서버접속에 필요한 정보 String url = "jdbc:mysql://localhost:3306/shopdb"; //접속한 뒤에 작업을 하기 위해 필요한 라이브러리 클래스 String driver = "com.mysql.jdbc.Driver"; String id = "root"; String pwd = "mcys1309"; Class.forName(driver); // class 정보 줄건데 너 알고있어라. DB연결할 때 필요한 정보를 VM에 넣음. Connection con = DriverManager.getConnection(url,id,pwd); //driverManager에서 url에 접속해서 id,pwd 넣고 Server로 들어옴 if(con!=null) { System.out.println("Connected!"); con.close(); } else { System.out.println("Connection failed"); }
'DataBase MySQL' 카테고리의 다른 글
어플리케이션 DB접속 (JDBC 라이브러리 이용) (1/19~20) (0) 2022.01.29 DataBase, DBMS, mySQL setup (1/14) (0) 2022.01.18