Database Connection from java projects

Posted on 2006-10-19
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
LVL 15

Assisted Solution

lyonst earned 100 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 ...
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 20

Accepted Solution

Venabili earned 300 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: and 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 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: - 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 100 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 300 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
Is Applet the way to go for my drag and drop system? 8 25
java mysql insert application 14 28
passing enum to a method 4 22
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

860 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