Informatics Practices Class 12 Important Questions Chapter 7 Accessing MySQL Database Using ODBC/JDBC

Informatics Practices Class 12 Important Questions Chapter 7 Accessing MySQL Database Using ODBC/JDBC is part of Informatics Practices Class 12 Important Questions. Here we have given Informatics Practices Class 12 Important Questions Chapter 7 Accessing MySQL Database Using ODBC/JDBC.

Informatics Practices Class 12 Important Questions Chapter 7 Accessing MySQL Database Using ODBC/JDBC

1 Mark Questions

Question 1.
What do you mean by database connectivity?
Answer:
Database connectivity refers to connection and communication between an application and database system.

Question 2.
Define ResultSet.
Answer:
A result set (represented by a ResultSet object) refers to a logical set of records that are fetched from the database by executing a query and made available to the application program.

Question 3.
Which method is used to retrieve an int type data from a ResultSet? (HOTS)
Answer:
To retrieve an int type data from a ResultSet getlnt() method is used.

Question 4.
Differentiate between executeQuery() method and executel)pdate() method.
Answer:
The execute query method is used to execute SQL-SELECT queries whereas non-SELECT queries are executed through the executeUpdate() method.

Question 5.
Which method is used to retrieve a date type data from a ResultSet?
Answer:
To retrieve a date type data from a ResultSet. getDate() method is used.

Question 6.
Explain the use of DBC Connection class.
Answer:
This class has many methods. Some of the commonly used methods are given below:
(i) deregisterDriver(Driver driver) It drops the driver from the list of drivers registered in the DriverManager class.
(ii) registerDriver(Driver driver) It registers the driver in the DriverManager class. getConnection(String url) It establishes the connection to a given database URL.
(iii) getConnection(String URL, String user, String password) It establishes the connection to a given database URL, with appropriate username and password.
(iv) getConnection(String url, Properties info) It establishes the connection to a given database URL, as per passed driver properties.
(v) getDriver(String url) It attempts to locate the driver by the given string.
(vi) getDrivers() It retrieves the enumeration of the drivers, which has been registered with the DriverManager class.

Question 7.
Expand the term JDBC and ODBC.
Answer:
JDBC stands for Java DataBase Connectivity. ODBC stands for Open DataBase Connectivity.

Question 8.
Which method is used to fetch a string value from a ResultSet?
Answer:
To retrieve a string value from a ResultSet getString() method is used.

2 Marks Questions

Question 9.
What tasks are performed by JDBC?
Answer:
JDBC primarily performs the following tasks:

  • Establishing a connection with a database.
  • Sending SQL statement to database server.
  • Processing the results obtained through the SQL query.

Question 10.
Which JDBC classes/objects are used in database connectivity applications? (HOTS)
Answer:
DriverManager Class, Connection object, Statement object, ResultSet object are used in database connectivity applications.

Question 11.
Explain the use of next(), first() and last() methods in a ResultSet.
Answer:

  1. next( ) Moves the cursor in forwarding direction one row. If the cursor is in the last row, it will return false.
  2. first( ) Moves the cursor to the first row in a ResultSet object. If the cursor is already present in the first row, it will return false.
  3. last( ) Moves the cursor to the last row in the ResultSet object. If the cursor is already present in the last row, it will return false.

Question 12.
How can we execute a query? (HOTS)
Answer:
Execute a Query To execute a query, we need to create an object of a type Statement for building and submitting an SQL statement to the database using createStatement() method of Connection type object.

Statement stmt = conn.createStatement( );

Thereafter, we need to execute the SQL statement using executeQuery() method, which returns a ResultSet that contains the resultant dataset of the executed query.
E.g.,

String MyString;
MyString = “SELECT roll, name, age FROM STUDENT";
ResultSet rs = stmt.executeQuery(MyString );

It however, the SQL statement is an UPDATE, INSERT or DELETE statement that you can execute SQL query using executeUpdate() method,
E.g.

Statement stmt = conn.createStatement( );
String MyString;
MyString = “DELETE FROM STUDENT";
ResultSet rs = stmt.executeUpdate(MyString );

Question 13.
What is the use of DriverManager Class?
Answer:
DriverManager Class
The JDBC DriverManager class loads the JDBC driver needed to access a particular data source, located and logs on to the database and returns a Connection object.

The DriverManager class works between the user and the drivers. The task of the DriverManager class is to keep track of the drivers that are available and handles in establishing a connection between a database and the appropriate driver. It even keeps track of the driver login time limits and printing of log and tracing messages. This class is mainly useful for simple application. The most frequently used methods of this class are DriverManager. getConnection( ). We can know by the name of the method, that this method establishes a connection to a database. The DriverManager class maintains the list of the Driver classes. Each driver has to get registered in the DriverManager class by calling the method DriverManager.registerDriver().

The driver is loaded by calling the Class.forName() method. JDBC drivers are designed to tell the DriverManager about themselves automatically when their driver implementation class gets loaded.

This class has many methods. Some of the commonly used methods are given below:
(i) deregisterDriver(Driver driver)
It drops the driver from the list of drivers registered in the DriverManager class.
(ii) registerDriver(Driver driver) It registers the driver in the DriverManager class. getConnection(String url) It establishes the connection to a given database URL.
(iii) getConnection(String URL, String user, String password) It establishes the connection to a given database URL, with appropriate username and password.
(iv) getConnection(String url, Properties info) It establishes the connection to a given database URL, as per passed driver properties.
(v) getDriver(String url) It attempts to locate the driver by the given string.
(vi) getDrivers() It retrieves the enumeration of the drivers, which has been registered with the DriverManager class.

Question 14.
How can you create a JDBC statement object?
Answer:

Class.forName("java.sql.Driver") ;
Class.forName(“com.mysql.jdbc.Driver");

Question 15.
What does getRow() method do?
Answer:
getRow() method retrieves a specified number of rows from a recordset and to fill an array with the resulting data.

Question 16.
Write all six steps for creating Database Connection Application.
Answer:
Step 1 Import the Package Required for Database Programming First of all, we need to import the library package containing the JDBC classes, i.e. Connection, DriverManager, Statement and ResultSet needed for database programming. For this purpose, we need to write the following statements:

(i) import java.sql. Connection;
(ii) import java.sql. DriverManager;
(iii) import java.sql. Statement;
(iv) import java.sql. ResultSet;

However even we can write import java.sql.*; to import the entire package. Thereafter, we need to add MySQL JDBC connector from the projects window.

Step 2 Register the JDBC Driver To register the JDBC Driver, we initialize a driver to open a communication channel with the database from the Java application. For connecting to MySQL,
we need to write one of the following statements:

Class.forName("java.sql.Driver") ; or
Class.forName(“com.mysql.jdbc.Driver");

Step 3 Open a Connection To open a Connection, we need to use the DriverManager.getConnection( ) method and create a connection object, which represent the physical connection with the database, that allows us to establish a physical connection to the data source for open a connection, we need to write following statement:

Connection conn = DriverManager.getConnection(DB_URL,USERNAME,PASSWORD);

Step 4 Execute a Query To execute a query, we need to create an object of a type Statement for building and submitting an SQL statement to the database using createStatement() method of Connection type object.

Statement stmt = conn.createStatement( );

Thereafter, we need to execute the SQL statement using execute Query() method, which returns a ResultSet that contains the resultant dataset of the executed query.
E.g.,

Stri ng MyStri ng ;
MyString = “SELECT roll, name, age FROM STUDENT";
ResultSet rs = stmt.executeQuery(MyString ) ;

It, however, the SQL statement is an UPDATE, INSERT or DELETE statement that you can execute SQL query using executeUpdate() method,
E.g.,

Statement stmt = conn.createStatement( );
String MyString;
MyString = “DELETE FROM STUDENT";
ResultSet rs = stmt.executeUpdate(MyString );

Step 5 Extract Data from ResultSet Extraction of data is required, if we are fetching data from the database, using SQL SELECT query. We can use the appropriate ResultSet.get() method to retrieve the data from the desired resultset.
The ResultSet object provide several methods, some example of these methods include getlnt(), getLong(), getString(), getFloat(), getDate(), etc.
E.g.

int roll = rs.getInt(“roll");
String name = rs.getString(“name" ) ;
int age = rs.getlnt(“age");

Step 6 Clean up the Environment At last we have to clean the environment. For this purpose, we have to use the close( ) method to physically close all the database resources.

rs. close( );
stmt. close( );
conn.close( );

Question 17.
What will Class.forName do while loading Drivers?
Answer:
It is used to create an instance of a driver and register it with DriverManager. After loading the driver, DriverManager instance is available for making a connection with a DBMS.

Question 18.
Name the packages and classes that must be included as part of your java application for database connectivity to work. (HOTS)
Answer:

Package-java.sql
Classes-java.sql.Connection
java.sql.Statement
java.sql.ResultSet

Question 19.
Which command is used to import an entire package?
Answer:
To import the entire package the

“import java.sql.*;" command is used.

Question 20.
Name the class, which is working between the user and the drivers?
Answer:
The DriverManager class works between the user and drivers.

Question 21.
Name the various parameters required to open a JDBC connection.
Answer:
There are four parameters required to open a JDBC connection:

  • Database URL
  • JDBC driver
  • User Name
  • Password

Question 22.
Write a code to return the total number of rows in a ResultSet.
Answer:
Code to return the total number of rows in a ResultSet is given below:

String sql = “SELECT From Employees";
ResultSet rs = stmt.execute Query
(sql );
rs.1ast ();
int r = rs.getRow( ) ;
JOptionPane.showMessageDialog
(null , " . "+r);

Question 23.
Discuss the various methods of Resultset.
Answer:
When a database is connected and data fetched into it, a ResultSet is maintained. A ResultSet object maintains a cursor, which points to its current row of data.
(i) next( ) Moves the cursor in forwarding direction one row. If the cursor is in the last row, it will return false.
(ii) first() Moves the cursor to the first row in a ResultSet object. If the cursor is already present in the first row, it will return false.
(iii) last( ) Moves the cursor to the last row in the ResultSet object. If the cursor is already present in the last row, it will return false.
(iv) relative(int rows) Moves the cursor relatively, e.g. if the cursor is already present in the 4th row and we apply the relative(2), then it will place the cursor in the 6th row.
(v) absolute(int rno) Moves the cursor on the specified number of row, irrespective of the current cursor position.
(vi) getRow() Retrieve a specified number of rows from a ResultSet.

We hope the Informatics Practices Class 12 Important Questions Chapter 7 Accessing MySQL Database Using ODBC/JDBC help you. If you have any query regarding Informatics Practices Class 12 Important Questions Chapter 7 Accessing MySQL Database Using ODBC/JDBC, drop a comment below and we will get back to you at the earliest.

<!–

–>