Categories

Main
Database
Java
Microsoft.NET
Adabas
DB2
Informix
Microsoft SQL Server
MySQL
Oracle
Pervasive.SQL
PostgreSQL
Sybase
Other
ASP
ColdFusion
Crystal Reports
Delphi, C etc
JAVA
Microsoft.NET
Perl and the DBI
PHP
ANSI SQL
Unix Shell Scripts
Visual Basic
XML & XSLT
Corel Paradox
FileMaker
Microsoft Access
Microsoft Excel
Other PC Databases
Applications & Tools
Database Concepts & Design
EJB programming & troubleshooting
EJB design
General J2EE
XML & Web services
Web tier: servlets, JSP, Web frameworks
Performance and scalability
Industry news
TSS feedback
Mobicents Contributors
Mobicents Users
JSLEE Resource Adaptor Types
Planning JavaOne 2008
Sun Tech Days
Other Java conferences
Binary Web Services and XML
Metro and JAXB
GlassFish
GlassFish Plugins
Project jMaki
GlassFish WebTier
Mural
Java Development Tools
Java WS & XML Community News
JAXP
Java SE
6uN Early Access
Java Quick Starter
Java SE Snapshots: Project Feedback
JCK Forum
Feedback and Suggestions
JDK Distros
JDK Open Source
General JSR Discussion
JCP 2.6
JXTA Community Forum
ME Interest
ME Cool Apps
ME General Help
ME Feedback & Suggestions
ME Application Developer Interest
Blu-ray Disc Java
ME Developer Days
Squawk
Mobile Developer Alliance
OpenCable
LWUIT
JavaFX Script Language Discussion
OpenJFX General Discussion
Scene Graph
General Performance Discussion
Your Java Career
NetBeans 6.0
Servlets
JSP
JSF
Portals and Portlets
EJB and Other Java EE Technologies
Distributed Java
Object Relational Mapping
JDBC
Web Services
Swing / AWT / SWT / JFace
JNLP and Web Start
Java Micro Edition
Sockets and Internet Protocols
Threads and Synchronization
Performance
Applets
I/O and Streams
Other Java APIs
Game Development
Java in General (beginner)
Java in General (intermediate)
Java in General (advanced)
Programmer Certification (SCJP)
Developer Certification (SCJD)
Associate Certification (SCJA)
Web Component Certification (SCWCD)
EJB Certification (SCBCD)
Mobile Application Certification (SCMAD)
Architect Certification (SCEA)
Web Services Certification (SCDJWS)
XML Certification
Product and Other Certifications
Mock Exam Errata
Sun Certification Results
Authors' Corral
Book Reviews
Events
Bunkhouse Porch
Teachers' Lounge
Testing
OO, Patterns, UML and Refactoring
IDEs, Version Control and other tools
Ant, Maven and Other Build Tools
Linux / UNIX
Mac OS
HTML and JavaScript
XML and Related Technologies
Agile and Other Processes
General Computing
Security
Groovy
Scala
Other Languages
Struts
Application Frameworks
Other Open Source Projects
BEA/Weblogic
IBM/Websphere
Oracle/OAS
Apache/Tomcat
JBoss
Other Java Products and Servers
JavaRanch
Cattle Drive (java college)
Moderators Only
Trash Can
Jobs Offered
Jobs Wanted
Jobs Discussion
Meaningless Drivel
Programming Diversions
Blatant Advertising
Java Announcements
New To Java
Advanced Java
Java Applets
Networking
Threads and Synchronization
Java 2D
AWT / Swing
SWT / JFace
CLDC and MIDP
CDC and Personal Profile
Sun Java Wireless Toolkit
Enterprise JavaBeans
JavaServer Pages (JSP) and JSTL
Java Servlet
JavaServer Faces
Web Frameworks
Database
XML
Lucene
NetBeans
Eclipse
IntelliJ IDEA
JCreator
Other IDEs
Java Tutorials
Java Tips
Jobs Discussion
Jobs Offered
Jobs Wanted
Professional Certification
Forum Lobby
Java Blogs
Introductions
Reviews / Advertising
Suggestions & Feedback

Resources

Java Database
Linux
Coding
Mobile
Hardware
Software Development
Software Development
iOS,OS X
iOS,OS X
ORACLE
IBM DEVELOPER
IBM DEVELOPER
MSDN
MSDN


Tags

JDBC

Database access, JDBC/ODBC, could even brush on some SQL

DriverManager.getDrivers() Basic question


Hi,What exactly happens when i say Class.forName("MyDriverClass") ?I have two sample programs in one I load the driver and in other I try to get the drivers loded using DriverManager.getDrivers(). But In the second program it doesn't return the driver. But I I retrieve the driver in first progrma then it lists the driver which is loaded using Class.forName.I also tried to load DriverManager.registerDriver(new MYDriverClass()). This also does work in the above mentioned scenario.Here is the code. Thanks for the time.Example Program to load driver:code:import java.sql.*;import java.util.*;public class Example2 {public static void main(String args[]){try{Class.forName("oracle.jdbc.driver.OracleDriver");}catch(Exception e){System.out.println(e);}}}Example to test the loaded drivers:code:import java.sql.*;import java.util.*;public class Example1 {public static void main(String args[]){try{List drivers = Collections.list(DriverManager.getDrivers());for(int i=0;i<drivers.size();i++){Driver driver = (Driver)drivers.get(i);String driverName = driver.getClass().getName();System.out.println("Driver "+i+":::"+driverName);}}catch(Exception e){System.out.println(e);}}}--------------------L G Goundalkar                 lggoundalkar@yahoo.com                 Sun Certified Programmer for Java 2 Platform.Sun Certified Web Component Developer for J2EE.

   
   

 
   
      If you look at the documentation for thejava.sql.Driverinterface, you'll see the following:quote:When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a driver by callingClass.forName("foo.bah.Driver")From what I've seen, the driver classes (these are the classes that implement "java.sql.Driver") will include a "static { ... }" block that instantiates the driver and registers it with the "DriverManager" class.There are various ways of registering drivers, but if you're doing it programmatically, you should always use "Class.forName(...)".[ February 18, 2004: Message edited by: Wayne L Johnson ]

   
   
   
   
   
      Hi..I agree with your comments. But what i need to know is in my first program i initialised the driver and since its a static one, it should be present in the JVM's Class Loader. So when I run my second program it should return me the loaded driver. Isn't it ?Cheers.--------------------L G Goundalkar                 lggoundalkar@yahoo.com                 Sun Certified Programmer for Java 2 Platform.Sun Certified Web Component Developer for J2EE.

   
   

 
   
      I guess I misunderstood your initial question.  Exactly how are you running the tests?  Since both of the classes are "public" I must assume that they each reside in separate source files, "Exampe1.java" and "Example2.java".If you are testing by executing each in turn:code:> java Example2> java Example1Then the second application one won't find the driver loaded by the first, which is what you are seeing.When you do "java Example2", the JVM is initialized and starts up, and the "main(...)" method of class "Example2" is invoked, the result that the Oracle driver is loaded and registered.  However once the main method exits there are no more active threads and so the JVM exits.  Everything you did goes away, and all of your objects are either explicitly garbage collected, or recovered by the OS.If you then do "java Example1", the JVM is once again initialized and starts up again, but nothing that you did before is visible.You need to call the two methods from within the same JVM.  You could invoke the "Example2.main(...)" from within the Example1 "main(...)" method, which is pretty ugly.  Or you could write a third class that calls the two methods in turn (I'd rename the methods to something other than "main") and then start that third class.

   
   
   
   
   
      Hi..I got it. But Tried this and it didnt work.I added main method in Example2 and created a endless loop i.e. while(true). So when I run Example2, it loads the driver and the thread is still alive since main method is having endless loop.Noe I ran the Example1 which lists the drivers. Its not listing the drivers even now.Does that mean whenever I say "java myprogram" it creates a new JVM thread and the class loader for both of them are different ???Thanks for the time.--------------------L G Goundalkar                 lggoundalkar@yahoo.com                 Sun Certified Programmer for Java 2 Platform.Sun Certified Web Component Developer for J2EE.

   
   

 
   
      Every time you execute "java myClass" it starts a new JVM.  And you can't see anything in another JVM unless you explicitly set up some type of remote communication (i.e., sockets or RMI or http or something).So if you type "java myClass" and it runs in a loop, and then type "java myOtherClass", you have two JVMs running and nothing that "myClass" has set up--be it static or not--will be visible in "myOtherClass".

   
   
   
   
   
      Thanks I didnt know that.Well if thats the case then what happens when I deploy servlets? Does it run in different JVM instances on the server? Or the container run in the one JVM instance and sevlets reside in container as child threads..?What about applet? If i have more than one applet on the page does it create more number of instances on the client JRE ?Thanks for the time.--------------------L G Goundalkar                 lggoundalkar@yahoo.com                 Sun Certified Programmer for Java 2 Platform.Sun Certified Web Component Developer for J2EE.

   
   

 
   
      Servlets run inside of a "container".  I tend to use Tomcat, which can be run in a stand-alone mode or part of a larger web server such as Apache.  When you start Tomcat it defines a classpath and starts Java--it's own JVM.  Since lots of servlets can run inside that JVM they can share resouces, such as connections from a pool, beans, etc.I believe that when there are multiple applets running in a web page there is still only one JVM, which is why applets can "talk" to each other.Note that in a distributed model where there are muliple servers handling incoming requests this requires special programming.  You either lock the user [each session] into a single server, or you have to synchronize the session state between the servers.This is a simplistic explanation and perhaps others can add details.  But as I mentioned previously, APIs such as sockets, RMI and HTTP allow data--and in some cases Java objects themselves--to be passed between machines and JVMs.So you could use sockets or RMI to create a server that would dish up database connections, and then a client piece could connect to the server (on a different JVM), get a database connection, and use it to query a database.

   
   
   
   
   
      Thanks Johnson..One more clarification please.In a distributed environment, is it enough for me to serialize the Java Beans which are used by my web components to make the application diastributable? ( NO EJB in my application )Do I get any control to choose between the type of distribution where I can say that in my application the session is bound to a specific application server ( Physical Machine ) or the secodn type where the session data gets persisted and transferred to the other machine.Do I have to worry about the same during design or coding phase of my web application?Or it is one more feature provided by the application server vendor. ?Thanks for the time.--------------------L G Goundalkar                 lggoundalkar@yahoo.com                 Sun Certified Programmer for Java 2 Platform.Sun Certified Web Component Developer for J2EE.


Related Links

Oracle JDBC Driver
ResultSet check if there are any values
Folder Zip Compression in Java
Oracle connection pool thru app server?
Mckoi vs HSQLDB
connecting java with plsql
JDBC Driver
Oracle 8i with NLS=WE8DEC + JSP using Unicode
delete multiple records using checkboxes
ODBC Connection through a batch file
Oracle help
Commons dbcp pooling problems...
SQLQuery
jdbc objects in different JVM's
Taking an argument from a file into SQL prep statement
Exhausted Resultset exception when result count is huge.