1:下载并安装Tomcat5.5.9 http://jakarta.apache.org/tomcat/
2:下载struts1.2.7 http://struts.apache.org/download.cgi
3:下载MySql的JDBC驱动 mysql-connector-java-3.0.16-ga-bin.jar (google一下)
4:创建MySql数据库名字: strutsdatabase
5:创建MySql数据库表:
CREATE TABLE `test` (
`username` varchar(20) NOT NULL default ''
) TYPE=MyISAM;
/*Data for the table `test` */
insert into `test` values ('rajesh'),('George'),('Vikas'),('Prakash'),('Mahesh');
6:在struts-config.xml的 data-source中的元素应该是这样:
<data-sources>
<data-source type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="com.mysql.jdbc.Driver" />
<set-property
property="url"
value="jdbc:mysql://localhost:3306/strutsdatabase?autoReconnect=true" />
<set-property
property="username"
value="root" />
<set-property
property="password"
value="" />
<set-property
property="maxActive"
value="10" />
<set-property
property="maxWait"
value="5000" />
<set-property
property="defaultAutoCommit"
value="false" />
<set-property
property="defaultReadOnly"
value="false" />
<set-property
property="validationQuery"
value="SELECT COUNT(*) FROM test" />
</data-source>
</data-sources>
7:创建action class来测试这个DataSourcepackage test;
struts-config.xml里添加action mapping元素.
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.sql.*;
public class TestDataSource extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
javax.sql.DataSource dataSource;
java.sql.Connection myConnection=null;
try {
dataSource = getDataSource(request);
myConnection = dataSource.getConnection();
Statement stmt=myConnection.createStatement();
ResultSet rst=stmt.executeQuery("select username from test");
System.out.println("******************************************");
System.out.println("********Out Put from TestDataSource ******");
while(rst.next()){
System.out.println("User Name is: " + rst.getString("username"));
}
System.out.println("******************************************");
rst.close();
stmt.close();
// do what you wish with myConnection
} catch (SQLException sqle) {
getServlet().log("Connection.process", sqle);
} finally {
//enclose this in a finally block to make
//sure the connection is closed
try {
myConnection.close();
} catch (SQLException e) {
getServlet().log("Connection.close", e);
}
}
return mapping.findForward("success");
}
}
8:在
<action
path="/DataSource"
type="test.TestDataSource">
<forward name="success" path="/success.jsp"/>
</action>
9:创建一个success.jsp在你的strutsdatabase的根目录下
(随便写点东西像是:HELLO WORLD!:))
10:测试你的成果:http://localhost:8080/strutsdatabase/DataSource.do
11:看看你的浏览器是不是出现了:HELLO WORLD!了.呵呵!
MyEclipse 5.0 M2 注册码(转致www.1cn.biz)
此版本序列号是(www.1cn.biz)经过n久反编译、穷举法等等计算出来的,一直算到myeclipse的9.99版本,序列号过期时间为2099年12月31日,估计用上5-10年都有可能(就看myeclipse的更新速度了),
Subscriber: www.1cn.biz
Subscriber Code: jLR8ZC-444-55-4467865481680090