wiki:JspJdbc

JDBC

package : java.sql

Interfaces

Connection

Driver

PreparedStatement?

ResultSet?

Statement

Classes

Class : java.lang 패키지

DriverManager?

Timestamp

Connection Method

void close()

Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released.

Statement createStatement()

Creates a Statement object for sending SQL statements to the database.

PreparedStatement? prepareStatement(String sql)

Creates a PreparedStatement? object for sending parameterized SQL statements to the database.

Driver Method

Class.forName("foo.bah.Driver")

PreparedStatement? Method

ResultSetexecuteQuery?()

Executes the SQL query in this PreparedStatement? object and returns the ResultSet? object generated by the query.

int executeUpdate()

Executes the SQL statement in this PreparedStatement? object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

  • void setter("이름", 값) 메소드들

이름을 만들때 javaBeans setter 메소드와 차이점은 jdbc set은 타입이 중요 setString()

beans set은 이름이 중요 setTitle()

Statement Method

void close()

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

ResultSetexecuteQuery?(String sql)

Executes the given SQL statement, which returns a single ResultSet? object.

int executeUpdate(String sql)

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

ResultSet? Method

close()

Releases this ResultSet? object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

boolean next()

Moves the cursor froward one row from its current position.

이름을 만들때 javaBeans getter 메소드와 차이점은 jdbc get은 타입이 중요 setString()

beans get은 이름이 중요 setTitle()

DriverManager? Method

static Connection getConnection(String url, String user, String password)

Attempts to establish a connection to the given database URL.

Class Method

static Class<?>forName(String className)

Returns the Class object associated with the class or interface with the given string name.

사용순서

1단계 : Driver 준비

  • Driver에 5개의 인터페이스가 구현되어 있슴
  • DBMS 벤더의 jdbc 드라이버를 프로젝트 WEB-INF\lib 폴더에 jar 파일 복사
  • 이클립스 Libraries에서 확인가능

2단계 : Class.forName 설정

  • Class.forName 메소드로 사용할 드라이버 명시
  • 드라이버 클래스는 이클립스 Libraries에서 확인 가능
Class.forName("oracle.jdbc.OracleDriver") 오라클용
Class.forName("com.mysql.jdbc.Driver") Mysql용

3단계 : DriverManager?.getConnection

getConnection(url, user, pass) 메소드의 내용

  • url - a database url of the form jdbc:subprotocol:subname
  • user - the database user on whose behalf the connection is being made
  • password - the user's password

("jdbc:oracle:thin:@172.16.4.11:1521:orcl", "soomang", "jjangdol") 오라클용 url
("jdbc:mysql://172.16.4.11:3306/test", "soomang", "jjangdol") Mysql용 url

  • 이클립스에서 getConnection 정보 확인

<Statement 방식>

  • 입력, 수정, 삭제 시
    • 4단계 : Connection.createStatement()
    • 5단계 : Statement.executeUpdate("delete from board")
    • 6단계 : Statement.colse()
    • 7단계 : Connection.close()

  • 선택문 사용시
    • 4단계 : Connection.createStatement()
    • 5단계 : Statement.executeQuery("select * from board")
    • 6단계 : ResultSet?.netxt()
    • 7단계 : ResultSet?.getter("필드명") 메소드
    • 8단계 : ResultSet?.close()
    • 9단계 : Statement.close()
    • 10단계 : Connection.close()

<PrepareStatement? 방식>

  • 1,2,3단계는 동일

  • 입력, 수정, 삭제 시

  • 선택문 사용시

ResultSet?.getInt("num") 또는 ResultSet?.getInt(1) ResultSet?.getString("title"); board.setTitle(ResultSet?.getString("title")) boardList.add(board) 어레이리스트 사용시

Attachments