Java技術情報


/*
 * レベル4 use DB driver (update)
 */

import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.*;

// DB通信関係のオペレートに必要なクラス群を追加。
import java.sql.*;


public class Level040 extends HttpServlet {

    /*** init サービス ***/
    public void init(ServletConfig conf) throws ServletException
    {
	super.init(conf);
    }


    /*** doGet サービス ***/
    public void doGet (HttpServletRequest req, HttpServletResponse res)
	    throws ServletException, IOException
    {

	// 変数宣言
	String s_param = "";
	String s_result[] = null;// 配列型文字列の宣言。
	String s_sql = "";

	Connection connection = null;
	PreparedStatement  statement  = null;
	int i_count = 0;
	int i_result = -1;


	try{

	    res.setContentType("text/html; charset=MS932");

	    // update文なども JDBC通信階層は同じなので、"開けたら閉める"の鉄則は同じ。

	    // JDBC Driver Load
	    Class.forName("oracle.jdbc.driver.OracleDriver");

	    // set parameter
	    Properties props = new Properties();     // ユーザー名/パスワードなどを入れるオブジェクト
	    props.put("user","www2");                // ユーザー名を入れる。
	    props.put("password","www2");            // パスワードを入れる。
		String s_db_url = "jdbc:oracle:thin:@db.gospel4.ne.jp:1521:ORCL";
	    connection = DriverManager.getConnection(s_db_url,props);

	    if(connection == null){
		throw new SQLException("connection error.");
	    }

	    // JDBC のデフォルト では、 1SQLごとに自動的にコミットしてしまうので、はずす。
	    if( connection.getAutoCommit()) { connection.setAutoCommit(false);}

	    // make sql
	    s_sql  = "UPDATE TABLE_A \n";
	    s_sql += "SET COL_B='1' \n";
	    s_sql += "WHERE COL_A='ABC' \n";
	    //System.out.println("s_sql = [" + s_sql + "]");    // SQLのデバッグ文。平時はコメントアウト

	    // SQLをセットし、コネクションからステートメントを作成
	    statement = connection.prepareStatement(s_sql);

	    // SQLを投げると、処理した件数が返される。
	    i_result = statement.executeUpdate();

	    if(i_result >= 0) {
		connection.commit(); //成功したらコミット
	    }

	    connection.rollback(); //なにがあろうと最終的にはロールバック。
	    //中途半端なものはすべてロールバックされる。
	    //成功コミットしたあとにロールバックしても成功コミットは有効

	    statement.close();   //
	    connection.close();  // 続けてSQLをなげる場合などは、コネクションは閉じない。

	    PrintWriter pw = res.getWriter();

	    pw.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
	    pw.println();
	    pw.println("<head>");
	    pw.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\">");
	    pw.println("<!-- The Servlet expression tags interpolate script variables into the HTML -->");
	    pw.println("<title>ハロー!</title>");
	    pw.println("</head>");
	    pw.println("<body bgcolor=white>");
	    pw.println();

	    pw.println("<h1>ハロー!</h1>");

	    //////////////
	    pw.println(s_sql + " を実行し、<BR>");
	    pw.println(String.valueOf(i_result) + " 行処理しました。<BR>");
	    //String.valueOf(int)は、int型からString型への変換
	    //////////////

	    pw.println();
	    pw.println("</body>");

	    pw.flush();
	    pw.close();

	}
	catch( ClassNotFoundException eCNFE )
	{
	    System.out.println("ClassNotFoundException error : " + eCNFE.toString());
	}
	catch( SQLException eSQLE )
	{
	    System.out.println("SQLException error : " + eSQLE.toString() );
	}
	catch( Exception e){
	    System.out.println("Exception error : " + e.toString() );
	}

	//ステートメントとコネクションは閉じ忘れのないようにする。
	try{
	    if(statement != null){
		statement.close();
	    }
	    if(connection != null){
		connection.rollback(); //なにがあろうと最終的にはロールバック。
		connection.close();
	    }
	}catch(Exception e){
	    //no action.
	    //保険の保険はかけないものとする。
	}

    }


    /*** doPost サービス ***/
    public void doPost (HttpServletRequest req, HttpServletResponse res)
	    throws ServletException, IOException
    {
	doGet(req,res);
    }

}



ブラウザはIE6.0sp2以上、解像度1024*768以上を推奨します。
戻る
gospel4 トップへ