首先在你的context里面配置数据库
1这是一个访问Hypersonic的例子
<Resource name="jdbc/zlsWaterDS" auth="SERVLET" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/zlsWaterDS">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter> <name>maxActive</name><value>10</value></parameter>
<parameter><name>maxIdle</name><value>10</value></parameter>
<parameter><name>maxWait</name> <value>50000</value></parameter>
<parameter><name>username</name> <value>sa</value></parameter>
<parameter><name>password</name><value></value></parameter>
<parameter><name>driverClassName</name><value>org.hsqldb.jdbcDriver</value></parameter>
<parameter><name>url</name><value>jdbc:hsqldb:file:../resource/sfgl</value> </parameter>
</ResourceParams>
2 这是访问oracle的
<Resource name="jdbc/zlsWaterDS" auth="SERVLET" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/zlsWaterDS">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter><name>maxActive</name> <value>100</value></parameter>
<parameter><name>maxIdle</name><value>30</value></parameter>
<parameter><name>maxWait</name><value>10000</value></parameter>
<parameter><name>username</name><value>SYSTEM</value></parameter>
<parameter><name>password</name><value>MANAGER</value></parameter>
<parameter><name>driverClassName</name><value>oracle.jdbc.driver.OracleDriver</value></parameter>
<parameter><name>url</name><value>jdbc:oracle:thin:@ibmserver:1521:ORCL</value></parameter>
</ResourceParams>
这里的ORCL为SID,注意大小写,用大写
程序中使用数据源的方法
static public class ViewJqfkActionListener extends ExoActionListener {
public void execute(ExoActionEvent event) throws Exception {
String yhh = event.getParameter(OBJECTID);
UIListYhda uiList = (UIListYhda) event.getComponent();
UIJqfk uiJqfk = (UIJqfk) uiList.getSibling(UIJqfk.class);
List list = new ArrayList();
Jqfk j;
Connection conn =
uiList.service_.getDataSource("WaterDS").getConnection();
try {
Statement stmt =
conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql =
"select * from ZLS.JQFK where YHH = '" + yhh + "' ";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
j = new Jqfk();
j.setYhh(rs.getString("YHH"));
j.setYhm(rs.getString("YHM"));
j.setYsl(rs.getString("YSL"));
j.setZje(rs.getString("ZJE"));
j.setCbqk(rs.getString("CBQK"));
j.setJqrq(rs.getDate("JQRQ"));
j.setHs(rs.getString("HS"));
j.setHsrq(rs.getDate("HSRQ"));
list.add(j);
}
//rs.close();
//stmt.close();
//conn.close();
uiJqfk.setData(list);
uiList.setRenderedSibling(UIJqfk.class);
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.close();
}
}
}
注意,这里只要关闭conn就可以了,不要关闭rs和stmt
另外,显示的时候一定要用
static private Formater ft_ = Formater.getFormater(null) ;
w.write(ft_.format(y.getYhch()));
来防止数据库该字段有异常