Webアプリケーション開発・その5
Oracleドライバのライブラリパスを設定
C:\Users\H1009014\Downloads\sqldeveloper-18.1.0.095.1630-no-jre\sqldeveloper\jdbc\lib\ojdbc8.jar
実行環境のパスを追加
package com.example.part1;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
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 HelloWorld
*/
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloWorld() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// Oracle JDBC Driverのロード
Class.forName("oracle.jdbc.driver.OracleDriver");
// Oracle8iに接続
Connection conn =
DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe", "test_DB", "test");
//("jdbc:oracle:thin:@localhost:1521:ORCL", "test_DB", "test");
// ステートメントを作成
Statement stmt = conn.createStatement();
// 問合せの実行
ResultSet rset = stmt.executeQuery("select AGE, GENDER from FAMILY");
// 問合せ結果の表示
response.getWriter().write("AGE" + " " + "GENDER" + "<BR>");
response.getWriter().write("-----------------------------<BR>");
while ( rset.next() ) {
// 列番号による指定
//System.out.println(rset.getInt(1) + "\t" + rset.getString(2));
response.getWriter().write(rset.getInt(1) + " " + rset.getString(2) + "<BR>");
}
// 結果セットをクローズ
rset.close();
// ステートメントをクローズ
stmt.close();
// 接続をクローズ
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
//response.getWriter().write("Hello, World!");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}