5

I am newbie to Java Spring and learning JDBC template to access database. Now i have 3 relational tables and i need to join them using JDBC template and need to print the result. How can i implement it. Any working example will sure help me a lot.

Thanks

Manish
  • 3,341
  • 15
  • 52
  • 87
  • See if [**this**](http://forum.spring.io/forum/spring-projects/data/53153-using-jdbctemplate-on-join-table) helps – Paul Samsotha Jan 19 '14 at 07:48
  • I am confused about this example as i m not experienced in Spring. Any complete workinng example will help me a lot. – Manish Jan 19 '14 at 07:57

1 Answers1

7

You're really asking a multi-facetated question. I have provided links to of SOs and posts below, but there is many many more answers out there. If these aren't of any use just search for another one there are literally hundreds of posts out there all on the same subject.

  1. you need a query that'll combine three tables into one query (you don't mention if you'll need nested joins, or a simple join three times). For that i suggest you read up on some SQL. Here is a post that will give you an example to a nested select and links to multiple other posts explaining different SQL.

  2. You need to use Spring JDBC (you don't mention which specific template implementation you are using). Here is post that gives you RowMapper examples and the logic to iterate over the results.

  3. you require logic to iterate over the results. This is done most easily with Springs RowMapper or ResultSetExtractor interfaces. Here is a post that'll explain the differences between the two and link to the API (which I recommend you read).

ResultSetExtracor Implementation Example - plus iteration logic

RowMapper Implementation Example - plus iteration logic

Community
  • 1
  • 1
Prancer
  • 3,336
  • 2
  • 32
  • 38
  • Which means it is like normal jdbc in which we combine tables by writing sql query. There is no need to create multiple classes for each table for joining tables. – Manish Jan 22 '14 at 13:36
  • i found one example http://kaninotes.blogspot.com/2013/01/data-access-with-spring-jdbc-note-1.html but here we need to create two classes for joining tables. if i wish to join 7 classes howill i implement this. It will be horrible dream that team. – Manish Jan 22 '14 at 13:46
  • You don't want to model your objects from your database tables. These are two very different things. One is an object (knows nothing about a database), one is a storage location for data (knows nothing of objects). In the linked example you pointed out, I think you are getting these two concepts mixed up. Although the Kani's objects might seem to be modeled on tables, they are not (as pointed our by the Employee -> has a list of Projects). Kani has simply created an object-oriented design and has a relational database – Prancer Jan 22 '14 at 16:43
  • Which means no need to use modeled objects in Sping JDBC template? – Manish Jan 23 '14 at 13:10
  • Unless you are using ORDBMS you are not going to want to have a Person Object = person table....a Person object may have many more traits than your person table such as an address, phone, etc -- each which should be separate objects; Person, has-a Address, has-a Phone. Your person table may have all these fields, or some mix of them, all in one table... You will want to develop your classes similar to how the example you posted has and build your object from the result set returned from the database when you run your query – Prancer Jan 24 '14 at 21:37
  • 2
    This doesn't answer the question – FazoM Oct 29 '15 at 14:18