title: Can you determine which database vendor is used from a SQLException?
body: <p>I'm assuming the simple answer is <em>no</em>, but wondering if one of the Java ninja's out there might know of a clever hack...  More specifically my problem is I am trying to create automation that can extract/restore a database from some other data format (xml, another database vendor, json...).  I am trying to limit the scope of vendor specific code to an <code>*Adapter</code> class.  The interface is:</p>
  
  <pre><code>public String escapeName( String name );
  
  public ColumnMetaData columnMetaDataFromResultSet( ResultSet resultSet ) throws SQLException;
  
  public SqlQuery getColumnMetaDataSql( String schema, String table );
  
  public String getCreateTableSql( TableMetaData tableMetaData );
  
  public String getDropTableSql( TableMetaData tableMetaData );
  
  public String getRowInsertSql( TableMetaData tableMetaData );
  
  public String getRowSelectSql( TableMetaData tableMetaData );
  
  public SqlQuery getTableMetaDataSql();
  
  public TableMetaData tableMetaDataFromResultSet( ResultSet resultSet ) throws SQLException;
  </code></pre>
  
  <p>The important method to this question is <code>getDropTableSql( TableMetaData tableMetaData )</code>.  That method would theoretically return a SQL statement that would drop the table indicated by the metadata so that it could be recreated (this is used to publish full datasets on fixed term basis so, out with the old, in with the new).  The adapters for both MySQL and SQLServer support some flavor of safe <code>DROP</code> (<code>DROP table IF EXISTS</code> or similar), but Oracle and DB2 don't.  So my next thought was to just drop it and catch the appropriate exception.  But the exception that gets thrown is <code>java.sql.SQLSyntaxErrorException</code>(or <code>org.springframework.jdbc.BadSqlGrammarException</code> since I am using springs <code>JdbcTemplate</code>).  That family of exception seems far to broad to catch safely (and it doesn't really seem like the grammar to me...).  Each vendor seems to return a different SQLState and ErrorCode, so those are not much help unless I can also determine the vendor.  So, can I?  I know that spring does it somehow, most likely by digging it out of the <code>DataSource</code> that issued the query.  Just haven't figured it out yet.</p>
  
  <p>---------------------- UPDATE ---------------------------</p>
  
  <p>I was able to achieve my goal with this code: </p>
  
  <pre><code>private void dropTable( JdbcTemplate jdbcTemplate, String dropSql ) {
      try {
          jdbcTemplate.execute( dropSql );
      }
      catch ( BadSqlGrammarException sqle ) {
          try {
              throw sqle.getRootCause();
          }
          catch ( SQLException rootCause ) {
              int errorCode = rootCause.getErrorCode();
              String sqlState = rootCause.getSQLState();
  
              try {
                  String vendor = jdbcTemplate.getDataSource().getConnection().getMetaData().getDatabaseProductName();
  
                  if ( vendor.equalsIgnoreCase( "mysql" ) &amp;&amp; errorCode == 1051 &amp;&amp; sqlState.equals( "42S02" ) ) {
                      logger.info( "mysql table does not exist, unable to drop ({})", dropSql );
                  }
                  else if ( vendor.equalsIgnoreCase( "oracle" ) &amp;&amp; errorCode == 942 &amp;&amp; sqlState.equals( "42000" ) ) {
                      logger.info( "oracle table does not exist, unable to drop ({})", dropSql );
                  }
                  else {
                      throw( sqle );
                  }
              }
              catch ( Exception e ) {
                  throw( sqle );
              }
          }
          catch ( Throwable e ) {
              throw sqle;
          }
      }
  }
  </code></pre>