Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- ubuntu
- 백준알고리즘
- MySQL
- github action
- Python
- JSP
- sqli
- virtualbox
- 자료구조
- CodeQL
- 운영체제
- OpenSource
- 알고리즘
- firewall
- Codeup
- 데이터통신
- LGTM
- Database
- goKart
- SUA
- gotify
- DVWA
- OWASP
- juice-shop
- Network
- gosec
- Juice Shop
- JDBC
- github
- C언어
Archives
- Today
- Total
비트(bit)주세요
JDBC 본문
728x90
JSP에서 DB를 연동하기 위해선 JDBC가 필요합니다.
JDBC는 자바에서 데이터베이스에 접속할 수 있도록 하는 자바 API입니다.
JDBC를 이용한 데이터베이스 연동과정은 다음과 같습니다.
① Package Import
<%@ page import java.sql.* %>
② JDBC 드라이버 Load
Class.forName("com.mysql.jdbc.Driver");
③ Connection 객체 생성
Connection conn = null;
String url ="jdbc:mysql://localhost:3306/[DB 이름]";
String user = "[DB 계정 아이디]";
String password = "[DB 계정 패스워드]";
conn = DriverManager.getConnection(url, user, password);
④ Statement 객체 생성
PreparedStatement pstmt = null;
String sql = "select * from student";
pstmt = conn.prepareStatement(sql);
Statement?
: 자바는 스스로 SQL 구문을 이해하지 못합니다.
-> 그래서 Statement가 자바에게 SQL 쿼리를 이해시켜주고 실행시켜줍니다.
PreparedStatement?
: Statement보다 향상된 기능을 가집니다.
인자와 관련된 작업에 특화되어 있습니다.
코드 안정성과 가독성이 높습니다.
코드량이 증가합니다.
⑤ Query 수행
ResultSet rs = null;
rs = pstmt.executeQuery(); // SQL 쿼리가 select일때
rs = pstmt.executeUpdate(); // SQL 쿼리가 update, insert, delete일때
⑥ Result 객체로부터 데이터 추출
String name = rs.getString("name");
String depart = rs.getString("depart");
String phone = rs.getString("phone");
⑦ Resultset Close
rs.close();
⑧ Statement 객체 close
pstmt.close();
⑨ Connection 객체 close
conn.close();
DB 연동만 할 때의 소스코드
<%@ page import="java.sql.*"%>
<%request.setCharacterEncoding("utf-8"); %>
<%
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/[DB이름]"; //DB 경로설정
String user = "[DB 계정 아이디]";
String password = "[DB 계정 패스워드]";
Class.forName("com.mysql.jdbc.Driver"); //JDBC 드라이버 Load
conn = DriverManager.getConnection(url, user, password); //Connection 객체 생성
%>
'CS > 웹 프로그래밍(JSP)' 카테고리의 다른 글
createStatement, prepareStatement 차이 (0) | 2021.05.28 |
---|---|
내장 객체 (0) | 2021.04.22 |
JSP 액션태그 (0) | 2021.04.13 |
주석 (0) | 2021.04.13 |
JSP 스크립트 요소 (0) | 2021.04.13 |