PostgreSQL
環境
- Windows Vista
- jre 1.6
- Eclipse 3.4.2
- Tomcat 6.0
- PostgreSQL 8.4.701
- JDBC: postgresql-8.4-701.jdbc4.jar
手順
- PostgreSQLをダウンロード&インストール
- PostgreSQLにDBを作成
- 作成したDBにテーブルと列を作成
- 作成したテーブルにレコードを作成
- PostgreSQL用JDBCをダウンロード
- %CATALINA_HOME%\lib にJDBCのjarをセット
- WEB-INF/libでもOK
- CLASSPATHにJDBCのjarを登録
- Eclipseに新規にTomcatプロジェクトを生成
- Servletクラスを生成
- コーディング(下記参照)
- JOTMをダウンロード
- JOTMのlibの中身を %CATALINA_HOME%\lib にコピー
context.xmlの編集
- Tomcatプロジェクト生成時に作られたcontext.xmlを開く
- %CATALINA_HOME%\conf\Catarina\localhost配下
<Context path="/TestJTA2" reloadable="true" docBase="D:\eclipse_workspace\TestJTA2" workDir="D:\eclipse_workspace\TestJTA2 \work">
<Resource name="jdbc/postgres" auth="Container"
type="javax.sql.DataSource"
factory="org.objectweb.jotm.datasource.DataSourceFactory"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/testdb"
username="postgres"
password="hoge"
maxActive="20"
maxIdle="10"
maxWait="-1" />
<Resource auth="Container"
name="UserTransaction"
type="javax.transaction.UserTransaction"
factory="org.objectweb.jotm.UserTransactionFactory"
jotm.timeout="60" />
</Context>
Servletのソース
package test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.SystemException;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
/**
* Servlet implementation class TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse re sponse) throws ServletException, IOException {
System.out.println("### Start doGet ###");
UserTransaction ut = null;
//TransactionManager tm = null;
DataSource ds = null;
Connection conn = null;
PreparedStatement pst = null;
try{
Context ctx = new InitialContext();
ut = (UserTransaction)ctx.lookup("java:comp/env/UserTransaction");
// トランザクションの開始。この後の処理が、一連の不可分な処理になる。
ut.begin();
//tm = (TransactionManager)ctx.lookup(" java:comp/env/TransactionManager");
ds = (DataSource) ctx.lookup(" java:comp/env/jdbc/postgres");
conn = ds.getConnection();
// JDBC を使った SQL の実行
//Statement stmt = con.createStatement();
String sql = null;
// INSERT の実行(1)
sql = "insert into staff (id, name) values (11, 'Kato')";
//stmt.executeUpdate(sql);
pst = conn.prepareStatement(sql);
pst.executeUpdate();
// トランザクションの終了(コミット)。ここまでの一連の処理を確定させる。
ut.commit();
}catch(Exception e) {
System.out.print("[[JTA]] Test >> ");
e.printStackTrace();
try {
ut.rollback();
} catch (IllegalStateException e1) {
// TODO 自動生成された catch ブロック
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO 自動生成された catch ブロック
e1.printStackTrace();
} catch (SystemException e1) {
// TODO 自動生成された catch ブロック
e1.printStackTrace();
}
}
/* ContentType を設定*/
response.setContentType("text/html; charset=Shift_JIS");
/* 出力用 PrintWriter を取得*/
PrintWriter out = response.getWriter();
/* HTML 出力 */
out.println("<html>");
out.println("<head>");
out.println("<title>TestServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>TestServletが実行されました。</h2>");
out.println("<form method=\"POST\" action=\""
+ request.getContextPath() + "/SimpleServlet\">");
out.println("<input type=\"submit\" value =\"POST送信\"> ");
out.println("</form>");
out.println("<A href=\"" + request.getContextPath() + "/SimpleServlet\">");
out.println("GET送信");
out.println("</A>");
out.println("</body>");
out.println("</html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
web.xmlの編集
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestJTA2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
<resource-ref>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
最終更新:2010年05月09日 21:56