[jsp] 이클립스 jsp로 DB select 하기

seulki·2022년 10월 19일
0

jsp

목록 보기
33/51
post-thumbnail
post-custom-banner

🎈 한 개 select하기

jdbc_select.jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
		<%
			Connection conn = null;
			String driver = "oracle.jdbc.driver.OracleDriver";
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String user = "jsp";
			String password = "jsp";
						
			Class.forName(driver);
					
			conn = DriverManager.getConnection(url, user, password);
			
			String sql = "SELECT sysdate FROM dual";
			
			PreparedStatement pstm = conn.prepareStatement(sql);
			ResultSet rs = pstm.executeQuery();
			
			if(rs.next()){
				out.println(rs.getDate(1));
			}
			
			//close 반납
			rs.close();
			pstm.close();
			conn.close();
		%>
</body>


🎈 여러 column select 하기

jdbc_select2.jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	Connection conn = null;
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@localhost:1521:xe";
	String user = "jsp";
	String password = "jsp";
			
	int check = 0;
			
	Class.forName(driver);
						
	conn = DriverManager.getConnection(url, user, password);
			
	String sql = "SELECT * FROM car";
			
	PreparedStatement pstm = conn.prepareStatement(sql);
	ResultSet rs = pstm.executeQuery();
	//테이블 전체 가져오기	
	while(rs.next()){
		out.println("차 번호 : " + rs.getString(1) + "</br>");
		out.println("브랜드 : " + rs.getString("brand") + "</br>");
		out.println("색상 :" + rs.getString(3) + "</br>");
		out.println("가격 : " + rs.getInt("price") + "원</br>");
			}
			
	out.println("</br><hr>");
	//carnum이 3인 car의 color만 가져오기
    String carnum = "3";	
	sql = "SELECT color FROM car WHERE CARNUM = ? ";
	pstm = conn.prepareStatement(sql);
	pstm.setString(1, carnum); // 첫번째 ? 에는 carnum을 넣어라
					
	rs = pstm.executeQuery();
			
	if(rs.next()){
		out.println("컬러 : " + rs.getString(1));
			}
			
	//close 반납
	rs.close();
	pstm.close();
	conn.close();
%>
</body>
</html>


profile
웹 개발자 공부 중
post-custom-banner

0개의 댓글