## serach.jsp ##
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
</head>
<body>
<form action="./PreStmt.do" method="post">
검색어 : <input type="text" name="val1" value="1"> (1: 날짜, 2: 시간)
<input type="submit" value="검색">
</form>
</body>
</html>
## StatementTestServlet.java ##
package console;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class StatementTestServlet
*/
@WebServlet("/PreStmt.do")
public class StatementTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
Connection conn = null;
Statement stmt1 = null;
PreparedStatement stmt2 = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
String sParam1 = request.getParameter("val1");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@10.0.0.**:1521:XE","*****","*****");
String sql1 = " SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS STR_DT1 FROM DUAL ";
stmt1 = conn.createStatement();
rs1 = stmt1.executeQuery(sql1);
if ( rs1.next() )
{
System.out.print(rs1.getString("STR_DT1"));
out.print("Statement 결과 : " + rs1.getString("STR_DT1") + "</br>");
}
String sql2 = " SELECT DECODE(?, '1', TO_CHAR(SYSDATE, 'YYYY-MM-DD'), '2', TO_CHAR(SYSDATE, 'HH24:MI:SS'), '') AS STR_DT2 FROM DUAL ";
stmt2 = conn.prepareStatement(sql2);
stmt2.setString(1, sParam1);
rs2 = stmt2.executeQuery();
if ( rs2.next() )
{
System.out.print(rs2.getString("STR_DT2"));
out.print("PreparedStatement 결과 : " + rs2.getString("STR_DT2") + "</br>");
}
}
catch(ClassNotFoundException e)
{
System.err.print("ClassNotFoundException");
System.err.print(e.getMessage());
}
catch(SQLException e)
{
System.err.print("SQLException");
System.err.print(e.getMessage());
}
finally
{
}
}
}
## serach.jsp 조회 화면 ##
## StatementTestServlet.java 결과 화면 ##
'프로그래밍(WEB) > JAVA' 카테고리의 다른 글
@WebServlet 사용법 [퍼옴] (0) | 2014.06.03 |
---|---|
Scope 변수 사용 예제 (0) | 2014.06.02 |