http://blog.csdn.net/gotoidea/archive/2004/07/29/55048.aspx
解决方案 »
- [求助]struts标签无法获取遍历的值
- 在jsp 页面用el表达式取出的值怎样应用去掉空格函数去空格
- 有问有答
- 用浏览器打开jsp文件时为什么开始报错但刷新几次又能运行?
- STRUTS <html:text>标签如何获得request范围内对象的属性显示在文本框内?
- 诚恳麻烦请热心人推荐下EJB开发的IDE!很是感谢。
- linux下安装jdk的问题!
- 应用程序如何通过JNDI的方式连接数据池,如何连接EJB?谢谢必给分!
- 求大神帮忙 一个简单的struts2项目 tomcat启动报错如下 不知道怎么解决 (在线等)
- NetBeans 运行项目 错误:程序包javax.persistence不存在。我已添加相应的jar还会错
- 求助Struts问题:明明定义的属性却报错找不到--在线等待
- 关于jbuilder的jdbc的oracle驱动的问题
终于实现Struts的数据源连接了
今天用了3个小时,看了官方文档终于实现了。
过程根据提示错误,发现少了3个.jar文件,都是struts包没有自动包括的,
commons-dbcp-1.2.1.jar
struts-legacy.jar
commons-pool-1.2.jar
当然了还有mysql.jar这个文件都有被包含lib\下
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource"><!--这里可以有属性key="A"--->
<set-property
property="driverClassName"
value="org.gjt.mm.mysql.Driver" />
<set-property
property="url"
value="jdbc:mysql://localhost/sony" />
<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 member" />
</data-source> </data-sources>=========在Action的 execute方法中
javax.sql.DataSource ds = null;
java.sql.Connection conn = null;
java.sql.Statement stmt = null;
ds = getDataSource(request);<!--或者ds = getDataSoruce(request,"A")-->
conn = ds.getConnection();WEB服务器tomcat5.0发表于 2004年07月29日 9:21 AM
评论
# 回复:终于实现Struts的数据源连接了 2004-08-06 1:41 PM Ricky
commons-dbcp-1.2.1.jar
struts-legacy.jar
commons-pool-1.2.jar
这几个文件在哪有得下载?谢谢 # 回复:终于实现Struts的数据源连接了 2004-08-25 10:53 PM moqiong
分别到以下3个地址去找 http://apache.linuxforum.net/dist/jakarta/commons/dbcp/binaries/ http://apache.linuxforum.net/dist/jakarta/struts/struts-legacy/ http://apache.linuxforum.net/dist/jakarta/commons/pool/binaries/
/**
* Called by the controller when the user attempts to register
* @param mapping ActionMapping.
* @param form ActionForm.
* @param request HttpServletRequest.
* @param response HttpServletResponse.
* @return ActionForward
*/
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response )
throws IOException,ServletException {
String target = "success";
ActionErrors errors = new ActionErrors();
String name = ((CustomerRegisterForm)form).getCustomerName().trim();
/**
boolean isexisted = isExistedCustomer( name );
if( isexisted ) {
target = "failure";
errors.add( ActionErrors.GLOBAL_ERROR,
new ActionError("error.customer.existed",name) );
}
else { */
String customerId = storeCustomer( form );
if( customerId != null ) {
request.setAttribute( "id",customerId );//session scope
request.setAttribute( "name",name );//request scope
}
else {
target = "failure";
errors.add( ActionErrors.GLOBAL_ERROR,
new ActionError("error.customreregister.failure",name) );
}
//}
//Report any errors we have discovered back to the original form
if( !errors.isEmpty() ) {
saveErrors( request,errors );
}
return mapping.findForward( target );
}
/**
*
* @param name
* @return
*/
private boolean isExistedCustomer( String name ) {
boolean isexisted = false;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null; //Obtain data source
ServletContext context = servlet.getServletContext();
DataSource datasource = (DataSource)context.getAttribute( Globals.DATA_SOURCE_KEY );
try {
conn = datasource.getConnection();
stmt = conn.createStatement();
String query = "select customer_name from customer where customer_name='" + name + "'";
rs = stmt.executeQuery( query );
if( rs.next() ) {
isexisted = true;
}
}
catch( SQLException e ) {}
finally {
if( rs != null ) {
try{
rs.close();
}
catch ( SQLException ee )
{
System.err.println( ee.getMessage() );
}
rs = null;
}
if( stmt != null ) {
try{
stmt.close();
}
catch ( SQLException eee )
{
System.err.println( eee.getMessage() );
}
stmt = null;
}
if( conn != null ) {
try
{
conn.close();
}
catch ( SQLException e )
{
System.out.println( e.getMessage() );
}
conn = null;
}
}
return isexisted;
}
/**
* Store customer's information
* @param form
* @return boolean true if success or false
*/
private String storeCustomer( ActionForm form ) {
String id = null;
long base = 10000000;
String customerId = "10000000";
String customerName = ((CustomerRegisterForm)form).getCustomerName().trim();
String customerPass = ((CustomerRegisterForm)form).getCustomerPassword().trim();
String customerTelephone = ((CustomerRegisterForm)form).getCustomerTelephone().trim();
String recommendBuySalesmanId = ((CustomerRegisterForm)form).getRecommendBuySalesmanId().trim();
String receiverName = ((CustomerRegisterForm)form).getReceiverName().trim();
String receiverAddress = ((CustomerRegisterForm)form).getReceiverAddress().trim();
String receiverPhone = ((CustomerRegisterForm)form).getReceiverPhone().trim();
String receiverZipcode = ((CustomerRegisterForm)form).getReceiverZipcode().trim();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ServletContext context = servlet.getServletContext();
DataSource datasource = (DataSource)context.getAttribute( Globals.DATA_SOURCE_KEY );
//DataSource datasource = getDataSource( request ); try {
conn = datasource.getConnection();
stmt = conn.createStatement();
String query = "select count(*) from customer";
rs = stmt.executeQuery( query );
if( rs.next() ) {
int num = rs.getInt(1);
customerId = String.valueOf(num+base);
}
System.out.println( "Register Information as below:" );
System.out.println( "ID : " + customerId );
System.out.println( "Name : " + customerName );
System.out.println( "Telephone : " + customerTelephone );
System.out.println( "Recommender : " + recommendBuySalesmanId );
System.out.println( "receiverName : " + receiverName );
System.out.println( "receiverAddress : " + receiverAddress );
System.out.println( "receiverPhone : " + receiverPhone );
System.out.println( "receiverZipcode : " + receiverZipcode );
System.out.println( "Password : " + customerPass );
String update = "INSERT INTO customer VALUES( '" + customerId
+ "','" + customerName
+ "','" + customerTelephone
+ "','" + recommendBuySalesmanId
+ "','" + receiverName
+ "','" + receiverAddress
+ "','" + receiverPhone
+ "','" + receiverZipcode + "','',0.0,'"
+ customerPass + "' )" ;
int row = stmt.executeUpdate( update );
System.out.println( "Affecting Rows : " + row );
id = customerId;//return value
}
catch( SQLException e ) {}
finally {
if( rs != null ) {
try{
rs.close();
}
catch ( SQLException ee )
{
System.err.println( ee.getMessage() );
}
rs = null;
}
if( stmt != null ) {
try{
stmt.close();
}
catch ( SQLException eee )
{
System.err.println( eee.getMessage() );
}
stmt = null;
}
if( conn != null ) {
try
{
conn.close();
}
catch ( SQLException e )
{
System.out.println( e.getMessage() );
}
conn = null;
}
}
return id;
}
}