# keine verbindung vom struts framework zu mysql



## paidopoieo (16. Mrz 2006)

Hi, 
Ich kann leider keine verbindung zu meiner datenbank herstellen......
mein struts-conf.xml file sieht folgendermassen aus: 


```
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
 "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">

<struts-config>

	


      <data-sources>
        
        
        <data-source type="org.apache.commons.dbcp.BasicDataSource">
            <set-property
              property="description"
              value="My MySQL Database Connection" />
            <set-property
              property="driverClassName"
              value="com.mysql.jdbc.Driver" />
            <set-property
              property="url"
              value="jdbc:mysql://ppc7.bio.ucalgary.ca/antimicro_peptides" />
            <set-property
              property="username"
              value="root" />
            <set-property
              property="password"
              value="xxxxxxxxxx" />
            <set-property
              property="maxActive"
              value="10" />
            <set-property
              property="maxWait"
              value="5000" />
            <set-property
              property="defaultAutoCommit"
              value="false" />
            <set-property
              property="defaultReadOnly"
              value="false" />
         </data-source>
    </data-sources>
  

  
  <form-beans>

    <form-bean      name="TestFormBean"
                    type="meinpackage.TestFormBean"/>

  </form-beans>
  
  
  
  
  
  <global-forwards>
     <forward name="welcome" path="/Welcome.do" />
  </global-forwards>   

  
  
  
  <action-mappings>
  
  <action
      path="/Welcome"
      forward="/pages/welcome.jsp" />
  
   
    <action   path="/TestAction"
              type="meinpackage.TestActionHandler"
              name="TestFormBean"
              validate="false"
              scope="request">
    <forward name="success" path="/pages/TestResult.jsp"/>          
    <forward name="failure" path="/pages/Error.jsp"/>          
    </action>
    
    <action
      path="/result_display"
      forward="/pages/welcome.jsp" />

  </action-mappings>

</struts-config>
```

zuerst sollte auf die welcome.jsp seite verbunden werden, von dieser dann auf folgende TestForm.jsp:


```
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<html:html>
<head>
<title>test form</title>
</head>
<body bgcolor="white">
[B]This is a template STRUTS APP[/b]
Enter your name:
<html:form action="/TestAction">

      <html:text property="ipString" size="50"/>

      <html:submit property="submit" value="Submit"/>
</html:form>
</body>
</html:html>
```

dort sollte dann der TestActionHandler aufgerufen werden:


```
package meinpackage;





import java.io.IOException;

import java.lang.reflect.InvocationTargetException;

import java.util.Locale;

import java.util.Vector;

import javax.servlet.*;

import javax.servlet.http.*;

import org.apache.struts.action.*;

import java.sql.*;

import java.util.ArrayList;

import javax.sql.*;

import javax.servlet.ServletContext;



import meinpackage.*;





public final class TestActionHandler extends Action {





    // --------------------------------------------------------- Public Methods





    /**

     * Template Action class

     */

    public ActionForward perform(ActionMapping mapping,

				 ActionForm form,

				 HttpServletRequest request,

				 HttpServletResponse response)

	throws IOException, ServletException {





	

       

         TestFormBean tform = (meinpackage.TestFormBean) form;

         // We will get the emps from the DB based on the short abbreviation entered

         /*** Old Code

         *  String tmpStr = "Welcome " + tform.getIpString() + " !";

         *  tform.setIpString(tmpStr);

         */



            String tmpStr = tform.getIpString();

		

		ArrayList empList = new ArrayList();

		

         	HttpSession session = request.getSession();

         	String target = null;

		

		//getEmps() is defined below ...



		empList = getEmps(tmpStr) ;



         	if(empList  != null){

            	target = "success" ;

			/* Addthe arrayList to the session, This is used in Jsp page testResult.jsp */

        		session.setAttribute("proteinTable" , empList);

        	}

       	 else{

            

            	System.err.println("DEBUG - Got null List. Problem accessing database");

            	target = "failure" ;

        	}

       

       	 



        	return (mapping.findForward(target));  





    }



  private ArrayList getEmps(String searchStr){

        Connection con = null;

        Statement  stmt = null;

        ResultSet rs = null;

        ArrayList empList = new ArrayList();

        StringBuffer  resultString ;

        ProteinBean emp;

        try{

        ServletContext context = servlet.getServletContext();

        DataSource dataSource = (DataSource) context.getAttribute(Action.DATA_SOURCE_KEY);

        

        

            con= dataSource.getConnection();

            stmt = con.createStatement();

            

            /** Access records from table emptable */

            StringBuffer sqlString = new StringBuffer("select accession_id, name_of_protein, length_of_proteinseq from protein'");

            rs = stmt.executeQuery(sqlString.toString());

        

            while (rs.next()) {

                emp = new ProteinBean();

                emp.setAccession_ID(rs.getString("accession_id"));

                emp.setName_of_protein (rs.getString("name_of_protein"));

                emp.setLength_of_Proteinseq(rs.getInt("length_of_proteinseq"));

                empList.add(emp);                

            }

            rs.close();

        }

               catch ( SQLException e ) {

            System.err.println("SQL Exception occured while accessing the table" );

            e.printStackTrace();

            return null;

        }

        catch ( Exception e ) {

            e.printStackTrace();

            return null;

        }

        return empList;

    }
}
```

dort erkennt er dann folgende Zeile nichtf:

```
DataSource dataSource = (DataSource) context.getAttribute(Action.DATA_SOURCE_KEY);
```

meine Frage: stimmt die Konfiguration in der struts-config.xml, brauch ich das global forward eigentlich oder kann ich nicht gleich die TestForm.jsp aufrufen, wenn ja wie......
wieso erkennt er mir die Action.DATA_SOURCE_KEY nicht, hab das im internet gefunden, das man auf diese weise die verbindung zur mysql datenbank aufbaut....

vielen dank im voraus...

lg


----------



## Bleiglanz (17. Mrz 2006)

was heisst erkennt er nicht?

welche Exception?

import javax.sql.Datasource?


----------



## paidopoieo (17. Mrz 2006)

ok, hab das ganze struts framework jetzt zum laufen gebracht, jedoch mal ohne datenbankanbindung.....
sobald ich aber folgenden datasource teil dem struts-config.xml file hinzufuege, stuerzt mir das ganze framework ab...


```
<data-sources>
        
       
        <data-source type="org.apache.commons.dbcp.BasicDataSource">
            <set-property
              property="description"
              value="My SQL Database Connection" />
            <set-property
              property="driverClassName"
              value="com.mysql.jdbc.Driver" />
            <set-property
              property="url"
              value="jdbc:mysql://ppc7.bio.ucalgary.ca/antimicro_peptides" />
            <set-property
              property="username"
              value="root" />
            <set-property
              property="password"
              value="xxxxxxxxxx" />
            <set-property
              property="maxActive"
              value="10" />
            <set-property
              property="maxWait"
              value="5000" />
            <set-property
              property="defaultAutoCommit"
              value="false" />
            <set-property
              property="defaultReadOnly"
              value="false" />
         </data-source>
    </data-sources>
```

sobald aber folgenden teil aus dem datasource element enferne: 


```
<data-source type="org.apache.commons.dbcp.BasicDataSource">
```

funktioniert das ganze framework wieder....

Fehlermeldung:

```
org.apache.jasper.JasperException
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NullPointerException
	org.apache.struts.taglib.TagUtils.computeURLWithCharEncoding(TagUtils.java:428)
	org.apache.struts.taglib.TagUtils.computeURLWithCharEncoding(TagUtils.java:311)
	org.apache.struts.taglib.logic.RedirectTag.generateRedirectURL(RedirectTag.java:294)
	org.apache.struts.taglib.logic.RedirectTag.doEndTag(RedirectTag.java:268)
	org.apache.jsp.index_jsp._jspx_meth_logic_redirect_0(index_jsp.java:87)
	org.apache.jsp.index_jsp._jspService(index_jsp.java:59)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
```

verwende struts 1.2.8 und tomcat 5.0.28 
os: fedora core 4


----------

