| Author |
How to Reuse JDBC connectivity code in other Java programs or classes?
|
Remo Reek
Greenhorn
Joined: Sep 29, 2009
Posts: 5
|
|
Hi everyone I am new to this forum I need everyone's help to solve my problem, Here's the code I have to use this code at any other program or java class. As I am using this code frequently in all programs can anyone help how to call these code as a function or anything???
Program1:
For example,
Program2:
Any help???
Thanks,
Regards,
Remo
|
 |
Ram Para
Ranch Hand
Joined: Jul 09, 2008
Posts: 48
|
|
This is not the right way to use connections.
Use Connection pooling.
Using Connection you can easily write a reusable function which will solve your purpose.
|
Ram Parashar
http://doinfinite.com
|
 |
Remo Reek
Greenhorn
Joined: Sep 29, 2009
Posts: 5
|
|
@Ram Para
Can you provide some sample code based on my example, I am just a beginner to JDBC.
Thanks
|
 |
Ram Para
Ranch Hand
Joined: Jul 09, 2008
Posts: 48
|
|
|
Which App server you are using.
|
 |
Remo Reek
Greenhorn
Joined: Sep 29, 2009
Posts: 5
|
|
@Ram Para
I am using Apache Tomcat Server and Database server is MSSQL2005.
|
 |
Malhar Me
Greenhorn
Joined: Jun 22, 2009
Posts: 27
|
|
|
You can use singlton pattern also.
|
 |
binu narayanan
Ranch Hand
Joined: Jul 24, 2009
Posts: 56
|
|
|
Why not use hibernate it takes care of connection pooling and lot many things
|
 |
Ram Para
Ranch Hand
Joined: Jul 09, 2008
Posts: 48
|
|
Perfect Configure a datasoure in Server.xml.
provide a JNDI name
you can refer
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
write a method
static Connection getJNDIConnection(){
String DATASOURCE_CONTEXT = "java:comp/env/jdbc/blah";//jndi from server.xml
Connection result = null;
try {
Context initialContext = new InitialContext();
if ( initialContext == null){
log("JNDI problem. Cannot get InitialContext.");
}
DataSource datasource = (DataSource)initialContext.lookup(DATASOURCE_CONTEXT);
if (datasource != null) {
result = datasource.getConnection();
}
else {
log("Failed to lookup datasource.");
}
}
catch ( NamingException ex ) {
log("Cannot get connection: " + ex);
}
catch(SQLException ex){
log("Cannot get connection: " + ex);
}
return result;
}
|
 |
Remo Reek
Greenhorn
Joined: Sep 29, 2009
Posts: 5
|
|
Thanks everybody, actually what my master ask me to do is create a class and call them in your program but I am not getting that thing.
I shouldnot have that JDBC connection code in my every program but I have to use them by anymeans in my another java programs.
|
 |
Balu Sadhasivam
Ranch Hand
Joined: Jan 01, 2009
Posts: 874
|
|
In program 1, add a method that returns a connection.
In program , create a DisplayDB object whenever you need connection object and call getConnection.
But most important thing about Connections are to close them as soon as you finish them so as to avoid holding up resources. So add method to close the connection object
|
 |
binu narayanan
Ranch Hand
Joined: Jul 24, 2009
Posts: 56
|
|
Create a class called DB do the common connection coding in that class. Like driver manager, connection string etc. Then import this class in the required class to make a connection and do not forget to close all the opened connection.
I am not in a position to send you some sample code.But will post as soon as get hold of one
|
 |
Remo Reek
Greenhorn
Joined: Sep 29, 2009
Posts: 5
|
|
@Balu Sadhasivam
Thanks everyone for your help and forgive me for not answering for your solutions, I actually tried your code but I need somemore clear explanation and sample, But the thing is really superb, Thanks for everyones help.
|
 |
 |
|
|
subject: How to Reuse JDBC connectivity code in other Java programs or classes?
|
|
|