- What is Java?
- History of Java
- Hello Java Program
- Features of Java
- Basic Syntax
- Java Setup
- Data Types in Java
- Java Variables
- Operators in Java
- Java If-else Statement
- Switch Case Statement
- Java Loops
- Do-While Loop
- Java While Loop
- Continue Statement
- Break Statement in Java
- Constructors in Java
- Oops Concept in Java
- Features of OOPs
- Exeception handeling
- Aggregation (HAS-A relationship) in Java
- Method Overriding in Java
- Method Overloading
- Java Static Keyword
- Java This Keyword
- Java Final Keyword
- Static Binding and Dynamic Binding
- Abstract class in Java
- Access Modifiers in Java
- Difference between abstract class and interface
- Interface in Java
- Garbage Collection in Java
- Java Package
- Serialization and Deserialization in Java
- Java Inner Classes
- Java Applets
- Multithreading in Java
- Thread Priorities in Java
- Thread Creation
- Inter Thread Communication
- Wrapper Classes in Java
- Java Input Output
- Java AWT Introduction
- Java Layout Manager
- Java Layout Policy
- Java AWT Events
- Collection Framework
- Collection Framework List Interface
- Swing in Java
- Swing Utility Classes
- Swing Layout Managers
- Java JDBC
Connecting to a Database
Here are the steps that we need to follow to connect to a database.
- Obtain the JDBC driver and add it to the CLASSPATH environment variable on your machine.
- Register the JDBC driver with the DriverManager.
- Construct a connection URL.
- Use the getConnection() static method of DriverManager to establish a connection.
The following sections describe these steps in detail.
Obtaining the JDBC Driver
You need to have the JDBC driver for your database before you can connect to the database using JDBC. You can geta JDBC driver from the vendor of your database. For example, if you are using the Oracle DBMS, you can downloadthe JDBC driver from its official web site at www.oracle.com. All database vendors that support JDBC will let youdownload the JDBC driver for their DBMS from their official web sites for free. Typically, a JDBC driver is bundled inone or more JAR/ZIP files.If you are using Java DB, the JDBC drivers were copied on your machine when you installed the JDK. You do notneed to download any additional JDBC drivers.
Setting up the CLASSPATH
If you are using a JDBC driver, you need to place the JAR/ZIP files for your JDBC driver in the CLASSPATH on yourmachine, so that your Java programs can use the Java classes that implement the JDBC driver for your database.Thederby.jar file is the JDBC driver needed to use the Java DB in embedded mode. If you are connecting to Java DB overa network, you will need to include the derbyclient.jar file in the CLASSPATH.
Registering a JDBC Driver
You need to register a JDBC driver, which you want to use to connect to a database. A JDBC driver is registered withthe java.sql.DriverManager class.
What is a JDBC driver?
Technically, a JDBC driver is a class that implements the java.sql.Driver interface.DBMS vendors supply the JDBC driver class along with any other classes it uses. You must know the name of the JDBCdriver class before you can register it with the DriverManager class. If you do not know the name of the driver class,please refer to the documentation of the JDBC driver for your DBMS.In the next section, I will list the driver class names for some DBMSs. The name may vary depending on theversion of DBMS or the supplier of the driver class. Sometimes different vendors supply the drivers for the sameDBMS. Different vendors will use different driver class names and different connection URL formats to connect to thesame DBMS.
Why do you need to register a JDBC driver with the DriverManager
Java does not know how to connect toa database. It depends on the JDBC driver to connect to a database. Think of a JDBC driver as a Java class whoseobject will be used by the DriverManager to connect to a database. The question is, “How does the DriverManagerknow about the JDBC driver you want to use to connect to a database?” Of course, it has no way to know about theJDBC driver by itself. Therefore, registering a driver with the DriverManager is simply telling the DriverManagerabout your JDBC driver class name. By registering a JDBC driver, you are telling the DriverManager that if you askthe DriverManager to establish a connection to a database, it needs to try using this driver. Can you register multipleJDBC drivers with the DriverManager? Yes. You can register multiple JDBC drivers. When you need to establish aconnection to a database, you must pass a connection URL to the DriverManager. The DriverManager passes theconnection URL to all registered drivers one by one, and asks them to connect to the database using informationthat you supply in the connection URL. If a driver recognizes the connection URL, it connects to the database andreturns the connection to the DriverManager. An object of the java.sql.Connection interface represents a databaseconnection in a Java program. If none of the registered drivers recognize a connection URL, the DriverManager willthrow a SQLException stating that it could not find a suitable driver.
There are three ways to register a JDBC driver with the DriverManager:
- By setting the jdbc.drivers system property
- By loading the driver class into the JVM
- By using the registerDriver() method of the DriverManager class
Setting the jdbc.drivers System Property
You can register a JDBC driver class name using the jdbc.drivers system property. You can set this property in yourcomputer globally; you can pass this property on the command line when you run your application, or you can set thisproperty in your application using the System.setProperty() method. A colon separates each driver to be registered.
Here are some examples:
//Register Sybase and Oracle drivers in the Java code
String drivers = “com.sybase.jdbc2.jdbc.SybDriver:oracle.jdbc.driver.OracleDriver”;System.setProperty(“jdbc.drivers”, drivers);
//Pass driver names to be registered as command-line arguments.
//The following command is entered in one line.
Loading the Driver Class
You can create an object of the driver class. When the driver class is loaded in the JVM, it registers itself with theDriverManager. For a class to be loaded, you can use a Class.forName(“driver class name”) method or create anobject of the class as follows:
// Register the Oracle JDBC driver
// Register the Oracle JDBC driver using the Class.forName() method.
// Exception handling has been omitted.
// Register the Java DB embedded driver
// Register the Java DB network client driver
You do not need to keep the reference of the driver object because the goal is to load the driver class in the JVM.
When the driver’s class is loaded into the JVM, the static initializer of the driver’s class is executed in which the driver
class registers itself with the DriverManager.