필드(Column)와 레코드(Row)로 구성됨
(어플리케이션) (JDBC) DBMS
| SQL문 <====> | JDBC 인터페이스 ===> JDBC 드라이버 | <=====> 테이블 |
mysql 설치
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
MYSQL - GUI 클라이언트
public class DBDemo {
public static Connection makeConnection() {
String url = "jdbc:mysql://localhost/javaex?serverTimezone=Asia/Seoul";
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("데이터베이스 연결중 ...");
conn = DriverManager.getConnection(url, "root", "1234");
System.out.println("데이터베이스 연결 성공");
} catch (ClassNotFoundException e) {
System.out.println("JDBC 드라이버를 찾지 못했습니다...");
} catch (SQLException e) {
System.out.println("데이터베이스 연결 실패");
System.out.println(e.getMessage());
}
return conn;
} public static void main(String[] args) {
makeConnection(); }}
결과출력:
conn에 메소드를 넣어서 간단하게 사용한다.
도커 다운로드
mysql 다운로드
============================================================================================
docker pull mysql // 설치
docker ps
docker run --name mysql1 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1111 -d mysql:latest
docker ps //컨테이너확인
docker rm -f 8922 // 도커삭제명령어
결과
결과 삭제완료
JDBC 연결 코드
public class DBDemo {
public static void main(String[] args) throws SQLException {
Connection conn = makeConnection();
createNewPerson(conn);
}
private static void createNewPerson(Connection conn) throws SQLException {
Statement stmt = conn.createStatement();
String sql = "insert into person (name, phone, email) values ('hong', '010-1111-1111', 'h@a.com')";
int result = stmt.executeUpdate(sql);
System.out.println(result + "개의 데이터가 추가되었습니다.");
}
public static Connection makeConnection(){
String url = "jdbc:mysql://localhost:3307/javaex?serverTimezone=Asia/Seoul";
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("데이터베이스 연결중 ...");
conn = DriverManager.getConnection(url, "root", "1234");
System.out.println("데이터베이스 연결 성공");
} catch (ClassNotFoundException e) {
System.out.println("JDBC 드라이버 검색 오류");
} catch (SQLException e) {
System.out.println("데이터베이스 연결 실패");
}
return conn;
}
}
DB에서 결과확인
결론-> 1.docker로 MYSQL 이미지받아서 3066 mysql 컨테이너 생성(완료)
2. MYSQL workbench로 연결 데이터 확인
3. JAVA jdbc코드를이용하여 insert into 수행
4. 결과확인 완료
<MYSQL 변경된 포트 적용하는방법>
=============================================
mysql -u root -p --port=3308
mysql 패스워드입력 ->로그인
mysql> create user 'ODBC'@'localhost:3308';
mysql> grant all privileges on *.* to 'ODBC'@'localhost:3308';
mysql> exit
cmd에서)
mysql -u root -p --port=3308 < employees.sql
===========================================