- 
		あらかじめMySQLにデータベースとテーブルを作成しておく(仮データとして、userIDを持つテーブルを定義する)
- 
		Eclipseで「動的Webプロジェクト」を作成
- 
		JDBC(mysql-connector...)をWebContent/WEB-INF/libに入れる
- 
		WebContent/META-INFにcontent.xmlを作成(ソース1)
- 
		WebContent内にindex.jspを作成、編集(ソース2)
- 
		実行してTomcatを起動、確認
以下ソース
	- 
		(1) content.xml : データベース名、ユーザ名、パスワードは適宜変更
	<?xml version="1.0" encoding="UTF-8"?>
	<Context>
	<Resource name="jdbc/JNDI" auth="Container" type="javax.sql.DataSource"
	driverClassName="com.mysql.jdbc.Driver"
	url="jdbc:mysql://localhost:3306/"データーベース名"?autoReconnect=true"
	username="ユーザ名" password="パスワード" />
	</Context>
	- 
		(2)index.jsp : 同様にデータベース名、ユーザ名、パスワード、テーブル名は適宜変更
	<%@ page language="java" contentType="text/html; charset=windows-31j"
	    pageEncoding="windows-31j"
	    import="java.sql.*"%>
	<!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=windows-31j">
	<title>Insert title here</title>
	</head>
	<body>
	<%
	try {
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	 
	Connection con = DriverManager.getConnection(
	"jdbc:mysql://localhost/データベース名", "ユーザ名", "パスワード");
	 
	Statement stmt = con.createStatement();
	ResultSet rs = stmt.executeQuery("SELECT userID from テーブル名");
	while (rs.next()) {
	out.println("-------------------------------<br>");
	out.println("userID     : " + rs.getString("userID") + "<br>");
	}
	rs.close();
	stmt.close();
	con.close();
	}catch(Exception e){
	 
	}
	%>
	</body>
	</html>
PR