1) 拷贝一个 mysql-connector-java-3.1.12-bin.jar 到 Tomcat/common/lib
2) Struts中配置数据源: Struts-config.xml中添加:data-source
<data-sources >
<data-source key="mysql">
<set-property property="password" value="root" />
<set-property property="minCount" value="5" />
<set-property property="maxCount" value="10" />
<set-property property="user" value="root" />
<set-property property="driverClass" value="org.gjt.mm.mysql.Driver" />
<set-property property="description" value="" />
<set-property property="url" value="jdbc:mysql://localhost:3306/manage" />
<set-property property="readOnly" value="false" />
<set-property property="autoCommit" value="false" />
<set-property property="loginTimeout" value="" />
</data-source>
</data-sources>
<data-sources >
<data-source key="mysql">
<set-property property="password" value="root" />
<set-property property="minCount" value="5" />
<set-property property="maxCount" value="10" />
<set-property property="user" value="root" />
<set-property property="driverClass" value="org.gjt.mm.mysql.Driver" />
<set-property property="description" value="" />
<set-property property="url" value="jdbc:mysql://localhost:3306/manage" />
<set-property property="readOnly" value="false" />
<set-property property="autoCommit" value="false" />
<set-property property="loginTimeout" value="" />
</data-source>
</data-sources>
3) 数据库管理类DBMange中设置一个静态变量:用来从plugin配置文件中获得信息
public static ServletContext context = null;
public static ServletContext context = null;
4) 建一个类MyPlugin 实现PlugIn接口,给 context 赋值
public void init(ActionServlet servlet, ModuleConfig config)throws ServletException {
DBManage.context = servlet.getServletContext();
}
public void init(ActionServlet servlet, ModuleConfig config)throws ServletException {
DBManage.context = servlet.getServletContext();
}
5) Struts中配置Plugin插件:Struts-config.xml最后添加:
<plug-in className=”com.wind.MyPlugin”/>
<plug-in className=”com.wind.MyPlugin”/>
6) 数据库管理类DBMange封装数据库获取数据库连接方法:获得一个数据库连接
public static Connection getConnection(){
Connection conn = null;
DataSource ds = (DataSource) context.getAttribute("mysql");
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Connection getConnection(){
Connection conn = null;
DataSource ds = (DataSource) context.getAttribute("mysql");
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
相关热门文章
给主人留下些什么吧!~~
评论热议