[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Database Connection from java projects

Posted on 2006-10-19
Medium Priority
Last Modified: 2010-03-31
Hello experts,

I am trying to prepare my middle ware independent from the User Interface technology and DBMS used in the back end. that means a middle ware code which has the business logic and works for Web applications , swing applications and so on...

but Here from the Web application and Rich cleint and fat client applications ... the database connections are different .

So I want your suggestions how to Generalize and handle my database connection/s (connection pools)  which works for all types of applications and to fix the places where I have to change if my database changes or my application type changes.

and I want to make the places as less as possible where I am depending on the applcaition specific technology.

so I am searching examples for Swing applications and we applications , how the database connections are handled.

Please try to give practical examples where I can learn the standards of dealing with Database connections and what to change if I change from web application to swing and one database to another ...

thanking you in advance.
Question by:raysee
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Assisted Solution

lyonst earned 400 total points
ID: 17765271
If you are looking for some open source alternatives try these:


Author Comment

ID: 17765624
thank you lyonst,

ya I am going to read about connection pools ... but do you think this works for both web applications and rich/fat client applications ?

Author Comment

ID: 17765827
which one you prefer in those pools ...
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 20

Accepted Solution

Venabili earned 1200 total points
ID: 17769240
If you use JDBC, it does not really matter if you use it in a web or in a fat client....
As for moving between databases - as long as you do not use specific for the DB sql and or methods, this is not a problem - change the drivers and in some cases the connection string and you are in.
I guess you are already familiar with this:
but just in case...

If you want pooling, lyonst gave you the options. I am personally using two of the pools:
http://sourceforge.net/projects/c3p0 and http://homepages.nildram.co.uk/~slink/java/DBPool/ and in the last months I seem to be using more the c3p0 one...

Both are easy to be used... What I usually do is to create a DB.java class which contains ALL the DB work - and it means all of it. And then to use the same class in the Web clients and in the Fat ones.  So in the real code of the application, you simply call methods of the DB class.

An example:
http://www.oreillynet.com/pub/a/oreilly/java/news/javaex_1000.html - swing + jdbc - pretty primitive but...

What bothers you in web/fat clients? There is no real differences in handling the databases in both... Unless I miss something from your question?

Assisted Solution

harshgrover earned 400 total points
ID: 17771802
hey raysee,
    it might be a good idea to give us a bit more idea on what kinda middleware application you are trying to develop.

    being database independent does not just mean being independent of different connection pools to the databases. you would really have to think it through. The databases are not standard across the board. the DB Scripts, the queries, they dont all work seamlessly. I haev worked with DB2, Oracle & Mysql, and there is quite a startling difference between the query languages for all the three systems.

     With that in mind, you might want to think about choosing a database, and sticking with it. But again, since i dont know what kind of middleware you are looking to develop, It would be difficult to comment or make suggestions on the same.

    You might want to look at relational persistance, eg tools like Hibernate, which could provide you with some kind of database independence.

Hope this helps,

Author Comment

ID: 17773179
Thank you Venabili,

ya till now I am working with JDBC but I always did it with Swing applications (that is fat cleints) but first time I am making a struts application ...

in my fat  client the database was also on the same system so I had a static connection to the database ...

I also did the same I have a DBConnectionManager  class which has a static method getconnection... and the connection object in this method was also static ... and I felt this doesn't work with many users in a web application ...

then I read some where about connection polling ... so I wanted to take your suggestions , how you handle your connection pooling...

Thank you again for your example of JDBC with swing but it is also a single connection I think (I did not see the source code till now) ... if it is a single connection with JDBC do you have any connection pooling example where I can see their implementation ?

any way thank you ...


Author Comment

ID: 17773246
Hi Harshgrover,

thank you for your suggestion ... ya I have started reading Hibernate but as you know it takes some time to bring it into my project but before that I want to use connection pooling and then use hibernate to make my middleware database independent ...

all I am write is ... dividing all of my classes which are not related to the userinterface into a middleware source folder and providing a single class called Service ... from where you can access the middle ware classes...

so that I wanted to keep my middleware code independent from the userinterface technology and I am going to try this with Struts , Swing and Ajax ... as I have only point to access my middleware and that is independent from any technology I don't have any problem with front end but my DAO classes (which are accessing data from the datase) may change if I change the database ...

so that also I want to keep as less as possible by implementing Hibernate  ...

any way if I have hibernate it will have its own connection pool but till then I want to use JDBC connection pooling ... and as simple SQL statements as possible ...

LVL 20

Assisted Solution

Venabili earned 1200 total points
ID: 17773340
>>Thank you again for your example of JDBC with swing but it is also a single connection I think (I did not see the source code till now) ... if it is a single connection with JDBC do you have any connection pooling example where I can see their implementation ?

There are examples of pooling only or swing only. Not that hard to combine.  For a pooling example - select a pool and get the examples from there.
Again - get ALL your DB work OUT of the code and move it in a single file ( or a few files if you prefer this)

>>I also did the same I have a DBConnectionManager  class which has a static method getconnection... and the connection object in this method was also static ... and I felt this doesn't work with many users in a web application ...

Well.. it works. But if it bothers you, then make it NON static :) And just make something like:
DBManager manager = new DBManager();
manager.getConnection(); - if you need pooling, this class will be implementing it.
manager.executeSelect .. and so on
After this you won't really care if it is a pool OR a single question - for your frontend it will be a single connection, and the DBManager will be the one making the pools if needed, managing them and so on

This way you will be independand from the front end -- as long as it can create a class, it will be fine :)

Author Comment

ID: 17773559
ok Venabili thank you and I will implement it with out static connection ...

and at the moment I have downloaded the DBPool and c3Po I will use one of them over the week end ... and let you know ...

may be as you have used c3po ... could you please give an example connection manager class ... which creates a connection pool and manges it ?

thank you ...

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question