👇 아래 포스팅에서 간단한 환경설정을 마친 후..
> mysql -V
mysql Ver 8.0.23 for Win64 on x86_64 (MySQL Community Server - GPL)
작성 후 maven update
<!-- mysql과 같은 version으로 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
데이터베이스의 CRUD를 모듈화 한 것
mysql(ver 8.0.23)과 연동 시
dburl = "jdbc:mysql://localhost:3306/데이터베이스이름?useSSL=false"
Class.forName("com.mysql.cj.jdbc.Driver");
todo_user 가 todo_db에서 id가 1인 데이터를 가져오는 예제
(아래 테이블)
id | title | name | sequence | type | regdate |
---|---|---|---|---|---|
1 | java공부 | ryong9rrr | 1 | TODO | 2021-04-11 00:55:50 |
main 폴더 아래 java 폴더를 만들고 Class 생성
Todo.java
package kr.co.ryong.todo.dto;
public class Todo {
private Integer id;
private String title;
private String name;
private Integer sequence;
private String type;
private String datetime;
public Todo() {
}
public Todo(Integer id, String title, String name, Integer sequence, String type, String datetime) {
super();
this.id = id;
this.title = title;
this.name = name;
this.sequence = sequence;
this.type = type;
this.datetime = datetime;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSequence() {
return sequence;
}
public void setSequence(Integer sequence) {
this.sequence = sequence;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
@Override
public String toString() {
return (id + " / " + title + " / " + name + " / " + sequence + " / " + type + " / " + datetime );
}
}
TodoDao.java
package kr.co.ryong.todo.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import kr.co.ryong.todo.dto.Todo;
public class TodoDao {
private static String dburl = "jdbc:mysql://localhost:3306/todo_db?useSSL=false";
private static String dbUser = "todo_user";
private static String dbpwd = "todo";
public Todo getTodo(Integer todoId) {
Todo todo = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, dbUser, dbpwd);
String sql = "SELECT * FROM todo WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, todoId);
rs = ps.executeQuery();
if(rs.next()) {
Integer id = rs.getInt("id");
String title = rs.getString("title");
String name = rs.getString("name");
Integer sequence = rs.getInt("sequence");
String type = rs.getString("type");
String datetime = rs.getString("regdate");
todo = new Todo(id, title, name, sequence, type, datetime);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(rs != null) {
try {
rs.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
if(ps != null) {
try {
ps.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
return todo;
}
}
test.java
package kr.co.ryong.todo;
import kr.co.ryong.todo.dao.TodoDao;
import kr.co.ryong.todo.dto.Todo;
public class test {
public static void main(String[] args) {
TodoDao dao = new TodoDao();
Todo todo = dao.getTodo(1);
System.out.println(todo);
}
}
test.java 를 실행하면 Console창에 아래와 같이 출력된다.
1 / java공부 / ryong9rrr / 1 / TODO / 2021-04-11 00:55:50