[JSP] SQL 연동 기본 개념
1. JDBC 드라이버 로딩 (MSSQL 경우)
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
2. Connection 객체 생성
Connection conn = DriverManager.getConnection(url);
getConnection()메소드는 세개의 매개변수(String로도 사용 가능
Connection conn = DriverManager.getConnection(url, user, password);
3. Connection 종료
conn.close();
4. Statement 객체 사용
Statement stmt = conn.createStatement();
5. 질의문 실행
stmt.executeUpdate(String sql);
결과 값이 있을 경우는 executeQuery() 매소드 사용
ResultSet rs = stmt.executeQuery(String sql);
5-1 ResultSet 사용
while(rs.next()){
out.print(rs.getString(1));
out.print(rs.getInt("age"));
}
5-2 ResultSet 종료
rs.close();
6. Statement 종료
stmt.close();
Etc. PreparedStatement 사용
String sql = "Insert into JSP (name, number) value (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,"JHSHIN");
pstmt.setInt(2, 1);
pstmt.executeUpdate();
Etc. PreparedStatement 종료
pstmt.close();