jdbc실습
- jdbc셈플
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JDBC Oracle Test</title>
</head>
<body>
<%
// 다음 3 라인의 정보를 변경해 주세요//////////////////////////////////////////
String dbURL = "jdbc:oracle:thin:@localhost:1521:mtdi";
String id = "scott";
String pwd = "tiger";
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(dbURL, id, pwd);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from emp");
while(rs.next()){
out.print(rs.getString(1)+" | ");
out.print(rs.getString(2)+" | ");
out.print(rs.getString(3)+" | ");
out.print(rs.getString(4)+"<br>");
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
=======================================
-
jsp
emp테이블보기
servlet
모델 객채 생성 데이터 가져옴 database.java
뷰에 대이터 전달 forward jsp내용 출력 emp.jsp
m - database.java
v - emp.jsp
c - controller.java
==================== main.jsp ====================
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="Controller">emp테이블 보기</a>
</body>
</html>
--------------------- Controller.java -------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Database;
/**
* Servlet implementation class for Servlet: Controller
*
*/
public class Controller extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("KSC5601");
Database db = new model.Database();
String str = db.getDB();
request.setAttribute("str",str);
try{
RequestDispatcher d =request.getRequestDispatcher("emp.jsp");
d.forward(request,response);
}catch(Exception e){}
}
}
------------------------- Database.java --------------
package model;
import java.sql.*;
public class Database {
Statement stmt;
Connection conn;
String str="";
public Database(){
try{
// 다음 3 라인의 정보를 변경해 주세요//////////////////////////////////////////
String dbURL = "jdbc:oracle:thin:@localhost:1521:ora92";
String id = "scott";
String pwd = "tiger";
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbURL, id, pwd);
stmt = conn.createStatement();
}catch(Exception e){}
}
public String getDB(){
try{
ResultSet rs = stmt.executeQuery("select * from emp");
while(rs.next()){
str+=rs.getString(1)+" | ";
str+=rs.getString(2)+" | ";
str+=rs.getString(3)+" | ";
str+=rs.getString(4)+"<br>";
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e){}
return str;
}
}
--------------------------- emp.jsp ---------------------
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
response.setContentType("text/html;charset=KSC5601");
request.setCharacterEncoding("KSC5601");
%>
<%=request.getAttribute("str").toString()%>
</body>
</html>
- jdbc셈플
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JDBC Oracle Test</title>
</head>
<body>
<%
// 다음 3 라인의 정보를 변경해 주세요//////////////////////////////////////////
String dbURL = "jdbc:oracle:thin:@localhost:1521:mtdi";
String id = "scott";
String pwd = "tiger";
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(dbURL, id, pwd);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from emp");
while(rs.next()){
out.print(rs.getString(1)+" | ");
out.print(rs.getString(2)+" | ");
out.print(rs.getString(3)+" | ");
out.print(rs.getString(4)+"<br>");
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
=======================================
-
jsp
emp테이블보기
servlet
모델 객채 생성 데이터 가져옴 database.java
뷰에 대이터 전달 forward jsp내용 출력 emp.jsp
m - database.java
v - emp.jsp
c - controller.java
==================== main.jsp ====================
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="Controller">emp테이블 보기</a>
</body>
</html>
--------------------- Controller.java -------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Database;
/**
* Servlet implementation class for Servlet: Controller
*
*/
public class Controller extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("KSC5601");
Database db = new model.Database();
String str = db.getDB();
request.setAttribute("str",str);
try{
RequestDispatcher d =request.getRequestDispatcher("emp.jsp");
d.forward(request,response);
}catch(Exception e){}
}
}
------------------------- Database.java --------------
package model;
import java.sql.*;
public class Database {
Statement stmt;
Connection conn;
String str="";
public Database(){
try{
// 다음 3 라인의 정보를 변경해 주세요//////////////////////////////////////////
String dbURL = "jdbc:oracle:thin:@localhost:1521:ora92";
String id = "scott";
String pwd = "tiger";
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbURL, id, pwd);
stmt = conn.createStatement();
}catch(Exception e){}
}
public String getDB(){
try{
ResultSet rs = stmt.executeQuery("select * from emp");
while(rs.next()){
str+=rs.getString(1)+" | ";
str+=rs.getString(2)+" | ";
str+=rs.getString(3)+" | ";
str+=rs.getString(4)+"<br>";
}
rs.close();
stmt.close();
conn.close();
}catch(Exception e){}
return str;
}
}
--------------------------- emp.jsp ---------------------
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
response.setContentType("text/html;charset=KSC5601");
request.setCharacterEncoding("KSC5601");
%>
<%=request.getAttribute("str").toString()%>
</body>
</html>

카테고리