http://blog.csdn.net/gotoidea/archive/2004/07/29/55048.aspx

解决方案 »

  1.   

    终于实现Struts的数据源连接
    终于实现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/ 
      

  2.   

    为什么下面的代码执行不成功呢?就是数据写不进数据库去(借花献佛了:) )public class CustomerRegisterAction extends Action {
    /**
     * 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;
    }
    }