목차 | |
1. | 실습 |
1. 실습
▣ StudentMain
package jdbcStudent;
public class StudentMain {
public static void main(String[] args) {
new StudentController();
}
}
▣ Student
package jdbcStudent;
public class Student {
// sno int auto_increment,
// sid int not null,
// sname varchar(50) not null,
// birth varchar(20),
// phone varchar(20),
// address text,
// regdate datetime default now()
private int sno;
private int sid;
private String sname;
private String birth;
private String phone;
private String address;
private String regdate;
public Student() {}
// 학생 등록
public Student(int sid, String sname, String birth, String phone, String address) {
this.sid = sid;
this.sname = sname;
this.birth = birth;
this.phone = phone;
this.address = address;
}
// 학생 리스트
public Student(int sno, int sid, String sname) {
this.sno = sno;
this.sid = sid;
this.sname = sname;
}
// 학생 상세 보기
public Student(int sno, int sid, String sname, String birth, String phone, String address, String regdate) {
this.sno = sno;
this.sid = sid;
this.sname = sname;
this.birth = birth;
this.phone = phone;
this.address = address;
this.regdate = regdate;
}
// 학생 수정
public Student(int sno, int sid, String sname, String birth, String phone, String address) {
this.sno = sno;
this.sid = sid;
this.sname = sname;
this.birth = birth;
this.phone = phone;
this.address = address;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRegdate() {
return regdate;
}
public void setRegdate(String regdate) {
this.regdate = regdate;
}
public String print() {
return sno + ". [" + sid + "] " + sname;
}
@Override
public String toString() {
return sno + ". [" + sid + "] " + sname + " | " + birth + " | " + phone + " | " + address + " | " + regdate;
}
}
▣ sqlFile
create table Student(
sno int auto_increment,
sid int not null,
sname varchar(50) not null,
birth varchar(20),
phone varchar(20),
address text,
regdate datetime default now(),
primary key(sno));
▣ StudentController
package jdbcStudent;
import java.util.List;
import java.util.Scanner;
public class StudentController {
private Scanner scan;
private Service svc;
private boolean flag;
public StudentController() {
scan = new Scanner(System.in);
svc = new StudentServiceImpl();
flag = true;
printMenu();
}
private void printMenu() {
while(flag) {
System.out.println("---------------------------------학생 관리 프로그램---------------------------------");
System.out.println(" ____ ______ __ __ ____ ____ __ __ ______ \r\n"
+ "/\\ _`\\ /\\__ _\\ /\\ \\/\\ \\ /\\ _`\\ /\\ _`\\ /\\ \\/\\ \\ /\\__ _\\ \r\n"
+ "\\ \\,\\L\\_\\ \\/_/\\ \\/ \\ \\ \\ \\ \\ \\ \\ \\/\\ \\ \\ \\ \\L\\_\\ \\ \\ `\\\\ \\ \\/_/\\ \\/ \r\n"
+ " \\/_\\__ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ _\\L \\ \\ , ` \\ \\ \\ \\ \r\n"
+ " /\\ \\L\\ \\ \\ \\ \\ \\ \\ \\_\\ \\ \\ \\ \\_\\ \\ \\ \\ \\L\\ \\ \\ \\ \\`\\ \\ \\ \\ \\ \r\n"
+ " \\ `\\____\\ \\ \\_\\ \\ \\_____\\ \\ \\____/ \\ \\____/ \\ \\_\\ \\_\\ \\ \\_\\\r\n"
+ " \\/_____/ \\/_/ \\/_____/ \\/___/ \\/___/ \\/_/\\/_/ \\/_/\r\n"
+ "");
System.out.println("--------------------------------------------------------------------------------");
System.out.println("1.학생등록|2.학생리스트|3.학생검색(내정보보기)|4.내정보수정|5.학생삭제|6.종료");
System.out.println("번호 입력 >");
int menu = scan.nextInt();
switch(menu) {
case 1: register(); break;
case 2: list(); break;
case 3: search(); break;
case 4: modify(); break;
case 5: delete(); break;
case 6:
flag = false;
System.out.println("종료합니다.");
break;
default : System.out.println("잘못된 번호입니다.");
}
}
}
private void delete() {
System.out.println("검색할 번호 입력 >");
int sno = scan.nextInt();
int isOk = svc.delete(sno);
System.out.println("학생 삭제 " + (isOk > 0 ? "성공" : "실패"));
}
private void modify() {
System.out.println("검색할 번호 입력 >");
int sno = scan.nextInt();
System.out.println("학생 학번 >");
int sid = scan.nextInt();
System.out.println("학생 이름 >");
scan.nextLine();
String sname = scan.nextLine();
System.out.println("학생 생년월일(ex:240101) >");
String birth = scan.next();
System.out.println("학생 전화번호(ex:01012341234) >");
String phone = scan.next();
System.out.println("학생 주소 >");
scan.nextLine();
String address = scan.nextLine();
Student s = new Student(sno, sid, sname, birth, phone, address);
int isOk = svc.update(s);
System.out.println("학생 등록 " + (isOk > 0 ? "성공" : "실패"));
}
private void search() {
System.out.println("검색할 번호 입력 >");
int sno = scan.nextInt();
Student s = svc.getStudent(sno);
System.out.println(s);
}
private void list() {
List<Student> list = svc.getList();
for(Student s : list) {
System.out.println(s.print());
}
}
private void register() {
System.out.println("학생 학번 >");
int sid = scan.nextInt();
System.out.println("학생 이름 >");
scan.nextLine();
String sname = scan.nextLine();
System.out.println("학생 생년월일(ex:240101) >");
String birth = scan.next();
System.out.println("학생 전화번호(ex:01012341234) >");
String phone = scan.next();
System.out.println("학생 주소 >");
scan.nextLine();
String address = scan.nextLine();
Student s = new Student(sid, sname, birth, phone, address);
int isOk = svc.insert(s);
System.out.println("학생 등록 " + (isOk > 0 ? "성공" : "실패"));
}
}
▣ Service
package jdbcStudent;
import java.util.List;
public interface Service {
int insert(Student s);
List<Student> getList();
Student getStudent(int sno);
int update(Student s);
int delete(int sno);
}
▣ StudentServiceImpl
package jdbcStudent;
import java.util.List;
public class StudentServiceImpl implements Service {
private DAO dao;
public StudentServiceImpl() {
dao = new StudentDAOImpl();
}
@Override
public int insert(Student s) {
// TODO Auto-generated method stub
System.out.println("학생 등록 serviceImpl success");
return dao.insert(s);
}
@Override
public List<Student> getList() {
System.out.println("학생 리스트 serviceImpl success");
return dao.selectList();
}
@Override
public Student getStudent(int sno) {
System.out.println("학생 정보 serviceImpl success");
return dao.selectStudent(sno);
}
@Override
public int update(Student s) {
System.out.println("학생 수정 serviceImpl success");
return dao.update(s);
}
@Override
public int delete(int sno) {
System.out.println("학생 삭제 serviceImpl success");
return dao.delete(sno);
}
}
▣ DAO
package jdbcStudent;
import java.util.List;
public interface DAO {
int insert(Student s);
List<Student> selectList();
Student selectStudent(int sno);
int update(Student s);
int delete(int sno);
}
▣ StudentDAOImpl
package jdbcStudent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import jdbc.DatabaseConnection;
public class StudentDAOImpl implements DAO {
public Connection conn;
private PreparedStatement pst;
private String query = "";
public StudentDAOImpl() {
DatabaseConnection dbc = DatabaseConnection.getInstance();
conn = dbc.getConnection();
}
@Override
public int insert(Student s) {
System.out.println("insert DAOImpl success");
query = "insert into student(sid, sname, birth, phone, address) values(?, ?, ?, ?, ?)";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, s.getSid());
pst.setString(2, s.getSname());
pst.setString(3, s.getBirth());
pst.setString(4, s.getPhone());
pst.setString(5, s.getAddress());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("insert error");
e.printStackTrace();
}
return 0;
}
@Override
public List<Student> selectList() {
System.out.println("학생 리스트 DAOImpl success");
query = "select * from student order by sno desc";
List<Student> list = new ArrayList<>();
try {
pst = conn.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
list.add(new Student(rs.getInt("sno"), rs.getInt("sid"), rs.getString("sname")));
}
return list;
} catch (SQLException e) {
System.out.println("list error");
e.printStackTrace();
}
return null;
}
@Override
public Student selectStudent(int sno) {
System.out.println("student DAOImpl success");
query = "select * from student where sno = ?";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, sno);
ResultSet rs = pst.executeQuery();
if(rs.next()) {
Student s = new Student(rs.getInt("sno"), rs.getInt("sid"), rs.getString("sname"), rs.getString("birth"), rs.getString("phone"), rs.getString("address"), rs.getString("regdate"));
return s;
}
} catch (SQLException e) {
System.out.println("student error");
e.printStackTrace();
}
return null;
}
@Override
public int update(Student s) {
System.out.println("update DAOImpl success");
query = "update student set sid = ?, sname = ?, birth = ?, phone = ?, address = ?, regdate = now() where sno = ?";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, s.getSid());
pst.setString(2, s.getSname());
pst.setString(3, s.getBirth());
pst.setString(4, s.getPhone());
pst.setString(5, s.getAddress());
pst.setInt(6, s.getSno());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("update error");
e.printStackTrace();
}
return 0;
}
@Override
public int delete(int sno) {
System.out.println("delete DAOImpl success");
query = "delete from student where sno = ?";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, sno);
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("delete error");
e.printStackTrace();
}
return 0;
}
}
▣ DatabaseConnection
package jdbcStudent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static DatabaseConnection dbc = new DatabaseConnection();
private Connection conn = null;
private String jdbDriver = "com.mysql.cj.jdbc.Driver";
private String jdbUrl = "jdbc:mysql://localhost:3306/javadb";
private DatabaseConnection() {
try {
Class.forName(jdbDriver);
conn = DriverManager.getConnection(jdbUrl, "javaUser", "mysql");
}catch (ClassNotFoundException e) {
System.out.println("드라이버를 찾을 수 없습니다");
e.printStackTrace();
}catch (SQLException e) {
System.out.println("연결정보가 정확하지 않습니다");
e.printStackTrace();
}
}
public static DatabaseConnection getInstance() {
return dbc;
}
public Connection getConnection() {
return conn;
}
}
▷ 출력
'Java' 카테고리의 다른 글
Java 기초(Thread, Singleton, DB 연결)- AWS 풀스택 과정 56일차 (0) | 2024.10.15 |
---|---|
Java 기초(File)- AWS 풀스택 과정 55일차 (0) | 2024.10.14 |
Java 기초(Generics, lambda)- AWS 풀스택 과정 54일차 (0) | 2024.10.11 |
Java 기초(map)- AWS 풀스택 과정 53일차 (4) | 2024.10.10 |
Java 기초(polymorphism)- AWS 풀스택 과정 52일차 (10) | 2024.10.08 |