• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to Reuse JDBC connectivity code in other Java programs or classes?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Remo Reek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ram Para

Can you provide some sample code based on my example, I am just a beginner to JDBC.

Thanks
 
Ram Para
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which App server you are using.
 
Remo Reek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ram Para

I am using Apache Tomcat Server and Database server is MSSQL2005.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use singlton pattern also.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use hibernate it takes care of connection pooling and lot many things
 
Ram Para
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic