Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems with PreparedStatement.getGeneratedKeys

838962Feb 11 2011 — edited Feb 11 2011
I need some help understanding why this code isn't working. I'm using Java 5, ojdbc14 and Oracle 10g.

The code below is supposed to insert a record. The table has a BEFORE INSERT trigger that generates a new primary key into the LTM_LETTER_TEMPLATE column. I need the code to return the key. I have found several examples of how to do this, but they all cause the same exception on the call to conn.prepareStatement. Stack trace is below as well. I searched the forum but didn't find anything that helped. Thanks in advance.
   long insertLetterTemplate(LetterTemplateVO pLetterTemplate) throws LetterTemplateException
   {
      final String insertSql = "insert into CS_LETTER_TEMPLATE "
         + "LTM_LETTER_TYPE_CD, "
         + "LTM_REGION_ID,  "
         + "LTM_LETTER_BODY,  "
         + "LTM_LETTER_FOOTER,  "
         + "LTM_USER_ID values (?,?,?,?,?)";
      
      String[] rtnCols = {"LTM_TEMPLATE_CD"};
      PreparedStatement ps = null;
      ResultSet rs = null;
      long rtnLtmTemplateCd = -1;
      DAOLogger logger = new DAOLogger(this.getClass(), "insertLetterTemplate");
      
      try
      {
         logger.begin();

         ps = conn.prepareStatement(insertSql, rtnCols);  //<- - - THIS LINE FAILS WITH EXCEPTION SHOWN BELOW
         FpsLogger.debug(this.getClass(), conn.nativeSQL(insertSql));
         ps.setLong(1, pLetterTemplate.getLtmLetterTypeCd());
         ps.setInt(2, pLetterTemplate.getLtmRegionId());
         ps.setString(3, pLetterTemplate.getLtmLetterBody());
         ps.setString(4, pLetterTemplate.getLtmLetterFooter());
         ps.setLong(5, pLetterTemplate.getLtmUserId());
         ps.executeUpdate();
         rs = ps.getGeneratedKeys();
         if (rs.next()) 
         {
            rtnLtmTemplateCd = rs.getLong(1);
         } else
         {
            LetterTemplateException lte = new LetterTemplateException("Insert failed. No sequence id returned from database.");
            logger.error(lte, "Insert failed. No sequence id returned from database.");
            throw lte;
         }
         logger.end();
      
      } catch (SQLException ex)
      {
         LetterTemplateException lte = new LetterTemplateException("Failed to update letter template due to a database exception.", ex);
         logger.error(lte, "Failed to update letter template due to a database exception.");
         throw lte;
      } finally
      {
         DAOUtil.closeJDBCObjects(ps, rs, conn);
         logger.closedConnection();
      }   
      return rtnLtmTemplateCd;
   }

This is the stack trace from the exception:
Caused by: java.sql.SQLException: Unsupported feature
	at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
	at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:161)
	at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:226)
	at oracle.jdbc.driver.DatabaseError.throwUnsupportedFeatureSqlException(DatabaseError.java:536)
	at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:2994)
	at com.cs.letter.template.LetterTemplateDAO.insertLetterTemplate(LetterTemplateDAO.java:267)
Edited by: user9221846 on Feb 11, 2011 7:14 AM

Edited by: user9221846 on Feb 11, 2011 7:15 AM

Edited by: user9221846 on Feb 11, 2011 7:47 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 11 2011
Added on Feb 11 2011
7 comments
1,412 views