- jdbc : 자바 어플리케이션에서 표준화된 데이터베이스 접근 제공
각 데이터베이스 접속에 대한 상세한 정보 알 필요 없음
이론적으로는 개발된 애플리케이션에서 db변경시 jdbc 드라이버만 교체하면 됨


- 드라이버 로드 : Class.forName("oracle.jdbc.driver.OracleDriver")
- 디비 연결  : Connection  conn = DriverManager.getConnection(JDBC_url,"아이디","비번");
- rul구성 : JDBC:oracle:thin:@ip주소:포트:sid

s-tatement 생성 및 쿼리
statement 객체 생성 후 sql문장을 변수 처리부와 하게 문자열 구성
Statement st = conn.createStatement();
st.executeUpdate('"insert into test values"+request.getParameter("username").......');

- PreparedStatement :sql문장 미리 생성하고 변수부는 따로 메소드로 대입 성능과 관리 면에서 모두 권장함

PreparedStatement pst = conn.preparedState("insert into test values(?,?)");
pst.setString(1,request.getParameter('username'));
pst.setString(1,request.getParameter('age'));
pst.executeUpdate();


ResultSet rs = pst.executeQuery();
next() 통해 로우 이동

while(rs.next()){
name = rs.getString(1); //1은 인덱스, 1부터 시작함 or rs.getString('username');
age = rs.getString(2);
}

rs.close();

 

 

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 <%@ page import = "java.sql.*"%>
 <%
 	Connection conn = null;
 	String driver = "oracle.jdbc.driver.OracleDriver";
 	String url = "jdbc:oracle:thin:@localhost:1521:본인의 sid";
 	
 	boolean connect = false;
 	
 	try{
 		Class.forName(driver);
 		conn = DriverManager.getConnection(url,"아이디","비밀번호");
 		connect = true;
 		conn.close();
 		
 	}catch(Exception e){
 		connect = false;
 		e.printStackTrace();
 		
 	}
 
 
 %>
 
 
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3>

	<% if(connect == true) {%>
		연결되었습니다.
	<% }else  {%>
		실패하였습니다.
	<%} %>
</h3>
</body>
</html>







 

'Servlet & JSP' 카테고리의 다른 글

MVC 패턴  (0) 2019.07.03
beans 예제_2  (2) 2019.06.25
beans 예제  (0) 2019.06.25
REQUEST 예제_SCOPE 안쓰고 데이터 넘기기  (0) 2019.06.24
SESSION 예제_장바구니  (0) 2019.06.24

+ Recent posts