日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

--- struts数据源配置(详解)---

發布時間:2023/12/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 --- struts数据源配置(详解)--- 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

來源:http://www.blogjava.net/biiau/archive/2008/04/16/193513.html

?

配置Struts數據源有很多種方法,我們這里講解最實用的一種。

配置數據源所需要的JAR包:commons-dbcp-1.2.2.jar、commons-pool-1.4.jar、struts-legacy.jar
下載地址:
http://www.blogjava.net/Files/biiau/struts-dataSource.rar

1.配置struts-config.xml文件,加入數據源

<!-- ============ Data Source Start ================== -->
?<data-sources>
??<data-source key="org.apache.struts.action.DATA_SOURCE"
???type="org.apache.commons.dbcp.BasicDataSource">
???<set-property property="autoCommit" value="true" />
???<set-property property="description"??value="SQL2000 Data Source" />
???<set-property property="driverClassName"
????value="net.sourceforge.jtds.jdbc.Driver" />
???<set-property property="maxCount" value="10" />
???<set-property property="minCount" value="2" />
???<set-property property="username" value="username" />
???<set-property property="password" value="password" />
???<set-property property="url"
????value="jdbc:jtds:sqlserver://localhost:1433/databaseName" />
??</data-source>
?</data-sources>
<!-- ============ Data Source End ================== -->

2.添加STRUTS插件;添加插件的目的是:任何文件中都可以用到此數據源


首先創建插件,它繼承自org.apache.struts.action.PlugIn?????

?

package?com.xxx.db;

import?java.sql.Connection;
import?java.sql.PreparedStatement;
import?java.sql.ResultSet;
import?java.sql.SQLException;
import?java.sql.Statement;

import?javax.sql.DataSource;

import?org.apache.struts.action.ActionServlet;
import?org.apache.struts.action.PlugIn;
import?org.apache.struts.config.ModuleConfig;

public?class?DBConn?implements?PlugIn?{

?
private?static?DataSource?dataSource?=?null;
?
private?Connection?conn?=?null;
?
private?PreparedStatement?preStmt?=?null;
?
private?Statement?stmt?=?null;

?
//?得到數據源
?public?void?init(ActionServlet?servlet,?ModuleConfig?config)?{
??dataSource?
=?(DataSource)?servlet.getServletContext().getAttribute(
????
"org.apache.struts.action.DATA_SOURCE");
?}


?
public?DBConn()?throws?SQLException?{
??
if?(dataSource?!=?null)?{
???conn?
=?dataSource.getConnection();
??}

?}


?
public?ResultSet?executeQuery(String?sql)?{
??ResultSet?rs?
=?null;

??
try?{
???
if?(stmt?==?null)?{
????stmt?
=?conn.createStatement();
???}


???rs?
=?stmt.executeQuery(sql);
??}
?catch?(SQLException?e)?{
???e.printStackTrace();
??}


??
return?rs;
?}


?
public?void?executeUpdate(String?sql)?throws?SQLException?{
??
if?(stmt?==?null)?{
???stmt?
=?conn.createStatement();
??}


??stmt.executeUpdate(sql);
?}


?
public?Connection?getConnection()?{
??
return?conn;
?}


?
public?void?prepareStatement(String?sqlStr)?throws?SQLException?{
??preStmt?
=?conn.prepareStatement(sqlStr);
?}


?
public?void?setString(int?index,?String?value)?throws?SQLException?{
??preStmt.setString(index,?value);
?}


?
public?void?setInt(int?index,?int?value)?throws?SQLException?{
??preStmt.setInt(index,?value);
?}


?
public?void?setBoolean(int?index,?boolean?value)?throws?SQLException?{
??preStmt.setBoolean(index,?value);
?}


?
public?void?setLong(int?index,?long?value)?throws?SQLException?{
??preStmt.setLong(index,?value);
?}


?
public?void?setFloat(int?index,?float?value)?throws?SQLException?{
??preStmt.setFloat(index,?value);
?}


?
public?void?setBytes(int?index,?byte[]?value)?throws?SQLException?{
??preStmt.setBytes(index,?value);
?}


?
public?void?clearPreStmt()?throws?SQLException?{
??preStmt.clearParameters();
??preStmt?
=?null;
?}


?
public?ResultSet?executeQuery()?throws?SQLException?{
??
if?(preStmt?!=?null)?{
???
return?preStmt.executeQuery();
??}
?else?{
???
return?null;
??}

?}


?
public?void?executeUpdate()?throws?SQLException?{
??
if?(preStmt?!=?null)?{
???preStmt.executeUpdate();
??}

?}


?
public?void?close()?{
??
try?{
???
if?(stmt?!=?null)?{
????stmt.close();
????stmt?
=?null;
???}


???
if?(preStmt?!=?null)?{
????preStmt.close();
????preStmt?
=?null;
???}


???
if?(conn?!=?null)?{
????conn.close();
????conn?
=?null;
????System.out.println(
"****?a?connection?is?closed?****");
???}

??}
?catch?(Exception?e)?{
???System.err.println(e.getMessage());
??}

?}


?
public?void?destroy()?{
?}

}


然后配置struts-config.xml文件:
???????? <plug-in className="com.xxx.db.DBConn"></plug-in>

3.到此,數據源完成。用法示例:
??????? Connection conn =? new DBCon().getConnection();

總結

以上是生活随笔為你收集整理的--- struts数据源配置(详解)---的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。