Programming in Java | Week 11

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.
Home
Account
Cart
Search