title: Connect DB2 using Struts, Spring and Hibernate
body: <p>I integrate struts, spring and Hibetnate together. When I tried connecting to DB2 :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;
  
  &lt;bean class="org.apache.commons.dbcp.BasicDataSource" id ="myDataSource" destroy-method="close"&gt;
  
          &lt;property name="driverClassName" value="com.ibm.db2.jcc.DB2SimpleDataSource"/&gt;
          &lt;property name="url" value ="jdbc:db2://localhost:50000/MYTESTDB"/&gt;
          &lt;property name="username" value="db2admin"/&gt;
          &lt;property name="password" value="db2admin"/&gt;        
  &lt;/bean&gt;
  
  &lt;bean id ="mySessionFactory" class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="myDataSource"/&gt;
      &lt;property name="mappingResources"&gt;
          &lt;list&gt;
              &lt;value&gt;/com/tuanhuy/dao/UserBean.hbm.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  
      &lt;property name="hibernateProperties"&gt;               
          &lt;value&gt;hibernate.dialect=org.hibernate.dialect.DB2Dialect&lt;/value&gt;       
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>There are errors</p>
  
  <pre><code>javax.servlet.ServletException: org.hibernate.exception.GenericJDBCException: Cannot open connection
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  
  
  原因 
  
  org.hibernate.exception.GenericJDBCException: Cannot open connection
      org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
      org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
      org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
      org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
      org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
      org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
      org.hibernate.jdbc.BorrowedConnectionProxy.invoke(BorrowedConnectionProxy.java:74)
      $Proxy4.createStatement(Unknown Source)
      com.tuanhuy.dao.UserDAOImpl.checkUserLogin(UserDAOImpl.java:34)
      com.tuanhuy.service.UserService.checkUserLogin(UserService.java:23)
      com.tuanhuy.struts.LoginAction.execute(LoginAction.java:32)
      org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
      org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
      org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
      org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  
  
  原因 
  
  org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (DB2 SQL Error: SQLCODE=-1031, SQLSTATE=58031, SQLERRMC=null, DRIVER=3.57.82)
      org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
      org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
      org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
      org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
      org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
      org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
      org.hibernate.jdbc.BorrowedConnectionProxy.invoke(BorrowedConnectionProxy.java:74)
      $Proxy4.createStatement(Unknown Source)
      com.tuanhuy.dao.UserDAOImpl.checkUserLogin(UserDAOImpl.java:34)
      com.tuanhuy.service.UserService.checkUserLogin(UserService.java:23)
      com.tuanhuy.struts.LoginAction.execute(LoginAction.java:32)
      org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
      org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
      org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
      org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  
  
  原因 
  
  com.ibm.db2.jcc.am.SqlException: DB2 SQL Error: SQLCODE=-1031, SQLSTATE=58031, SQLERRMC=null, DRIVER=3.57.82
      com.ibm.db2.jcc.am.bd.a(bd.java:679)
      com.ibm.db2.jcc.am.bd.a(bd.java:60)
      com.ibm.db2.jcc.am.bd.a(bd.java:127)
      com.ibm.db2.jcc.am.jb.a(jb.java:4191)
      com.ibm.db2.jcc.t4.bb.q(bb.java:697)
      com.ibm.db2.jcc.t4.bb.n(bb.java:525)
      com.ibm.db2.jcc.t4.bb.a(bb.java:329)
      com.ibm.db2.jcc.t4.bb.a(bb.java:112)
      com.ibm.db2.jcc.t4.b.m(b.java:1241)
      com.ibm.db2.jcc.t4.b.b(b.java:1114)
      com.ibm.db2.jcc.t4.b.c(b.java:701)
      com.ibm.db2.jcc.t4.b.b(b.java:687)
      com.ibm.db2.jcc.t4.b.a(b.java:374)
      com.ibm.db2.jcc.t4.b.&lt;init&gt;(b.java:310)
      com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:214)
      com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:224)
      org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
      org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
      org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
      org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
      org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
      org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
      org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
      org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
      org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
      org.hibernate.jdbc.BorrowedConnectionProxy.invoke(BorrowedConnectionProxy.java:74)
      $Proxy4.createStatement(Unknown Source)
      com.tuanhuy.dao.UserDAOImpl.checkUserLogin(UserDAOImpl.java:34)
      com.tuanhuy.service.UserService.checkUserLogin(UserService.java:23)
      com.tuanhuy.struts.LoginAction.execute(LoginAction.java:32)
      org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
      org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
      org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
      org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
      org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
      org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
      org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
      org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  </code></pre>