• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

connection without class.forName

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
How can i get the data base connection without using the class.forName.
Thanks in advance
Hari
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
You can use the following of course if you are having a App Server in place:
import javax.sql.*;
import javax.naming.*;
.....
.......
and within method
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
InitialContext ictx = new InitialContextFactory(ht);
DataSource ds = (DataSource)ictx.lookup("some_jdbc_connection_pool_in_server");
Connection cn = ds.getConnection();
.........
then as usual whatever you want to do with connection
..............
Hope this serves your need
Regards
K.Muthukumaran

InitialContext ictx
 
Hari babu
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to do this without using my application server. how do i do this ?
Any help is appreciated
Hari
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use the DriverManager class:
Oracle example:

Jamie
[ September 22, 2002: Message edited by: Jamie Robertson ]
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks...

But i have seen people connecting to dataBase sucessfully in the following manner.
how is this done??

Following is the link of the code from javaranch user only who was able to access database without the DriverManager.registerDriver statement and Class.forName.

https://coderanch.com/t/401176/java/java/Class-forName

Please explain me .... how is he able to access in this fashion without DriverManager.registerDriver statement.

-Thanks
Anju
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The user in the thread linked imports the driver and then does the equivalent thing to Class.forName(), except that their code is now hard-coded to the Driver class. Changing database DRivers requires them to recompile their code.

If you've every seen applications like Jira (bug tracking) and a whole bunch of others including anything that accesses a database a wondered 'How do they have code that can change databases so easily?', the answer is using either Class.forName() or a DataSource to make your code independent of the dtaabase type and Driver type.

Dave
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use the jdbc.drivers property to register a driver to the DriverManager e.g java -Djdbc.drivers=drivers MyProgram. Then you can skip the Class.forName("driver") since the driver will already be registered in the DriverManager.
 
A Sethi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I got it
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic