관리 메뉴

moozi

customerdb.jsp 본문

안드로이드개발강좌

customerdb.jsp

moozi 2017. 9. 29. 11:06

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%!//선언부는 첫 방문자에 의해서 단 한번 수행합니다.
 Connection conn = null;
 Statement stmt = null;
 ResultSet rs = null;
 String url = "jdbc:oracle:thin:@localhost:1521:orcl";
 String uid = "hr";
 String pass = "1234";
 String sql = "select * from customer";%>

  <%
   try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection(url, uid, pass);
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
    String strJson="";
    strJson="{\"customers\": [";
   
    while(rs.next()) { 
     strJson+="{\"id\":\""+rs.getString("id")
       +"\", \"name\":\""+rs.getString("name")
       +"\", \"address\":\""
       +rs.getString("address")+"\"},";
    }
    strJson=strJson.substring(0,strJson.length()-1);//마지막 쉼표 제거
    strJson+="]}"; 
    out.println(strJson);
   } catch (Exception e) {
    out.println(e.getMessage());
    //e.printStackTrace();
   } finally {
    try {
     if (rs != null)
      rs.close();
     if (stmt != null)
      stmt.close();
     if (conn != null)
      conn.close();
    } catch (Exception e) {
     e.printStackTrace();
    }
   }//finally의 끝
  %>

'안드로이드개발강좌' 카테고리의 다른 글

json데이터 java코드로 뷰에 출력하기  (0) 2017.09.29
json데이터 listview에 출력하기  (0) 2017.09.29
customerdb.jsp  (0) 2017.09.29
jspconn01  (0) 2017.09.29
customers.jsp  (0) 2017.09.29
android xml 파싱  (0) 2017.09.27
0 Comments
댓글쓰기 폼