이 게시글은 이것이 자바다(저자 : 신용권, 임경균)의 책과 동영상 강의를 참고하여 개인적으로 정리하는 글임을 알립니다.
USER 테이블
Oracle
MySQL
BOARDS 테이블
Oracle
MySQL
구현
Oracle
Board.java
import java.util.Date;
public class Board {
private int bno;
private String btitle;
private String bcontent;
private String bwriter;
private Date bdate;
public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}
public String getBtitle() {
return btitle;
}
public void setBtitle(String btitle) {
this.btitle = btitle;
}
public String getBcontent() {
return bcontent;
}
public void setBcontent(String bcontent) {
this.bcontent = bcontent;
}
public String getBwriter() {
return bwriter;
}
public void setBwriter(String bwriter) {
this.bwriter = bwriter;
}
public Date getBdate() {
return bdate;
}
public void setBdate(Date bdate) {
this.bdate = bdate;
}
}
BoardExample9.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import ch20.oracle.sec09.exam02.Board;
public class BoardExample9 {
//Field
private Scanner scanner = new Scanner(System.in);
private Connection conn;
//Constructor
public BoardExample9() {
try {
//JDBC Driver 등록
Class.forName("oracle.jdbc.OracleDriver");
//연결하기
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521/orcl",
"java",
"oracle"
);
} catch(Exception e) {
e.printStackTrace();
exit();
}
}
//Method
public void list() {
//타이틀 및 컬럼명 출력
System.out.println();
System.out.println("[게시물 목록]");
System.out.println("-----------------------------------------------------------------------");
System.out.printf("%-6s%-12s%-16s%-40s\n", "no", "writer", "date", "title");
System.out.println("-----------------------------------------------------------------------");
//boards 테이블에서 게시물 정보를 가져와서 출력하기
try {
String sql = "" +
"SELECT bno, btitle, bcontent, bwriter, bdate " +
"FROM boards " +
"ORDER BY bno DESC";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
Board board = new Board();
board.setBno(rs.getInt("bno"));
board.setBtitle(rs.getString("btitle"));
board.setBcontent(rs.getString("bcontent"));
board.setBwriter(rs.getString("bwriter"));
board.setBdate(rs.getDate("bdate"));
System.out.printf("%-6s%-12s%-16s%-40s \n",
board.getBno(),
board.getBwriter(),
board.getBdate(),
board.getBtitle());
}
rs.close();
pstmt.close();
} catch(SQLException e) {
e.printStackTrace();
exit();
}
//메인 메뉴 출력
mainMenu();
}
public void mainMenu() {
System.out.println();
System.out.println("-----------------------------------------------------------------------");
System.out.println("메인 메뉴: 1.Create | 2.Read | 3.Clear | 4.Exit");
System.out.print("메뉴 선택: ");
String menuNo = scanner.nextLine();
System.out.println();
switch(menuNo) {
case "1" -> create();
case "2" -> read();
case "3" -> clear();
case "4" -> exit();
}
}
public void create() {
//입력 받기
Board board = new Board();
System.out.println("[새 게시물 입력]");
System.out.print("제목: ");
board.setBtitle(scanner.nextLine());
System.out.print("내용: ");
board.setBcontent(scanner.nextLine());
System.out.print("작성자: ");
board.setBwriter(scanner.nextLine());
//보조 메뉴 출력
System.out.println("-----------------------------------------------------------------------");
System.out.println("보조 메뉴: 1.Ok | 2.Cancel");
System.out.print("메뉴 선택: ");
String menuNo = scanner.nextLine();
if(menuNo.equals("1")) {
//boards 테이블에 게시물 정보 저장
try {
String sql = "" +
"INSERT INTO boards (bno, btitle, bcontent, bwriter, bdate) " +
"VALUES (SEQ_BNO.NEXTVAL, ?, ?, ?, SYSDATE)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, board.getBtitle());
pstmt.setString(2, board.getBcontent());
pstmt.setString(3, board.getBwriter());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
}
//게시물 목록 출력
list();
}
public void read() {
//입력 받기
System.out.println("[게시물 읽기]");
System.out.print("bno: ");
int bno = Integer.parseInt(scanner.nextLine());
//boards 테이블에서 해당 게시물을 가져와 출력
try {
String sql = "" +
"SELECT bno, btitle, bcontent, bwriter, bdate " +
"FROM boards " +
"WHERE bno=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bno);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
Board board = new Board();
board.setBno(rs.getInt("bno"));
board.setBtitle(rs.getString("btitle"));
board.setBcontent(rs.getString("bcontent"));
board.setBwriter(rs.getString("bwriter"));
board.setBdate(rs.getDate("bdate"));
System.out.println("#############");
System.out.println("번호: " + board.getBno());
System.out.println("제목: " + board.getBtitle());
System.out.println("내용: " + board.getBcontent());
System.out.println("작성자: " + board.getBwriter());
System.out.println("날짜: " + board.getBdate());
//보조 메뉴 출력
System.out.println("-------------------------------------------------------------------");
System.out.println("보조 메뉴: 1.Update | 2.Delete | 3.List");
System.out.print("메뉴 선택: ");
String menuNo = scanner.nextLine();
System.out.println();
if(menuNo.equals("1")) {
update(board);
} else if(menuNo.equals("2")) {
delete(board);
}
}
rs.close();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
//게시물 목록 출력
list();
}
public void update(Board board) {
//수정 내용 입력 받기
System.out.println("[수정 내용 입력]");
System.out.print("제목: ");
board.setBtitle(scanner.nextLine());
System.out.print("내용: ");
board.setBcontent(scanner.nextLine());
System.out.print("작성자: ");
board.setBwriter(scanner.nextLine());
//보조 메뉴 출력
System.out.println("-------------------------------------------------------------------");
System.out.println("보조 메뉴: 1.Ok | 2.Cancel");
System.out.print("메뉴 선택: ");
String menuNo = scanner.nextLine();
if(menuNo.equals("1")) {
//boards 테이블에서 게시물 정보 수정
try {
String sql = "" +
"UPDATE boards SET btitle=?, bcontent=?, bwriter=? " +
"WHERE bno=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, board.getBtitle());
pstmt.setString(2, board.getBcontent());
pstmt.setString(3, board.getBwriter());
pstmt.setInt(4, board.getBno());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
}
//게시물 목록 출력
list();
}
public void delete(Board board) {
//boards 테이블에 게시물 정보 삭제
try {
String sql = "DELETE FROM boards WHERE bno=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, board.getBno());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
//게시물 목록 출력
list();
}
public void clear() {
System.out.println("[게시물 전체 삭제]");
System.out.println("-------------------------------------------------------------------");
System.out.println("보조 메뉴: 1.Ok | 2.Cancel");
System.out.print("메뉴 선택: ");
String menuNo = scanner.nextLine();
if(menuNo.equals("1")) {
//boards 테이블에 게시물 정보 전체 삭제
try {
String sql = "TRUNCATE TABLE boards";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
}
//게시물 목록 출력
list();
}
public void exit() {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
System.out.println("** 게시판 종료 **");
System.exit(0);
}
public static void main(String[] args) {
BoardExample9 boardExample = new BoardExample9();
boardExample.list();
}
}
MySQL
Board.java
import java.util.Date;
public class Board {
private int bno;
private String btitle;
private String bcontent;
private String bwriter;
private Date bdate;
public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}
public String getBtitle() {
return btitle;
}
public void setBtitle(String btitle) {
this.btitle = btitle;
}
public String getBcontent() {
return bcontent;
}
public void setBcontent(String bcontent) {
this.bcontent = bcontent;
}
public String getBwriter() {
return bwriter;
}
public void setBwriter(String bwriter) {
this.bwriter = bwriter;
}
public Date getBdate() {
return bdate;
}
public void setBdate(Date bdate) {
this.bdate = bdate;
}
}
BoardExample9.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class BoardExample9 {
//Field
private Scanner scanner = new Scanner(System.in);
private Connection conn;
//Constructor
public BoardExample9() {
try {
//JDBC Driver 등록
Class.forName("com.mysql.cj.jdbc.Driver");
//연결하기
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/thisisjava",
"java",
"mysql"
);
} catch(Exception e) {
e.printStackTrace();
exit();
}
}
//Method
public void list() {
//타이틀 및 컬럼명 출력
System.out.println();
System.out.println("[게시물 목록]");
System.out.println("-----------------------------------------------------------------------");
System.out.printf("%-6s%-12s%-16s%-40s\n", "no", "writer", "date", "title");
System.out.println("-----------------------------------------------------------------------");
//boads 테이블에서 게시물 정보를 가져와서 출력하기
try {
String sql = "" +
"SELECT bno, btitle, bcontent, bwriter, bdate " +
"FROM boards " +
"ORDER BY bno DESC";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
Board board = new Board();
board.setBno(rs.getInt("bno"));
board.setBtitle(rs.getString("btitle"));
board.setBcontent(rs.getString("bcontent"));
board.setBwriter(rs.getString("bwriter"));
board.setBdate(rs.getDate("bdate"));
System.out.printf("%-6s%-12s%-16s%-40s \n",
board.getBno(),
board.getBwriter(),
board.getBdate(),
board.getBtitle());
}
rs.close();
pstmt.close();
} catch(SQLException e) {
e.printStackTrace();
exit();
}
//메인 메뉴 출력
mainMenu();
}
public void mainMenu() {
System.out.println();
System.out.println("-----------------------------------------------------------------------");
System.out.println("메인메뉴: 1.Create | 2.Read | 3.Clear | 4.Exit");
System.out.print("메뉴선택: ");
String menuNo = scanner.nextLine();
System.out.println();
switch(menuNo) {
case "1" -> create();
case "2" -> read();
case "3" -> clear();
case "4" -> exit();
}
}
public void create() {
//입력 받기
Board board = new Board();
System.out.println("[새 게시물 입력]");
System.out.print("제목: ");
board.setBtitle(scanner.nextLine());
System.out.print("내용: ");
board.setBcontent(scanner.nextLine());
System.out.print("글쓴이: ");
board.setBwriter(scanner.nextLine());
//보조메뉴 출력
System.out.println("-----------------------------------------------------------------------");
System.out.println("보조메뉴: 1.Ok | 2.Cancel");
System.out.print("메뉴선택: ");
String menuNo = scanner.nextLine();
if(menuNo.equals("1")) {
//boards 테이블에 게시물 정보 저장
try {
String sql = "" +
"INSERT INTO boards (btitle, bcontent, bwriter, bdate) " +
"VALUES (?, ?, ?, now())";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, board.getBtitle());
pstmt.setString(2, board.getBcontent());
pstmt.setString(3, board.getBwriter());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
}
//게시물 목록 출력
list();
}
public void read() {
//입력 받기
System.out.println("[게시물 읽기]");
System.out.print("bno: ");
int bno = Integer.parseInt(scanner.nextLine());
//boards 테이블에서 해당 게시물을 가져와 출력
try {
String sql = "" +
"SELECT bno, btitle, bcontent, bwriter, bdate " +
"FROM boards " +
"WHERE bno=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, bno);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
Board board = new Board();
board.setBno(rs.getInt("bno"));
board.setBtitle(rs.getString("btitle"));
board.setBcontent(rs.getString("bcontent"));
board.setBwriter(rs.getString("bwriter"));
board.setBdate(rs.getDate("bdate"));
System.out.println("#############");
System.out.println("번호: " + board.getBno());
System.out.println("제목: " + board.getBtitle());
System.out.println("내용: " + board.getBcontent());
System.out.println("쓴이: " + board.getBwriter());
System.out.println("날짜: " + board.getBdate());
//보조메뉴 출력
System.out.println("-------------------------------------------------------------------");
System.out.println("보조메뉴: 1.Update | 2.Delete | 3.List");
System.out.print("메뉴선택: ");
String menuNo = scanner.nextLine();
System.out.println();
if(menuNo.equals("1")) {
update(board);
} else if(menuNo.equals("2")) {
delete(board);
}
}
rs.close();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
//게시물 목록 출력
list();
}
public void update(Board board) {
//수정 내용 입력 받기
System.out.println("[수정 내용 입력]");
System.out.print("제목: ");
board.setBtitle(scanner.nextLine());
System.out.print("내용: ");
board.setBcontent(scanner.nextLine());
System.out.print("글쓴이: ");
board.setBwriter(scanner.nextLine());
//보조메뉴 출력
System.out.println("-------------------------------------------------------------------");
System.out.println("보조메뉴: 1.Ok | 2.Cancel");
System.out.print("메뉴선택: ");
String menuNo = scanner.nextLine();
if(menuNo.equals("1")) {
//boards 테이블에서 게시물 정보 수정
try {
String sql = "" +
"UPDATE boards SET btitle=?, bcontent=?, bwriter=? " +
"WHERE bno=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, board.getBtitle());
pstmt.setString(2, board.getBcontent());
pstmt.setString(3, board.getBwriter());
pstmt.setInt(4, board.getBno());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
}
//게시물 목록 출력
list();
}
public void delete(Board board) {
//boards 테이블에 게시물 정보 삭제
try {
String sql = "DELETE FROM boards WHERE bno=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, board.getBno());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
//게시물 목록 출력
list();
}
public void clear() {
System.out.println("[게시물 전체 삭제]");
System.out.println("-------------------------------------------------------------------");
System.out.println("보조메뉴: 1.Ok | 2.Cancel");
System.out.print("메뉴선택: ");
String menuNo = scanner.nextLine();
if(menuNo.equals("1")) {
//boards 테이블에 게시물 정보 전체 삭제
try {
String sql = "TRUNCATE TABLE boards";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
exit();
}
}
//게시물 목록 출력
list();
}
public void exit() {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
System.out.println("** 게시판 종료 **");
System.exit(0);
}
public static void main(String[] args) {
BoardExample9 boardExample = new BoardExample9();
boardExample.list();
}
}
'Java Category > Java' 카테고리의 다른 글
[Java] char 타입을 정수 타입으로 변환 (3) | 2023.11.26 |
---|---|
[Java] next()와 nextLine()의 차이 (0) | 2023.08.22 |
[Java] DB 트랜잭션 처리 (0) | 2023.08.20 |
[Java] DB 프로시저와 함수 호출 (0) | 2023.08.19 |
[Java] 데이터베이스에 저장(쓰기, 수정, 삭제) 및 읽기 (0) | 2023.08.18 |