파일명 : DBconnection.java
package db.beans;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBconnection {
public static Connection getConnection() throws Exception {
System.out.println("DB연결시도");
Class.forName("oracle.jdbc.driver.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "madang", "madang");
}
public static Connection getConnection (String ip, int port, String db, String user, String pw) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:@" + ip + ":" + port + ":" + db, user, pw);
}
}
파일명 : dbTest.jsp
<%@ page import="db.beans.*, java.sql.*, java.util.*, java.io.*"%>
<%@ page contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR" %>
<jsp:useBean id="QueryBean" scope="page" class="db.beans.QueryBean" />
<jsp:setProperty name="QueryBean" property="*" />
<%
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
request.setCharacterEncoding("UTF-8");
QueryBean.getConnection();
ArrayList resArr = new ArrayList();
try {
resArr = QueryBean.getUserInfo();
} catch (SQLException e) {
out.print(e.toString());
} finally {
QueryBean.closeConnection();
}
out.println("{");
out.println("\"datas\":[");
if (resArr.size() == 0) {
out.println("]");
out.println("}");
} else {
out.print("{");
out.print("\"ID\": \"" + (String)resArr.get(0) + "\", ");
out.print("\"NAME\": \"" + (String)resArr.get(1) + "\", ");
out.print("\"PHONE\": \"" + (String)resArr.get(2) + "\", ");
out.print("\"GRADE\": \"" + (String)resArr.get(3) + "\", ");
out.print("\"WRITE_TIME\": \"" + (String)resArr.get(4) + "\", ");
out.print("} ");
for ( int i=5; i<resArr.size(); i += 5 ) {
out.print(", ");
out.print("{");
out.print("\"ID\": \"" + (String)resArr.get(i) + "\", ");
out.print("\"NAME\": \"" + (String)resArr.get(i+1) + "\", ");
out.print("\"PHONE\": \"" + (String)resArr.get(i+2) + "\", ");
out.print("\"GRADE\": \"" + (String)resArr.get(i+3) + "\", ");
out.print("\"WRITE_TIME\": \"" + (String)resArr.get(i+4) + "\", ");
out.print("} ");
}
out.println("]");
out.println("}");
}
%>
'IT > Jsp' 카테고리의 다른 글
jsp - form 태그에서 서버로 값 넘길 때 한글깨짐 처리 (get, post 방식) (0) | 2018.09.03 |
---|---|
jsp - form 입력값 전송, request 객체, request.getParameter("파라미터이름") (0) | 2018.09.03 |
jsp - 스크립틀릿(scriptlet), 선언문, out.println, (0) | 2018.09.02 |
jsp - include, jspf (0) | 2018.09.01 |
Jsp 데이터베이스 연동 (0) | 2018.07.22 |