Bonjour tout le monde,
J'ai une application avec struts 1, et j'essaie de configurer une datasource mysql, j'ai essayé deux méthodes mais j'ai pas réussit:
1) dans struts config j'ai mis :
et dans ma class action j'ai mis:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <data-sources> <data-source type="org.apache.tomcat.dbcp.dbcp.BasicDataSource" key="dbsource"> <set-property property="driverClassName" value="com.mysql.jdbc.Driver"/> <set-property property="url" value="jdbc:mysql://localhost:3306/gestion_production?autoReconnect=true" /> <set-property property="username" value="pankaj"/> <set-property property="password" value="pankaj123"/> <set-property property="maxActive" value="200"/> <set-property property="maxWait" value="5000"/> <set-property property="defaultAutoCommit" value="false"/> <set-property property="defaultReadOnly" value="false"/> </data-source> </data-sources>
ca ne compile pas la méthode getDataSource(request);
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { javax.sql.DataSource dataSource; java.sql.Connection myConnection; try { dataSource = getDataSource(request); myConnection = dataSource.getConnection(); // 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); } } };
et la deuxième méthode j'ai fait :
Dans le fichier context.xml
et dans web.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <Context> <Resource name="jdbc/gestion_production" auth="Container" type="javax.sql.DataSource" maxActive="50" maxIdle="30" maxWait="10000" username="pankaj" password="pankaj123" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/gestion_production"/> </Context>
et dans la class action:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 <resource-ref> <description>MySQL Datasource example</description> <res-ref-name>jdbc/gestion_production</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
et ça donne nulle pointer exception au niveau de ds, sachant que j'ai copié le connecteur dans tomcat.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 @Resource(name="jdbc/gestion_production") private DataSource ds; Connection con = ds.getConnection();
SVP avez vous une idée?merci d'avance.
Partager