Microsoft SQL Server
Crystal Reports
Delphi, C etc
Perl and the DBI
Unix Shell Scripts
Visual Basic
Corel Paradox
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 Plugins
Project jMaki
GlassFish WebTier
Java Development Tools
Java WS & XML Community News
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
Mobile Developer Alliance
JavaFX Script Language Discussion
OpenJFX General Discussion
Scene Graph
General Performance Discussion
Your Java Career
NetBeans 6.0
Portals and Portlets
EJB and Other Java EE Technologies
Distributed Java
Object Relational Mapping
Web Services
Swing / AWT / SWT / JFace
JNLP and Web Start
Java Micro Edition
Sockets and Internet Protocols
Threads and Synchronization
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
Bunkhouse Porch
Teachers' Lounge
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
Other Languages
Application Frameworks
Other Open Source Projects
Other Java Products and Servers
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
Threads and Synchronization
Java 2D
AWT / Swing
SWT / JFace
CDC and Personal Profile
Sun Java Wireless Toolkit
Enterprise JavaBeans
JavaServer Pages (JSP) and JSTL
Java Servlet
JavaServer Faces
Web Frameworks
IntelliJ IDEA
Other IDEs
Java Tutorials
Java Tips
Jobs Discussion
Jobs Offered
Jobs Wanted
Professional Certification
Forum Lobby
Java Blogs
Reviews / Advertising
Suggestions & Feedback


Java Database
Software Development
Software Development



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                        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                        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, "" and "".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                        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                        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                        Sun Certified Programmer for Java 2 Platform.Sun Certified Web Component Developer for J2EE.

Related Links

HF-SQL and Stored Procedures
Finding duplicates
Head First SQL query optimizer
JDBC related certification?
SQL injection?
Head First SQL: specific relational database system?
Error registering a driver to mysql db when using Tomcat
Why do I need SQL when I have Excel?
Does HF SQL Covers Advaced SQL?
Teaching aids for Head First Books
Does HF SQL cover anything on Object Oriented SQL
Book distribution for HF SQL
HF - SQL Inner and Outer Joins
Does Head First SQL address complex subjects?
HF SQL and Normalizing
TCP/IP SOCKETS T4Agent.sendRequest() OutputStream.flush() Broken pipe * 0