• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Database Connection from java projects

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.
0
raysee
Asked:
raysee
4 Solutions
 
lyonstCommented:
If you are looking for some open source alternatives try these:

http://java-source.net/open-source/connection-pools
0
 
rayseeAuthor Commented:
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 ?
0
 
rayseeAuthor Commented:
which one you prefer in those pools ...
0
Industry Leaders: 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!

 
VenabiliCommented:
Well.
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:
http://java.sun.com/javase/technologies/database/index.jsp
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?
0
 
harshgroverCommented:
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.
    http://www.hibernate.org/

Hope this helps,
Gary
0
 
rayseeAuthor Commented:
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 ...

0
 
rayseeAuthor Commented:
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 ...

0
 
VenabiliCommented:
>>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 :)
0
 
rayseeAuthor Commented:
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 ...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now