Solved

Database Connection from java projects

Posted on 2006-10-19
9
215 Views
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.
0
Comment
Question by:raysee
9 Comments
 
LVL 15

Assisted Solution

by:lyonst
lyonst earned 100 total points
Comment Utility
If you are looking for some open source alternatives try these:

http://java-source.net/open-source/connection-pools
0
 

Author Comment

by:raysee
Comment Utility
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
 

Author Comment

by:raysee
Comment Utility
which one you prefer in those pools ...
0
 
LVL 20

Accepted Solution

by:
Venabili earned 300 total points
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Assisted Solution

by:harshgrover
harshgrover earned 100 total points
Comment Utility
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
 

Author Comment

by:raysee
Comment Utility
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
 

Author Comment

by:raysee
Comment Utility
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
 
LVL 20

Assisted Solution

by:Venabili
Venabili earned 300 total points
Comment Utility
>>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
 

Author Comment

by:raysee
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countAbc challenge 9 49
countHi2 challenge 7 43
mapShare challenge 13 66
starter POM and spring-boot-starter,  spring-boot-web 2 9
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now