QA's approach 2 Java - JDBC API
Java API To Access Relational DB's
What is JDBC?
JDBC refers to the Java Database Connectivity. It provides java API that allows Java programs to access database management systems (relational database). The JDBC API consists of a set of interfaces and classes which enables java programs to execute SQL statements. Interfaces and classes in JDBC API are written in java.
JDBC core components:
The JDBC API consists of the following core components:
1.JDBC Drivers
2.Connections
3.Statements
4.ResultSets
1. JDBC Drivers:
JDBC driver is a collection of classes which implements interfaces defined in the JDBC API for opening database connections, interacting with database and closing database connections.
2. Connections:
Before performing any database operation via JDBC, we have to open a database connection. To open a database connection we can call getConnection() method of DriverManager class.
Syntax: Connection connection = DriverManager.getConnection(url, user, password)
3. Statements:
The JDBC statements are used to execute the SQL or PL/SQL queries against the database. We need a statement for every single query. JDBC API defines the Statement, CallableStatement, and PreparedStatement types of statements.
4. ResultSets:
A query returns the data in the form of ResultSet. To read the query result date ResultSet provides a cursor that points to the current row in the result set.
The following information is required for making connection to the database:
1. Driver class name: is name of the class that implements java.sql.Driver interface. The JDBC’s driver manager needs to load this class in order to work with the database driver.
2. Database URL: a string that contains information about the database to connect to and other configuration properties. This string has its own format and is varied among different databases.
General form of JDBC URL
jdbc:drivertype:driversubtype://params
Sub Type is optional
JDBC URL Examples of commonly used Database:
Driver Name: com.mysql.jdbc.Driver
URL: jdbc:mysql://<server>:<port>/<databaseName>
Eg: jdbc:mysql://localhost:3306/myDBName
Oracle
Driver Name: oracle.jdbc.driver.
URL: OracleDriver jdbc:oracle:thin:@<server>:<port>:<databaseName>
Eg: jdbc:oracle:thin:@localhost:1521:xe
Microsoft SQL Server
Driver Name: com.microsoft.sqlserver.jdbc.SQLServerDriver
URL: jdbc:sqlserver://<server>:<port>/databaseName=<databaseName>
Eg:jdbc:sqlserver://localhost:1433;databaseName=myDBName
MS Access (JDBC-ODBC Bridge)
Driver Name: sun.jdbc.odbc.JdbcOdbcDriver
URL: jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=<myDBName.mdb>;
Eg: jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=myDBName.mdb;
Eg: jdbc:oracle:thin:@localhost:1521:xe
Microsoft SQL Server
Driver Name: com.microsoft.sqlserver.jdbc.SQLServerDriver
URL: jdbc:sqlserver://<server>:<port>/databaseName=<databaseName>
Eg:jdbc:sqlserver://localhost:1433;databaseName=myDBName
MS Access (JDBC-ODBC Bridge)
Driver Name: sun.jdbc.odbc.JdbcOdbcDriver
URL: jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=<myDBName.mdb>;
Eg: jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=myDBName.mdb;
Comments
Post a Comment