Struts2.0+tomcat使用数据源
配置tomcat 数据源(conf\context.xml)
<resource name="jdbc/mydata" autn="done"
?? type="javax.sql.DataSource"? maxActive="100" maxIdle="30"
?? maxwait="10000"? username="root" password="root"
?? driveClassName="com.mysql.jdbc.Driver"
?? url="jdbc:mysql://127.0.0.1:3306/mydata"
/>
?
在S2中引用数据源配置(web.xml)
<resource-ref>
<description>?DB connection?</desciption>
<res-ref-name>jdbc/mydata</res-ref=name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>done</res-auth>
</resource-ref>
?
?
获取connection
public static Connection getDSConnection(){try{ InitialContext ctx = new InitialContext(); DataSource ds =(DataSource)ctx.loolup("java:comp/env/jdbc/mydata");conn = ds.getConnection();return conn;}catch (Exception e){ e.printStack(); System.err.println("DB ERROR!") return conn; }}
?
?
?