JDBC_Sample.java

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

public
class JDBC_Sample extends HttpServlet {

    public void doGet (HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
    {
        res.setContentType( "text/html; charset=euc-jp" );

	ServletOutputStream out = res.getOutputStream();

	out.println("<html>");
	out.println("<head><title>JRun の JDBC サンプル</title></head>");
	out.println("<body>");
	out.println("<h1>JRun の JDBC サンプル</h1>");
	
	try {
		try {
			Class.forName("jdbc.idbDriver");
		}
		catch (Exception e)
		{
			out.println( e.getMessage() + "\n 例外のないクラスです。");
		}
	
		Connection conn1 = DriverManager.getConnection("jdbc:idb:./services/jws/htdocs/cfanywhere/db/sample.prp","","");
		
		Statement stmt1 = conn1.createStatement();
		ResultSet rs1 = stmt1.executeQuery("SELECT * FROM employee_table");
		
		//ResultSet rs1 = stmt1.executeQuery("INSERT INTO employee_table VALUES(1, 'Dan', 'Smith')");
		while (rs1.next())
		{
			out.print( rs1.getInt( "ID" ) + ", ");
			out.print( rs1.getString( "FIRSTNAME" ) + ", ");
			out.println( rs1.getString( "姓" ) + "<BR>" );
		}
		
		DatabaseMetaData dbmd1 = conn1.getMetaData();
		out.println( "<P>\n接続先: " + dbmd1.getDatabaseProductName());
		out.println( "<P>データベースのバージョン番号: " + dbmd1.getDatabaseProductVersion());
		out.println( "<P>現在のログイン名: " + dbmd1.getUserName() + "<P>");
		
		ResultSetMetaData rsmd1 = rs1.getMetaData();
		
		for( int i=1; i<=rsmd1.getColumnCount(); i++)
		{
			out.println( "欄 " + i + " 名前:   " + rsmd1.getColumnName(i) + "<BR>" );
			out.println( "欄 " + i + " サイズ:   " + rsmd1.getColumnDisplaySize(i) + "<BR>" );
			out.println( "欄 " + i + " SQL タイプ:   " + rsmd1.getColumnType(i) + "<P>" );
		}
		
		stmt1.close();
		conn1.close();
	}
	catch( SQLException exception )
	{
		out.println( "\n SQL 例外 -- " + exception.getMessage() + "\n" );
	}

	out.println("</body></html>");
    }

    public String getServletInfo() {
	return "Create a page that says <i>Hello World</i> and send it back";
    }
}