struts-config.xml添加
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- ==================== form-beans Configuration ========================== -->
<form-beans>
<form-bean name="MovieForm" type="com.coraise.web.stb.MovieForm"/>
</form-beans>
<!-- ==================== action-mappings Configuration ========================== -->
<action-mappings>
<action path="/movie" type="com.coraise.web.stb.MovieAction" parameter="action" name="MovieForm" scope="request">
<forward name="listMovieInfo" path="/stb/movie_list.jsp"/>
<forward name="listMusicInfo" path="/stb/music_list.jsp"/>
<forward name="listEnMovieInfo" path="/stb/en_movie_list.jsp"/>
<forward name="listEnMusicInfo" path="/stb/en_music_list.jsp"/>
<forward name="viewMediaInfo" path="/stb/mediaInfo.jsp"/>
<forward name="viewEnMediaInfo" path="/stb/en_mediaInfo.jsp"/>
<forward name="confirm2" path="/stb/confirm2.jsp"/>
<forward name="en_confirm2" path="/stb/en_confirm2.jsp"/>
</action>
</action-mappings>
<!-- ==================== Data Source Configuration ========================== -->
<data-sources>
<data-source key="DATA_SOURCE" type="org.apache.struts.util.GenericDataSource">
<set-property property="user" value="root" />
<set-property property="password" value="1234" />
<set-property property="maxCount" value="20" />
<set-property property="minCount" value="2" />
<set-property property="driverClass" value="com.mysql.jdbc.Driver" />
<set-property property="description" value="" />
<set-property property="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=GBK" />
<set-property property="readOnly" value="false" />
<set-property property="autoCommit" value="false" />
<set-property property="loginTimeout" value="200" />
</data-source>
</data-sources>
</struts-config>
在你的Action类的excute方法中即可执行以下连接数据库代码
import javax.servlet.ServletContext;
import javax.sql.*;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String tmpName = "currentName";
ServletContext context = this.servlet.getServletContext();
DataSource dataSource = (DataSource)context.getAttribute("DATA_SOURCE");
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from user where id = 6");
if( rs.next() ){
tmpName = rs.getString("name");
System.out.print(tmpName);
}
}catch (SQLException e) {
System.err.println(e.getMessage());
}