DriverManagerJDBC 드라이버의 가장 기본적인 서비스다. 자바에서 작성한 쿼리문을

연동하는데 아래 코드를 기본적으로 사용한다


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


+ Recent posts