Table 만들기 / Table 삭제하기
📌 Note Code
Test1. 사용자 메뉴 완성하기
💻 입력
package com.dayDB;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Scanner;
import com.db.DBConn;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Connection conn = DBConn.getConnection();
Statement stmt = null;
String sql;
//사용자에게 메뉴 보여줌
int ch;
int id;
String name,birth,tel;
try {
while(true) {
do{
System.out.print("1.입력 2.출력 3.종료 : ");
ch = sc.nextInt();
}while(ch<1);
switch (ch) {
case 1:
conn.setAutoCommit(false); //자바는 자동커밋됨
System.out.print("아이디?");
id = sc.nextInt();
System.out.print("이름?");
name = sc.next();
System.out.print("생일?");// 1990-10-10
birth = sc.next();
System.out.print("전화?");
tel = sc.next();
//아래 3개의 문장이 다 들어가야지 완성형이 되고 => commit하면 됨
sql = String.format("insert into test1 (id,name) values (%d, '%s')", id, name);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
sql = String.format("insert into test2 (id,birth) values (%d, '%s')", id, birth);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
sql = String.format("insert into test3 (id,tel) values (%d, '%s')", id, tel);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
conn.commit();
break;
case 2:
break;
case 3:
DBConn.close();
System.exit(0);
break;
default:
break;
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
1.입력 2.출력 3.종료 : 1
아이디?111
이름?suzi
생일?1996-08-24
전화?010-2686-3448
1.입력 2.출력 3.종료 :
.
.
.