Data is not getting saved to the database

Discussions

EJB programming & troubleshooting: Data is not getting saved to the database

  1. Data is not getting saved to the database (4 messages)

    Hi,

    I am new to Java. Wrote code given below to insert data to the Database. Every thing seems to be fine, not getting any exception but data is not getting saved to the database. Please some one suggest me what could be wrong.

     public void insertBatch(Vector list) throws SQLException, DAOException
     {
      logger.debug("insertBatch(): Enter");
      Connection connection = null;
      Statement stmt = null;
      
      try
      {
       int count = 0;
       String sql = null;
       Iterator it = list.iterator();
       connection = DBUtil.getDBConnection();
       stmt = connection.createStatement();
       connection.setAutoCommit(false);

       while(it.hasNext())
       {
        sql = String.valueOf(it.next()).trim();
        stmt.addBatch(sql);
        logger.debug(count + " - BATCH SQL: " + sql);
        count++;
       }
       int [] code = stmt.executeBatch();
       for(int i = 0; i < code.length; i++) System.out.println("BATCH ERROR CODE - " + code[i]);
      }
      catch(ServiceLocatorException e)
      {
       logger.error("ServiceLocatorException:", e);
       throw new DAOException("ServiceLocatorException: Unable to get servicelocator",e);
      }
      catch(SQLException se)
      {
       switch(se.getErrorCode())
       {
        case LogErrorConstants.SQLCODE.DEADLOCK : logger.error("SQL DEADLOCK in insertBatch(): ", se);
         //connection.rollback();
         break;
        case LogErrorConstants.SQLCODE.UNVALIABLE_RESOURCE : logger.error("Unavailable Resource insertBatch: ", se);
         //connection.rollback();
         break;
        case LogErrorConstants.SQLCODE.RECORD_ALREADY_EXIST : logger.error("Duplicate Record insertBatch(): ", se);
         //connection.rollback();
         break;
        default:
         //connection.rollback();
         logger.error("SQL EXCEPTION in insertBatch(): ", se.getNextException());
       }
       throw new SQLException("SQLCode " + se.getErrorCode());
      }
      finally
      {
       connection.commit();
       DBUtil.closeResources(stmt, connection);
       logger.debug("insertBatch(): Exit");
      }
     }

    Thanks in advance

    Threaded Messages (4)

  2. Make sure you are working in a transaction.

  3. What does it mean?[ Go to top ]

    What does it mean? Any more explanation to this. I really need to figure out things out. - Michael Courouleau

  4. transaction[ Go to top ]

    Use transaction. A transaction comprises a unit of work performed within a database management system (or similar system) against a database, and treated in a coherent and reliable way independent of other transactions. Transactions in a database environment have two main purposes

     

    _____________

    agile development methodology

  5. Use different methods for defferent tasks. as

    public static void main(String args[])

    {

       //statements..

      new JDBC().JdbcQuery();   //calls database class and and its method to perform on data

    }

     

     

    void JdbcQuery()

    {

      createConnection();  //calls another method to create connection

      statements;  // your work on data, may be database queries

      //close the connections you have made

    }