[JSP] JDBC 연동 (MySQL) - (2)

 

📌테이블 생성


testDB에 test라는 table을 하나 생성해주었다.

 

 

 

 

 

 

 

 

 

 

 

📌실습 예제


예제로 살펴보기에 앞서 이에 쓰이는 클래스들의 문법을 간단히 살펴보도록 하겠다.

💡Statement / PreparedStatement

  • executeQuery(String sql)
    • executeQuery() 메서드의 경우는 SELECT문을 실행할 때 사용된다.
    • executeQuery() 메서드는 ResultSet 객체를 반환한다.

  • executeUpdate(String sql)
    • 삽입, 수정, 삭제와 관련된 SQL문 실행에 사용된다.
    • 수정된 레코드 수를 반환한다.

Statement / PreparedStatement의 차이점: https://yeo-computerclass.tistory.com/143

 

Statement와 PreparedStatement의 차이점

📌Statement Statement stmt = conn.createStatement(); stmt.executeUpdate("INSERT INTO test(id, name) VALUES (1,'여승철')"); stmt.executeUpdate("INSERT INTO test(id, name) VALUES (2,'여승철')"); stmt...

yeo-computerclass.tistory.com

 

 

 

 

💡ResultSet

  • ResultSet
    • ResultSet이란 Statement 객체 또는 PreparedStatement 객체로 SELECT문을 사용하여 얻어온 레코드 값들을 테이블의 형태로 갖게되는 객체이다.
메소드 설명
close() ResultSet 객체를 밴환한다.(닫는다.)
getXXX(int ColumnIndex) 인자로 지정된 번호의 컬럼값을 XXX 데이터 타입으로 가져온다. (컬럼 인덱스 지정)
getXXX(String ColumnName) 인자로 지정한 컬럼명의 컬럼값을 XXX 데이터 타입으로 가젼온다.(컬럼명 지정)
next() 다음 행(레코드)으로 커서(작업 위치)를 이동한다. 다음 행이 없으면 false 반환, 있으면 true 반환.

 

ResultSet에 대한 자세한 설명: https://yeo-computerclass.tistory.com/144

 

ResultSet과 ResultSetMetaData (feat. 커서 이동 옵션)

ResultSet 예제 참고 https://yeo-computerclass.tistory.com/142?category=1269285 [JSP] JDBC 연동 (MySQL) - (2) 📌세팅 testDB에 test라는 table을 하나 생성해주었다. statementTest.jsp executeQuery(String..

yeo-computerclass.tistory.com

 

 

 

statementTest.jsp

 

 

 

 

결과