728x90
Java + 오라클, 즉, JDBC를 활용한 미니 주소록
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | package org.example; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.*; import java.util.Scanner; import static org.example.JDBCTest01.makeConnection; public class MiniClass { private static String name, phone, address, birthday; //테이블 생성 public static void CreateTable() { Connection conn = null; Statement stmt =null; try{ String url = "jdbc:oracle:thin:@localhost:1521:xe"; //localhost대신 ip주소가 들어갈수도 String id = "hr"; String pw = "hr"; Class.forName("oracle.jdbc.driver.OracleDriver"); System.out.println("드라이버 적재 성공"); conn = DriverManager.getConnection(url, id, pw); System.out.println("데이터베이스 연결 성공"); stmt = conn.createStatement(); StringBuffer sql = new StringBuffer(); sql.append("create table student(num number(4)"); sql.append(", name varchar2(20), phone varchar2(15)"); sql.append(", address varchar(50), birthday varchar2(20)"); sql.append(", constraint student_num_pk primary key(num))"); stmt.executeUpdate(sql.toString()); System.out.println("테이블을 성공적으로 생성했습니다."); } catch (ClassNotFoundException e) { System.out.println("드라이버를 찾을 수 없습니다."); } catch (SQLException e) { e.printStackTrace(); System.out.println("연결에 실패하였습니다."); } finally{ try{ if(stmt != null) stmt.close(); if(conn != null) conn.close(); }catch(SQLException e){} } } //데이터 삽입 public static void InsertStudent(String name, String phone, String address, String birthday){ try{ Connection conn = makeConnection(); String query = "INSERT INTO student (NUM, NAME, PHONE, ADDRESS, BIRTHDAY) VALUES (STUDENT_SEQ.nextval, ?, ?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(query); pstmt.setString(1, name); pstmt.setString(2, phone); pstmt.setString(3, address); pstmt.setString(4, birthday); int ret = pstmt.executeUpdate(); System.out.println(ret); } catch (SQLException throwables) { throwables.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } //데이터 조회 public static void SelectStudent() { try { Connection conn = makeConnection(); Statement stmt = conn.createStatement(); String sql = "select * from student"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { int num = rs.getInt("num"); String name = rs.getString("name"); String phone = rs.getString("phone"); String address = rs.getString("address"); String birthday = rs.getString("birthday"); System.out.println("번호 : " + num); System.out.println("이름 : " + name); System.out.println("전화번호 : " + phone); System.out.println("주소 : " + address); System.out.println("생년월일 : " + birthday); System.out.println("======================================"); } } catch (SQLException e) { e.printStackTrace(); } } //데이터 수정 public static void UpdateStudent(int num, String name, String phone, String address, String birthday){ try { Connection conn = makeConnection(); String sql = "update student set NAME =\'" +name + "\', PHONE=\'" + phone + "\', " + "ADDRESS =\'" + address + "\', BIRTHDAY = \'" + birthday + "\' where NUM= "+num; PreparedStatement pstmt = conn.prepareStatement(sql); int ret = 0; ret = pstmt.executeUpdate(); System.out.println(ret); } catch (SQLException throwables) { throwables.printStackTrace(); } } //데이터 삭제 public static void DeleteStudent(int num){ try { Connection conn = makeConnection(); Statement stmt = conn.createStatement(); String sql = "delete student where num =" + num; PreparedStatement pstmt = conn.prepareStatement(sql); int ret = 0; ret = pstmt.executeUpdate(); System.out.println(ret); } catch (SQLException throwables) { throwables.printStackTrace(); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); do{ System.out.println("1. 테이블 생성"); System.out.println("2. 주소록 조회"); System.out.println("3. 주소록 추가"); System.out.println("4. 주소록 수정"); System.out.println("5. 테이블 삭제"); System.out.print("메뉴를 선택하세요. : "); //int menu = Integer.parseInt(sc.next()); //한글입력시 바로 Runtime Error발생 String str = sc.nextLine(); boolean isNumeric = str.matches("[+-]?\\d*(\\.\\d+)?"); //정규식 표현->숫자만 int menu=0; if(isNumeric) { menu = Integer.parseInt(str); }else{ menu = 100; //잘못된 값을 모두 100으로 변환 } switch(menu) { case 1: CreateTable(); System.out.println("테이블을 생성합니다."); break; case 2: SelectStudent(); System.out.println("주소록을 조회합니다."); break; case 3: System.out.print("입력: " ); Scanner sc1 = new Scanner(System.in); Scanner sc2 = new Scanner(System.in); Scanner sc3 = new Scanner(System.in); Scanner sc4 = new Scanner(System.in); InsertStudent(sc1.next(),sc2.next(),sc3.next(),sc4.next()); System.out.println("주소록을 추가합니다."); break; case 4: System.out.print("입력: " ); Scanner s = new Scanner(System.in); Scanner s1 = new Scanner(System.in); Scanner s2 = new Scanner(System.in); Scanner s3 = new Scanner(System.in); Scanner s4 = new Scanner(System.in); UpdateStudent(s.nextInt(), s1.next(),s2.next(),s3.next(),s4.next()); System.out.println("주소록을 수정합니다."); break; case 5: System.out.print("입력: " ); Scanner ds = new Scanner(System.in); DeleteStudent(ds.nextInt()); System.out.println("테이블을 삭제합니다."); break; default: System.out.println("잘못된 값이 입력되었습니다.\n입력을 다시 해주세요."); break; } }while(true); } } | cs |
2. 오라클 DB을 활용한 콘솔용 미니 게임 프로젝트
Main,java
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 32 33 34 35 36 37 38 39 40 41 42 | package com.suCorp; import java.util.Scanner; public class Main { public static void main(String[] args) { while(true) { clearConsole(); System.out.println("1. Game Start\n2. Record\n3. Exit"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); switch (num) { case 1: clearConsole(); System.out.println("Start"); sc = new Scanner(System.in); sc.nextLine(); clearConsole(); GameMain gm = new GameMain(); while(true){ if(gm.init()==1) break; } break; case 2: clearConsole(); System.out.println("Record"); sc = new Scanner(System.in); sc.nextLine(); break; case 3: clearConsole(); System.out.println("Exit"); return; } } } public static void clearConsole() { System.out.println(new String(new char[50]).replace("\0", "\r\n")); } } | cs |
GameMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.suCorp; import java.util.Scanner; public class GameMain { public static void main(String[] args) { } public int init(){ System.out.print("Write your character name: "); Scanner sc = new Scanner(System.in); String name = sc.next(); System.out.println(name); System.out.println("0"); sc = new Scanner(System.in); if(sc.next()!="") return 1; return 0; } } | cs |
키우기 게임 계획 - 미완성
콘솔에 특수기호 같은 것이 출력이 힘들어서 0,1,2, ... , 9 같은 식으로 성장하는 형태로 계획
밥주기, 운동, 대변 치우기 등의 관심을 주면 시간이 지날수록 커가는 식.
껐다 켰을 경우 캐릭터의 상태를 DB에서 불러와 그대로 이어서 플레이하는 방식
728x90
'Java' 카테고리의 다른 글
| 스마트팩토리 11주 54일차 java 6일차 (0) | 2021.07.14 |
|---|---|
| 스마트팩토리 11주 53일차 java 5일차 (0) | 2021.07.12 |
| 스마트팩토리 11주 51일차 java 3일차 (0) | 2021.07.08 |
| 스마트팩토리 11주 50일차 java 2일차 (0) | 2021.07.06 |
| 스마트팩토리 11주 49일차 java 1일차 (0) | 2021.07.05 |




최근댓글