반응형
DTO(Data Transfer Object) : 특정 테이블의 정보를 레코드 단위로 정의해놓은 클래스
DBConn클래스
class DBConn{
private static DBConn db;
public static DBConn getInstance() {
if(db==null) {
db = new DBConn();
}
return db;
}
public Connection getconn() {
Connection con = null;
try {
Class.forName("org.mariadb.jdbc.Driver");
String url = "jdbc:mariadb://220.70.33.29:3306/knowhoon";
con = DriverManager.getConnection(url, "knowhoon", "01234567");
} catch (Exception e) {
e.printStackTrace();
System.out.println("sql 접속오류");
}
return con;
}
}
DTO 클래스
public class LoginDTO {
private int no;
private String name;
private String id;
private String pw;
private String email;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
main
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
//pstmt를 활용한 검색
public class SELECT {
public ArrayList<LoginDTO> select() {
// CONN -> DBConn연결
// PSTMT - conn접속 정보 넣기
// ArrayList<Map<String, Object>>
// DTO -> ArrayList<DTO>
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String query = "SELECT * FROM login";
ArrayList<LoginDTO> list = new ArrayList<LoginDTO>(); // 담을 객체 만들기
// DBConn dbc = DBConn.getInstance();
// conn = dbc.getconn();
conn = DBConn.getInstance().getconn();
try {
pstmt = conn.prepareStatement(query);
rs = pstmt.executeQuery();
// 뽑아내기
while (rs.next()) {
// 레코드를 담을 DTO생성
LoginDTO dto = new LoginDTO();
dto.setNo(rs.getInt("no"));
dto.setName(rs.getString("name"));
dto.setId(rs.getString("id"));
dto.setPw(rs.getString("pw"));
dto.setEmail(rs.getString("email"));
list.add(dto);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
public void insert(LoginDTO dto){
Connection conn = DBConn.getInstance().getconn();
PreparedStatement pstmt = null;
String insert = "INSERT INTO login (name, id, pw, email) VALUES(?, ? ,? ,?)";
try {
pstmt = conn.prepareStatement(insert);
pstmt.setString(1, dto.getName());
pstmt.setString(2, dto.getId());
pstmt.setString(3, dto.getPw());
pstmt.setString(4, dto.getEmail());
pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
SELECT select = new SELECT();
// list출력
ArrayList<LoginDTO> list = select.select();
for (LoginDTO dto : list) {
System.out.print(dto.getNo() + "\t");
System.out.print(dto.getName() + "\t");
System.out.print(dto.getId() + "\t");
System.out.print(dto.getPw() + "\t");
System.out.println(dto.getEmail());
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i).getNo() + "\t");
System.out.print(list.get(i).getName() + "\t");
System.out.print(list.get(i).getId() + "\t");
System.out.print(list.get(i).getPw() + "\t");
System.out.println(list.get(i).getEmail());
}
//사용자 입력으로 데이터베이스 저장하기
//1. 스캐너 사용자가 입력
//2. DTO에 넣어서 입력메소드로 전송
Scanner sc = new Scanner(System.in);
System.out.println("이름 입력하세요.");
//String name = sc.next();
LoginDTO dto = new LoginDTO();
dto.setName(sc.next());
System.out.println("ID 입력하세요.");
dto.setId(sc.next());
System.out.println("PW 입력하세요.");
dto.setPw(sc.next());
System.out.println("email을 입력하세요.");
dto.setEmail(sc.next());
select.insert(dto);
sc.close();
}
}
반응형
'프로그래밍 > Java 공부' 카테고리의 다른 글
자바 Servlet - 세션 이용 로그인/ 로그아웃 기능 (0) | 2021.07.06 |
---|---|
JAVA - Thread(스레드) (0) | 2021.06.30 |
Java_PreparedStatement (0) | 2021.06.23 |
자바_POI활용, JXL활용 엑셀 문서로 저장하기 (0) | 2021.06.23 |
Java_IO(파일 입출력) (0) | 2021.06.22 |