2

I have to develop a Swing Project. I need to access the database in various places. So how should I arrange the classes so that there is one database class. Should I use inheritance for that. Just a brief outline. I'm a java(struts/spring/hibernate) developer but its going to be my first Swing Application.

2
  • what will you put in your database class? are you using JDBC? Commented Feb 29, 2012 at 13:14
  • @cacho ya jdbc on mssql. Commented Feb 29, 2012 at 13:15

2 Answers 2

7

Inheritance? No, rather you'd use composition. Accessing a database is no different for a Swing application than a non-Swing application with a few caveats:

  • Do all database access off of the main Swing event thread or event dispatch thread (EDT).
  • Do most all Swing calls on the EDT.
  • If the data will change dynamically will need to read up on ways to listen for data changes and bind the data to your display.
Sign up to request clarification or add additional context in comments.

Comments

5

A few ideas:

  • I think you should use an ORM (like OrmLite, for example) in your application

  • Create a package which will contain interfaces or abstract classes for your data access layer.

  • Create another package where your interfaces and abstract classes (of the data access layer) are implemented (these implementations should contain all your direct commands to the database)

  • Create another package, where you will have your business layer. The methods of your classes in this package should use the data access layer through the interfaces and combine them to solve any business-logic-level problem

  • You should access directly your business layer classes from the backend part of your application which should be separated from your user interface as much as possible

2 Comments

1+ I wish I could transfer all my upvotes on this question to your answer. Very nice.
I've also upvoted your answer, I believe it can be useful for many.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.