Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
@Ram Para

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

Thanks
+Pie Number of slices to send: Send
Which App server you are using.
+Pie Number of slices to send: Send
@Ram Para

I am using Apache Tomcat Server and Database server is MSSQL2005.
+Pie Number of slices to send: Send
You can use singlton pattern also.
+Pie Number of slices to send: Send
Why not use hibernate it takes care of connection pooling and lot many things
+Pie Number of slices to send: Send
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;
}
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send


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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
@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.
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5932 times.
Similar Threads
Problem in connecting MySql with Servlets
Locate my data base?
error while using sql update statement
Applet Reading from Excel Sheet
Applet reading from Excel sheet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:28:00.