Programming in Java | Week 11

Session: JAN-APR 2024

Course name: Programming In Java

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q1. What is the correct order to close database resources?
a. Connection then Statement then ResultSet
b. ResultSet then Statement then Connection
c. Statement then Connection then ResultSet
d. Statement then ResultSet then Connection

Answer: b. ResultSet then Statement then Connection


Q2. How many types of JDBC drivers there?
a. 3
b. 4
c. 8
d. 10

Answer: b. 4


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q3. What is the correct sequence to create a database connection?
i. Import JDBC packages.
ii. Open a connection to the database.
iii. Load and register the JDBC driver.
iv. Execute the statement object and return a query resultset.
v. Create a statement object to perform a query.
vi. Close the resultset and statement objects.
vii. Process the resultset.
viii. Close the connection.

a. i, ii, iii, v, iv, vii, viii, vi
b. i, iii, ii, v, iv, vii, vi, viii
c. ii, i, iii, iv, viii, vii, v, vi
d. i, iii, ii, iv, v, vi, vii, viii

Answer: b. i, iii, ii, v, iv, vii, vi, viii


Q4. Which of the following is correct about connection pooling?
a. Application server like WebLogic, WebSphere, jBoss and Tomcat provides the facilities to configure connection pooling.
b. components like Apache Commons DBCP Component can be used to configure connection pooling.
c. Both of the above.
d. None of the above.

Answer: c. Both of the above.


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q5. Which of the following is used to test the operation?
a. JDBC API
b. JDBC Driver manager
c. JDBC Test suite
d. JDBC-ODBC Bridge Drivers

Answer: c. JDBC Test suite


Q6. The JDBC architecture consists of _________ to access a database.
a. three-tier processing models
b. two-tier processing models
c. two-tier and three-tier processing models
d. None of the above

Answer: c. two-tier and three-tier processing models


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q7. Which of these obtains a Connection?
a. Connection.getConnection(url)
b. Driver.getConnection(url)
c. DriverManager.getConnection(url)
d. new Connection(url)

Answer: c. DriverManager.getConnection(url)


Q8. Which class provides methods to create a client-side socket in Java?
a. ServerSocket
b. NetSocket
c. Socket
d. ClientSocket

Answer: c. Socket


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q9. What does JDBC stand for?
a. Java DataBase Connectivity
b. Java DataBase Connection
c. Java DataBase Control
d. Java DataBase Connector

Answer: a. Java DataBase Connectivity


Q10. Which method can be used to query for a single object using JdbcTemplate?
a. queryForObject()
b. queryForList()
c. query()
d. singleQuery()

Answer: a. queryForObject()


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Programming Assignment

Question 1
The following code is missing some information needed to run the code.
Add whatever is missing and make the code runnable.

Solution:

import java.sql.*;

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Question 2
Write the JDBC codes needed to create a Connection interface using the DriverManager class and the variable DB_URL.
Check whether the connection is successful using ‘isAlive(timeout)’ method to generate the output, which is either ‘true’ or ‘false’.
Note the following points carefully:
§ Name the connection object as conn only.
§ Use timeout value as 1.

Solution:

conn = DriverManager.getConnection(DB_URL);
System.out.print(conn.isValid(1));

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Question 3
Due to some mistakes in the below code, the code is not compiled/executable.
Modify and debug the JDBC code to make it execute successfully.

Solution:

import java.sql.*;
import java.util.Scanner;
 
public class W11_P3 {
    public static void main(String args[]) {
        try {
              Connection conn = null;
              Statement stmt = null;
              String DB_URL = "jdbc:sqlite:/tempfs/db";
              System.setProperty("org.sqlite.tmpdir", "/tempfs");
              conn = DriverManager.getConnection(DB_URL);
              conn.close();
              System.out.print(conn.isClosed());

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Question 4
Complete the code segment to create a new table named ‘STUDENTS’ in SQL database using the following information.

Solution:

String CREATE_TABLE_SQL="CREATE TABLE STUDENTS (UID INT, Name VARCHAR(45), Roll VARCHAR(12), Age INT);";
stmt.executeUpdate(CREATE_TABLE_SQL);

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Question 5
Complete the code segment to rename an already created table named ‘STUDENTS’ into ‘GRADUATES’.

Solution:

String alter="ALTER TABLE STUDENTS RENAME TO GRADUATES;";
stmt.executeUpdate(alter);

For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 11 Assignment 11 Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: https://progiez.com/nptel-assignment-answers


Session: JULY-DEC 2023

Course Name: Programming In Java

Course Link: Click Here

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Programming Assignment

Question 1
Complete the code segment to insert the following data using prepared statement in the existing table ‘PLAYERS’.

ColumnUIDFirst_NameLast_NameAge
Row 11RamGopal26
Row 22JohnMayer22

Solution:

String query= "insert into PLAYERS(UID,first_name,last_name,age)" + "values (?, ?, ?, ?)";

PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt (1, 1);
preparedStmt.setString (2, "Ram");
preparedStmt.setString (3, "Gopal");
preparedStmt.setInt(4, 26);

preparedStmt.execute();

PreparedStatement preparedStmt2 = conn.prepareStatement(query);
preparedStmt2.setInt (1, 2);
preparedStmt2.setString (2, "John");
preparedStmt2.setString (3, "Mayer");
preparedStmt2.setInt(4, 22);

preparedStmt2.execute();

Question 2
Write the required code in order to update the following data in the table ‘PLAYERS’.

ColumnUIDFirst_NameLast_NameAge
From1RamGopal26
To1RamaGopala24

Solution:

query="UPDATE Players SET First_name='Rama',Last_name='Gopala',Age=24 WHERE UID=1;";
stmt.executeUpdate(query);

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Question 3
Write the appropriate code in order to delete the following data in the table ‘PLAYERS’.

ColumnUIDFirst_NameLast_NameAge
Delete1RamaGopala24

Solution:

stmt.executeUpdate("DELETE FROM Players WHERE UID = 1;");

Question 4
Complete the following program to calculate the average age of the players in the table ‘PLAYERS’.
Structure of Table ‘PLAYERS’ is given below:

ColumnUIDFirst_NameLast_NameAge
TypeIntegerVarchar (45)Varchar (45)Integer

Solution:

ResultSet rs = stmt.executeQuery("SELECT * FROM players;");
int count=0,total=0;
while(rs.next()){
	count++;
	total = total + Integer.parseInt(rs.getString(4));
}

System.out.println("Average age of players is " +(total/count));
conn.close();

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Question 5
Complete the code segment to drop the table named ‘PLAYERS’.

Solution:

query = "DROP TABLE players;";
stmt.executeUpdate(query);

These are NPTEL Programming In Java Week 11 Assignment 11 Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: Click here


Session: JAN-APR 2023

Course Name: Programming in Java

Course Link: Click Here

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q1. Which of the following interfaces is used to manage transactions in JDBC?
a. Connection
b. Statement
c. Transaction
d. ResultSet

Answer: a. Connection


Q2. Which of the following interfaces is used to execute parameterized SQL statements in JDBC?
a. ResultSet
b. PreparedStatement
c. Statement
d. Connection

Answer: b. PreparedStatement


These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q3. Which of the following statements is true?
a. The executeQuery( ) method of java.sql.Statement interface is used to execute a DELETE statement.
b. The executeQuery( ) method of java.sql.Statement interface is used to execute a SELECT statement.
c. The executeQuery( ) method of java.sql.Statement interface is used to execute a INSERT statement.
d. The executeQuery( ) method of java.sql.Statement interface is used to execute a UPDATE statement.

Answer: b. The executeQuery( ) method of java.sql.Statement interface is used to execute a SELECT statement.


Q4. Which of the following statements is true?
a. The executeUpdate( ) method of java.sql.Statement interface is used to execute a DELETE statement.
b. The executeUpdate( ) method of java.sql. Statement interface is used to execute a SELECT statement.
c. The executeUpdate( ) method of java.sql.Statement interface is used to execute a INSERT statement.
d. The executeUpdate( ) method of java.sql.Statement interface is used to execute a UPDATE statement.

Answer: a, c, d


These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q5. Which of the following statements is true about JDBC?
a. JDBC is a programming language.
b. JDBC is a type of database.
c. JDBC is an API for accessing relational databases from Java programs.
d. JDBC is used to create graphical user interfaces.

Answer: c. JDBC is an API for accessing relational databases from Java programs.


Q6. Which of the following statements is true about the PreparedStatement interface in JDBC?
a. PreparedStatement objects are precompiled before they are executed.
b. PreparedStatement objects can only be used for SELECT statements.
c. PreparedStatement objects cannot accept parameters at runtime.
d. PreparedStatement objects can be reused for multiple SQL statements.

Answer: a, d


These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q7. Which of the following statements is true about batch updates in JDBC?
a. Batch updates can only be used for INSERT statements.
b. Batch updates allow multiple SQL statements to be executed in a single transaction.
c. Batch updates are executed immediately as soon as they are added to the batch.
d. Batch updates can be rolled back.

Answer: b, d


Q8. Consider the following code.
What is the output of the above code?

a. Compilation error
b. Runtime error
c. 1, “one” is replaced by 2.”two™ in the table.
d. “one” and “two” both are inserted in different columns of same row.

Answer: d. “one” and “two” both are inserted in different columns of same row.


These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Q9. Which type of JDBC driver translates JDBC calls into native database API calls?
a. Type 1 driver
b. Type 2 driver
c. Type 3 driver
d. Type 4 driver

Answer: b. Type 2 driver


Q10. Which of the following method is static and synchronized in JDBC API?
a. getConnection( )
b. prepareCall()
c. executeUpdate( )
d. executeQuery()

Answer: a. getConnection( )


Programming Assignment of Programming in Java

Question 1

Complete the code segment to insert the following data using prepared statement in the existing table ‘PLAYERS.

Solution:

PreparedStatement preparedStmt = conn.prepareStatement ("insert into Players values(?,?,?,?)");
preparedStmt.setInt (1, 1);
preparedStmt.setString (2, "Ram");
preparedStmt.setString (3, "Gopal");
preparedStmt.setInt(4, 26);

int xxx= preparedStmt.executeUpdate();

PreparedStatement preparedStmt2 = conn.prepareStatement ("insert into Players values(?,?,?,?)");

preparedStmt2.setInt (1, 2);
preparedStmt2.setString (2, "John");
preparedStmt2.setString (3, "Mayer");
preparedStmt2.setInt(4, 22);

int xyz= preparedStmt2.executeUpdate();

Question 2

Write the required code in order to update the following data in the table ‘PLAYERS’.

Solution:

String a1="update players "+"set AGE =24 WHERE UID==1";
stmt.execute(a1);

String bb="update players "+"set LAST_NAME ='Gopala' WHERE UID==1";
stmt.execute(bb);

String c_z="update players "+"set FIRST_NAME='Rama' WHERE UID==1";
stmt.execute(c_z);

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Question 3

Write the appropriate code in order to delete the following data in the table ‘PLAYERS’.

Solution:

String My_fav_Query="DELETE FROM PLAYERS "+"WHERE UID=1";
  stmt.executeUpdate(My_fav_Query); 

Question 4

Complete the following program to calculate the average age of the players in the table ‘PLAYERS.

Solution:

ResultSet ans=stmt.executeQuery("SELECT * from players");

int ccdd=0;
int sum_is_sum=0;
while(ans.next())
{
  ccdd++;
  sum_is_sum+=Integer.parseInt (ans.getString(4));
}

System.out.println("Average age of players is " + sum_is_sum/ccdd);
  conn.close();

These are NPTEL Programming In Java Week 11 Assignment 11 Answers


Question 5

Complete the code segment to drop the table named ‘PLAYERS.

Solution:

query = "DROP TABLE players;";
stmt.executeUpdate(query);

These are NPTEL Programming In Java Week 11 Assignment 11 Answers

More Weeks of Programming In Java: Click Here

More Nptel courses: https://progiez.com/nptel-assignment-answers/


These are NPTEL Programming In Java Week 11 Assignment 11 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.