Struts1.3でJNDI接続 PostgreSQL
【環境】
Eclipse3.4.1
JDK1.6
TOMCAT5.5
Struts1.3
PostgreSQL8.2.1
環境がおかしくなったのでプロジェクト作り直し。
1.動的Webプロジェクトを作成
2.struts-blank-1.3.9.warのJarファイルをWEB-INF\libに展開する。
antlr-2.7.2.jar
commons-beanutils-1.7.0.jar
commons-chain-1.1.jar
commons-digester-1.8.jar
commons-logging-1.0.4.jar
commons-validator-1.3.1.jar
oro-2.0.8.jar
struts-core-1.3.9.jar
struts-taglib-1.3.9.jar
struts-tiles-1.3.9.jar
3.DBCP(コネクションプール)を使う為、下記の3つのコンポーネントを取得。
Jakarta-Commons DBCP
Jakarta-Commons Collections
Jakarta-Commons Pool
%TOMCAT_HOME%\common\libに配置する。
4.設定ファイル等々
・%TOMCAT_HOME%\conf\context.xml
<Context>の後に↓の記述があるはずなのに
==============================================
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>META-INF/context.xml</WatchedResource>
==============================================
※2行目の記述がなかったので追記
・WebContent/META-INFにcontext.xmlを新規作成。
==============================================
<Context path="/testap" docBase="testap"
debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_testap_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/postgres" auth="Container" //Resource nameでアクセスされる。
type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/sbt"
username="postgres" password="admin" maxActive="20" maxIdle="10" maxWait="-1"/>
</Context>
==============================================
上記設定内容
Path:testap
postgreSQLのポート:5432
データベース名:sbt
ユーザー:postgres
パスワード:admin
・web.xmlの編集 //welcomeファイルの次
==============================================
<resource-ref>
<description>postgreSQL Datasource example</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
==============================================
5.実装(サンプルソース)
==============================================
import javax.servlet.ServletException;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class DbUtil {
private static DataSource ds; //DataSourceはスレッドセーフ
/**
* DataSourceを必要に応じてJNDI経由で取得する。
*/
public static DataSource getDataSource() throws ServletException{
if (ds == null){
//JNDI経由でDataSourceを取得する
try {
InitialContext context = new InitialContext();
ds = (DataSource)context.lookup("java:comp/env/jdbc/postgres");
} catch (NamingException e) {
throw new ServletException(e);
}
}
return ds;
}
}
==============================================
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
public class loginAction extends Action {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
DataSource ds = DbUtil.getDataSource();
con = ds.getConnection();
DatabaseMetaData md = con.getMetaData();
ps = con.prepareStatement("select xxxxxx");
rs = ps.executeQuery();
…
}
==============================================
04 | 2025/05 | 06 |
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |