In a JDBC program, one of the first things to do is to load the JDBC driver by calling the forName() static method of the Class class. forName() takes one string parameter: the name of the driver along with its package. For example connect to SQL Server using JTDS driver. Therefore, the call would look like:
1. Load Driver
Class.forName("net.sourceforge.jtds.jdbc.Driver"); |
2. Establish a database connection
Once the JDBC driver loads, you can establish a connection to the database using the DriverManager.getConnection() method. This method's first argument is a string that contains the JDBC URL for the database. The second and third parameters are the user name and password, respectively.
A JDBC URL is formulated using the following pattern:
jdbc:
A connection to the database could be established in this manner:
String URL = "jdbc:jtds:sqlserver://localhost/Northwind"; Connection con = DriverManager.getConnection(URL, "sa", "secret"); |
After connection successfully, you can Insert, Update, Delete, Select or other manipulation into database.