言語
開発環境
Webアプリ/ミドル
データベース
OS/ネットワーク
ITスキル
海外サイト翻訳
書籍検索
用語検索
非常に簡単なDBアクセスするサンプルを紹介しています。
以下の環境を前提に記載。
<html>
<head>
<title>Hello</title>
</head>
<body>
Hello <span id="nameStr">name</span>!!
</body>
</html>
package hello.web;
import hello.dao.SampleDao;
import hello.entity.Sample;
public class HelloPage {
public String nameStr;
private SampleDao sampleDao;
public SampleDao getSampleDao() {
return this.sampleDao;
}
public void setSampleDao(SampleDao sampleDao) {
this.sampleDao = sampleDao;
}
public Class<?> initialize() {
Sample sample = sampleDao.selectByMid("00001");
nameStr = sample.getName();
return null;
}
public Class<?> prerender() {
return null;
}
}
package hello.dao;
import hello.entity.Sample;
import org.seasar.dao.annotation.tiger.Arguments;
import org.seasar.dao.annotation.tiger.S2Dao;
@S2Dao(bean=Sample.class)
public interface SampleDao {
@Arguments({"mid"})
public Sample selectByMid(String mid);
}
package hello.entity;
import org.seasar.dao.annotation.tiger.Bean;
import org.seasar.dao.annotation.tiger.Id;
import org.seasar.dao.annotation.tiger.IdType;
@Bean(table="sample")
public class Sample {
@Id(IdType.IDENTITY)
private String mid;
private String name;
public String getMid() {
return mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container 2.4//EN"
"http://www.seasar.org/dtd/components24.dtd">
<components namespace="jdbc">
<include path="jta.dicon"/>
<include path="jdbc-extension.dicon"/>
<component
class="org.seasar.extension.jdbc.impl.BasicResultSetFactory"/>
<component
class="org.seasar.extension.jdbc.impl.ConfigurableStatementFactory">
<arg>
<component
class="org.seasar.extension.jdbc.impl.BasicStatementFactory"/>
</arg>
<property name="fetchSize">100</property>
</component>
<component name="xaDataSource"
class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
<property name="driverClassName">"com.mysql.jdbc.Driver"</property>
<property name="URL">"jdbc:mysql://localhost:3306/test"</property>
<property name="user">"ユーザ名"</property>
<property name="password">"パスワード"</property>
</component>
<component name="connectionPool"
class="org.seasar.extension.dbcp.impl.ConnectionPoolImpl">
<property name="timeout">600</property>
<property name="maxPoolSize">10</property>
<property name="allowLocalTx">true</property>
<destroyMethod name="close"/>
</component>
<component name="dataSource"
class="org.seasar.extension.dbcp.impl.DataSourceImpl" />
</components>
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<!--: 省略-->
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
<Connector port="8009"
protocol="AJP/1.3"
redirectPort="8443"
URIEncoding="UTF-8" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="false"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="/test"
docBase="<tomcatホームディレクトリ>/webapps/test"
debug="0" reloadable="true">
</Context>
</Host>
</Engine>
</Service>
</Server>
自動生成された<Eclipseプロジェクト配下>src/main/webapp配下を
□tomcatホームディレクトリ
├─conf
| server.xml
└─webapps
└─test
│ index.jsp
├─view
│ │ hello.html
│ └─error
│ error.html
└─WEB-INF
│ faces-config.xml
│ web.xml
├─classes
│ │ app.dicon
│ │ appMessages.properties
│ │ appMessages_ja.properties
│ │ app_aop.dicon
│ │ convention.dicon
│ │ creator.dicon
│ │ customizer.dicon
│ │ env.txt
│ │ env_ut.txt
│ │ hello.html
│ │ jdbc.dicon
│ │ log4j.properties
│ │ s2container.dicon
│ │ teedaCustomize.dicon
│ │ teedaErrorPage.dicon
│ ├─error
│ │ error.html
│ └─hello
│ ├─converter
│ ├─dao
│ │ SampleDao.class
│ ├─dto
│ ├─dxo
│ ├─entity
│ │ Sample.class
│ ├─helper
│ ├─logic
│ ├─service
│ ├─validator
│ └─web
│ │ HelloPage.class
│ └─error
│ ErrorPage.class
└─lib
│ aopalliance-1.0.jar
│ commons-collections-3.1.jar
│ commons-el-1.0.jar
│ commons-fileupload-1.2.jar
│ commons-io-1.3.2.jar
│ commons-logging-1.1.jar
│ geronimo-jta_1.1_spec-1.0.jar
│ h2-1.0.69.jar
│ javassist-3.4.ga.jar
│ jstl-1.1.2.jar
│ log4j-1.2.13.jar
│ mysql-connector-java-5.0.8-bin.jar
│ ognl-2.6.9-patch-20090427.jar
│ poi-3.0-FINAL.jar
│ s2-dao-1.0.51.jar
│ s2-dao-tiger-1.0.51.jar
│ s2-extension-2.4.43.jar
│ s2-framework-2.4.43.jar
│ s2-tiger-2.4.43.jar
│ teeda-ajax-1.0.13-sp10.jar
│ teeda-core-1.0.13-sp10.jar
│ teeda-extension-1.0.13-sp10.jar
│ teeda-tiger-1.0.13-sp10.jar
│ xercesImpl-2.6.2.jar
│ xmlParserAPIs-2.6.2.jar
└─sources
commons-collections-3.1-sources.jar
commons-el-1.0-sources.jar
commons-fileupload-1.2-sources.jar
commons-io-1.3.2-sources.jar
commons-logging-1.1-sources.jar
log4j-1.2.13-sources.jar
s2-dao-1.0.51-sources.jar
s2-dao-tiger-1.0.51-sources.jar
s2-extension-2.4.43-sources.jar
s2-framework-2.4.43-sources.jar
s2-tiger-2.4.43-sources.jar
teeda-ajax-1.0.13-sp10-sources.jar
teeda-core-1.0.13-sp10-sources.jar
teeda-extension-1.0.13-sp10-sources.jar
teeda-tiger-1.0.13-sp10-sources.jar
tomcatを起動し、ブラウザより「http://<tomcatサーバのホスト>:8080/test/view/hello.html」へアクセスすると 「Hello test!! 」と表示される。
Loarding…
グループサイト zealseeds zealseedsラーニング zealseedsブックス 名か字 名科辞典 幸福の木の育て方
Copyright (C) 2007-2011 zealseeds. All Rights Reserved.お問合せ