Announcement Announcement Module
Collapse
No announcement yet.
JDBC MetaData Through Spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JDBC MetaData Through Spring?

    I'm looking at using Spring as my DAO framework. I'm tired of writing all the try/catch boilerplate for JDBC and Spring seems like a good alternative to rolling my own yet again.

    But for this new project I need to retrieve JDBC MetaData from my database (lists of tables, list of columns/fields in those tables, etc.) and I'm wondering if Spring can do it.

    I looked through the API and the docs and I didn't seen an obvious object to use for doing MetaData querying.

    But maybe (hopefuly) I'm missing something.

    Can anybody comment?

    Thanks!

    - Gary

  • #2
    I am also encoutering the same problem.I also wonder if there is something in spring can retrieve jdbc metadata for me.

    Thanks
    Yoshiyan

    Comment


    • #3
      Wouldn't this do what you want?

      Code:
           JdbcTemplate.execute(new StatementCallback() {
                public Object doInStatement(Statement stmt) throws SQLException {
                    DataBaseMetaData metaData = stmt.getConnection().getMetaData();
                    // do stuff with metaData 
                    return processedMetaData;
                }    
            });
      Ollie

      Comment


      • #4
        Originally posted by oliverhutchison
        Wouldn't this do what you want?

        Code:
             JdbcTemplate.execute(new StatementCallback() {
                  public Object doInStatement(Statement stmt) throws SQLException {
                      DataBaseMetaData metaData = stmt.getConnection().getMetaData();
                      // do stuff with metaData 
                      return processedMetaData;
                  }    
              });
        Ollie
        Er.... Yes! Or at least I think so, I'll go try it.

        I must confess that I was hoping to have MetaData specific query methods already created instead of having to implement a callback but I think that'll work.

        Thanks!

        - Gary

        Comment


        • #5
          There is a DatabaseMetaDataCallback interface that can be implemented with a callback method named processMetaData. Then you can pass this into the static method JdbcUtils.extractDatabaseMetaData.

          Here is an example:

          Code:
          	class GetTableNames implements DatabaseMetaDataCallback {
          		
                  public Object processMetaData(DatabaseMetaData dbmd) throws SQLException {
                      ResultSet rs  = dbmd.getTables(dbmd.getUserName(),null,null,new String[] {"TABLE"});
                      ArrayList l = new ArrayList();
                      while (rs.next()) {
                      	l.add(rs.getString(3));
                      }
                      return l;
                  }
              }
          This is how you would call it:
          Code:
          	    GetTableNames getTableNames = new GetTableNames();
                  try {
                      Object o = JdbcUtils.extractDatabaseMetaData(ds, getTableNames);
                      System.out.println(o);
                  }
                  catch (MetaDataAccessException e) {
                      System.out.println(e);
                  }

          Comment


          • #6
            Thanks Thomas

            Spring's got so many of these little gems it's easy to lose track. Though it would probably make sense to add an extractDatabaseMetaData method to JdbcTemplate.

            Ollie

            Comment


            • #7
              Originally posted by trisberg
              There is a DatabaseMetaDataCallback interface that can be implemented with a callback method named processMetaData. Then you can pass this into the static method JdbcUtils.extractDatabaseMetaData.
              Thomas, thanks! That was perfect, the code is working splendidly.

              - Gary

              Comment


              • #8
                question, does one have to call rs.close() in above call-back class? thanks

                Comment


                • #9
                  I haven't checked that particular class, but given that other callback based utilities clean up after themselves (like JdbcTemplate), I would guess that this does as well.

                  Comment


                  • #10
                    Originally posted by wpoitras
                    I haven't checked that particular class, but given that other callback based utilities clean up after themselves (like JdbcTemplate), I would guess that this does as well.
                    ah, ok
                    I hope so, since I would hate to introduce more boiler-plate code (try-finally), kind of defeats the purpose of using the spring jdbc classes

                    Comment

                    Working...
                    X