Important Questions CC109 - BIC1224
Important Questions CC109 - BIC1224
● List the required FIVE steps for building the JDBC application and explain each step.
ANS:
The following are the five steps involved in building a JDBC application:
i. Load the JDBC driver: The first step in building a JDBC application is to load the
appropriate JDBC driver that corresponds to the database management system
(DBMS) being used. The driver can be loaded by calling the Class.forName()
method and passing the fully qualified name of the driver class as a string
parameter. This step is necessary to establish a connection between the Java
program and the database server.
ii. Establish a connection to the database: After loading the driver, the next step is to
establish a connection to the database by calling the
DriverManager.getConnection() method and passing the database URL, username,
and password as parameters. The database URL is a string that identifies the
location of the database server, including the protocol, host name, port number,
and database name.
iii. Create a statement object: Once a connection is established, a Statement object
needs to be created to execute SQL statements against the database. This can be
done by calling the Connection.createStatement() method. The Statement object
can be used to execute queries, updates, and deletes on the database.
iv. Execute the SQL statement: After creating a Statement object, the next step is to
execute the SQL statement using one of the Statement object's execute()
methods. The execute() method can be used to execute any SQL statement,
including SELECT, INSERT, UPDATE, and DELETE.
v. Process the result set: If the SQL statement is a SELECT statement, the
executeQuery() method should be used instead of execute(). The executeQuery()
method returns a ResultSet object that contains the results of the query. The
ResultSet object can be processed using methods like next(), getString(), and
getInt() to retrieve data from the result set.
try {
} catch (OutOfMemoryError e) {
System.out.println("Caught OutOfMemoryError!");
● Based on the java code above describe how the program executes when a user enters a
number?
ANS:
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("You entered: " + number);
scanner.close();
}
}
● Write a program for calculating the equation of Y= (X1+X2)/D where the user can enter
the
values of X1, X2, and D after execution and the program can detect the mismatched
values
and the wrong value for variable D.
ANS:
import java.util.InputMismatchException;
import java.util.Scanner;
try {
// Read input values from user
System.out.print("Enter value for X1: ");
double x1 = scanner.nextDouble();
// Display result
System.out.println("The result of the equation is: " + y);
} catch (InputMismatchException e) {
// Handle mismatched input values
System.out.println("Invalid input! Please enter numbers only.");
} catch (ArithmeticException e) {
// Handle division by zero error
System.out.println("Invalid input! The value of D cannot be zero.");
} finally {
// Close the scanner to prevent resource leak
scanner.close();
}
}
}
● Given the UML diagram shown in Figure 1, write complete JAVA program to create the
class. (7 marks)
Rectangle
- height: double
- width: double
+Rectangle (height:double, width: double)
+getHeight ( ): double
+getWidth ( ): double
+calcArea ( ): void