DriverManager는 JDBC 드라이버의 가장 기본적인 서비스다. 자바에서 작성한 쿼리문을
연동하는데 아래 코드를 기본적으로 사용한다.
1 2 3 | Connection con=null; con=DriverManager.getConnection(sql); | cs |
API를 검색하면 형식이 Connection형일 정도로 Connection과 가깝고 인수로는 String url을 받는다.
이제 PreparedStatement, ResultSet(쿼리문이 select문 일경우)를 사용해서 쭉쭉 코드를 작성해보자.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.sql.*; public class abc extends HttpServlet{ // doGet( ) public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); // PrintWriter java에서 웹으로 출력 시 PrintWriter out= response.getWriter(); out.print("Servlet works<br>"); try{ Class.forName("oracle.jdbc.driver.OracleDriver"); out.print("driver success<br>"); Connection con=null; PreparedStatement pstmt=null; ResultSet rs=null; String url="jdbc:oracle:thin:@localhost:1521:XE"; String id="abc"; String pwd="abc"; String sql="select * from abc"; con=DriverManager.getConnection(sql); pstmt=com.prepareStatement(sql); rs=pstmt.executeQuery(); while(re.next()){ out.print(rs.getString("name")+"<br>"); } } catch(ClassNotFoundException e){ out.print("driver fail<br>"); } catch(SQLException){ } } } | cs |
'문돌이의 IT > 자바(Java)' 카테고리의 다른 글
자바(Java) 변수, 연산자 (0) | 2017.01.02 |
---|---|
자바(Java)프로그래밍이란? (0) | 2017.01.02 |
자바(Java) 구구단 소스코드 (0) | 2016.04.10 |
자바(Java) 내부익명클래스(anonymous class) 사용 방법 (2) | 2016.03.30 |
자바(Java) setLocationRelativeTo 사용방법 (2) | 2016.03.29 |