Breaking News
yeni film izle hd film seyret film izle

Connecting to a Database

Connecting to a Database

We create an object of the Connection interface to establish a connection of the Java application with a database. We can create multiple Connection objects in a Java application to access and retrieve data from multiple databases. The DriverManager class provides the getConnection ( ) method to create a Connection object.
The getConnection ( ) method is an overloaded method that has following three forms:
1. Connection getConnection(String ): Accept the JDBC URL of the database, which we need tom access , as a parameter.
String url= “jdbc:odbc:MyDSN”;
Connection con =DriverManager.getConnection(url);
The syntax for a JDBC URL that is passed as a parameter to the getConnection ( ) method is:
<protocol>:<subprotocal>:<subname>
JDBC URL has the following three components:
Protocol name: It indicate the name of the protocol that is used to access a database. In JDBC the name of the access protocol is always jdbc.
Sub-protocol name: It indicate the mechanism to retrieve data from a database. For example if we use the JDBC-ODBC Bridge driver to access a database, then the name of sub-protocol is odbc.
Subname: It indicates the Data Source Name(DSN) that contain databases information, such as the name of a database, location of the database server, user name and password to access a database server.
2. Connection getConnection(String , String , String ):
Ex: String url= “jdbc:odbc:MyDSN”;
Driver d =new ;
Connection con =DriverManager.getConnection(url, “ NewUser”, “NewPassword”);
3. Connection getConnection(String , Properties):
Ex:
String url= “jdbc:odbc:MyDSN”;
Properties p new Properties( );
p.setProperty(“user”,”NewUser”);
p.setProperty(“password”,”newPassword”);
Connection con =DriverManager.getConnection(url, p);