
1,、配置context.xml文件
<Context>
<Resource name="jdbc/news"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="jbit"
password="dbqn"'
driverClassName="Oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:daataName"
</Context>
你可以换成SQL的
2、配置web.xml文件
<resource-ref>
<res-auth>Container</res-auth>
<res-type>javax.sql.DataSource</res-type>
<res-ref-name>jdbc/news</res-ref-name>
</resource-ref>
3、添加数据库驱动
4、在javaBean中编写代码,使用lookup()方法获得数据源对象
public class BaseDAO {
private static Connection conn = null
public static Connection getConnection() {
try {
Context context = new InitialContext()
DataSource ds = (DataSource) context
.lookup("java:comp/env/jdbc/news")
conn = ds.getConnection()
} catch (Exception e) {
e.printStackTrace()
}
return conn
}
}
注:lookup("java:comp/env/jdbc/news")
中的参数"java:comp/env/"+数据源名称的形式
如果你要的话我正好有这Struts2的项目我可以给你看看
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)