NPTEL Programming in Java Week 11 All Programming Assignment Solutions | Swayam. With the growth of Information and Communication Technology, there is a need to develop large and complex software.
Further, those software should be platform independent, Internet enabled, easy to modify, secure, and robust. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the Java programming language emerges as the best programming environment.
Now, Java programming language is being used for mobile programming, Internet programming, and many other applications compatible to distributed systems.
This course aims to cover the essential topics of Java programming so that the participants can improve their skills to cope with the current demand of IT industries and solve many problems in their own filed of studies.
COURSE LAYOUT
- Week 1 : Overview of Object-Oriented Programming and Java
- Week 2 : Java Programming Elements
- Week 3 : Input-Output Handling in Java
- Week 4 : Encapsulation
- Week 5 : Inheritance
- Week 6 : Exception Handling
- Week 7 : Multithreaded Programming
- Week 8 : Java Applets and Servlets
- Week 9 : Java Swing and Abstract Windowing Toolkit (AWT)
- Week 10 : Networking with Java
- Week 11: Java Object Database Connectivity (ODBC)
- Week 12: Interface and Packages for Software Development
Course Name : “Programming in Java”
Question : 1 Complete the code segment to insert the following data using prepared statement in the existing table ‘PLAYERS’.
import java.sql.*;
import java.lang.*;
public class InsertData {
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");
// Open a connection
conn = DriverManager.getConnection(DB_URL);
stmt = conn.createStatement();
// The statement containing SQL command to create table "players"
String CREATE_TABLE_SQL="CREATE TABLE players (UID INT, First_Name VARCHAR(45), Last_Name VARCHAR(45), Age INT);";
// Execute the statement containing SQL command
stmt.executeUpdate(CREATE_TABLE_SQL);
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();
preparedStmt.setInt (1, 2);
preparedStmt.setString (2, "John");
preparedStmt.setString (3, "Mayer");
preparedStmt.setInt(4, 22);
preparedStmt.execute();
//Evaluation Portion
ResultSet rs = stmt.executeQuery("SELECT * FROM players;");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
conn.close();
}catch(Exception e){ System.out.println(e);}
}
}
Question : 2 Write the required code in order to update the following data in the table ‘PLAYERS’.
import java.sql.*;
import java.lang.*;
public class UpdateData {
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");
String query="";
// Open a connection
conn = DriverManager.getConnection(DB_URL);
stmt = conn.createStatement();
// The statement containing SQL command to create table "players"
String CREATE_TABLE_SQL="CREATE TABLE players (UID INT, First_Name VARCHAR(45), Last_Name VARCHAR(45), Age INT);";
// Execute the statement containing SQL command
stmt.executeUpdate(CREATE_TABLE_SQL);
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();
preparedStmt.setInt (1, 2);
preparedStmt.setString (2, "John");
preparedStmt.setString (3, "Mayer");
preparedStmt.setInt(4, 22);
preparedStmt.execute();
// write SQL command to update table data
query = " UPDATE Players SET First_name ='Rama',Last_name = 'Gopala',Age = 24 WHERE UID=1;";
// Execute the SQL command to update the table data
stmt.executeUpdate(query);
ResultSet rs = stmt.executeQuery("SELECT * FROM players;");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
conn.close();
}
catch(Exception e){ System.out.println(e);}
}
}
Question : 3 Write the appropriate code in order to delete the following data in the table ‘PLAYERS’.
import java.sql.*;
import java.lang.*;
public class DeleteData {
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");
String query="";
// Open a connection
conn = DriverManager.getConnection(DB_URL);
stmt = conn.createStatement();
String CREATE_TABLE_SQL="CREATE TABLE players ( UID INT, first_name VARCHAR(45), last_name VARCHAR(45), age INT);";
stmt.executeUpdate(CREATE_TABLE_SQL);
query = " insert into Players (UID, first_name, last_name, age)" + " values (?, ?, ?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt (1, 1);
preparedStmt.setString (2, "Rama");
preparedStmt.setString (3, "Gopala");
preparedStmt.setInt(4, 24);
preparedStmt.execute();
preparedStmt.setInt (1, 2);
preparedStmt.setString (2, "John");
preparedStmt.setString (3, "Mayer");
preparedStmt.setInt(4, 22);
preparedStmt.execute();
// Execute the command to delete a row form the table
stmt.executeUpdate("DELETE FROM Players WHERE UID = 1;");
ResultSet rs = stmt.executeQuery("SELECT * FROM players;");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
conn.close();
}
catch(Exception e){ System.out.println(e);}
}
}
Question : 4 Complete the following program to calculate the average age of the players in the table ‘PLAYERS’.
import java.sql.*;
import java.lang.*;
public class CalAverage {
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");
String query="";
// Open a connection
conn = DriverManager.getConnection(DB_URL);
stmt = conn.createStatement();
String CREATE_TABLE_SQL="CREATE TABLE players ( UID INT, first_name VARCHAR(45), last_name VARCHAR(45), age INT);";
stmt.executeUpdate(CREATE_TABLE_SQL);
query = " insert into Players (UID, first_name, last_name, age)" + " values (?, ?, ?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt (1, 1);
preparedStmt.setString (2, "Rama");
preparedStmt.setString (3, "Gopala");
preparedStmt.setInt(4, 24);
preparedStmt.execute();
preparedStmt.setInt (1, 2);
preparedStmt.setString (2, "John");
preparedStmt.setString (3, "Mayer");
preparedStmt.setInt(4, 22);
preparedStmt.execute();
preparedStmt.setInt (1, 3);
preparedStmt.setString (2, "Leo");
preparedStmt.setString (3, "Martin");
preparedStmt.setInt(4, 27);
preparedStmt.execute();
ResultSet rs = stmt.executeQuery("SELECT * FROM players;");
int count=0,total=0;
while(rs.next()){
count++;
total = total + Integer.parseInt(rs.getString(4));
}
//Output
System.out.println("Average age of players is " +(total/count));
conn.close();
}
catch(Exception e){ System.out.println(e);}
}
}
Question : 5 Complete the code segment to drop the table named ‘PLAYERS’.
import java.sql.*;
import java.lang.*;
public class DropTable {
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");
String query="";
// Open a connection
conn = DriverManager.getConnection(DB_URL);
stmt = conn.createStatement();
// The statement containing SQL command to create table "players"
String CREATE_TABLE_SQL="CREATE TABLE players (UID INT, First_Name VARCHAR(45), Last_Name VARCHAR(45), Age INT);";
// Execute the statement containing SQL command
stmt.executeUpdate(CREATE_TABLE_SQL);
// Write the SQL command to drop a table
query = "DROP TABLE players;";
// Execute the SQL command to drop a table
stmt.executeUpdate(query);
ResultSet rs = stmt.executeQuery("SELECT * FROM players;");
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("No. of columns : " + rsmd.getColumnCount());
conn.close();
}
catch(Exception e){ System.out.println(e);}
}
}