In this assignment, you will write a java program which takes any of the followi
ID: 3809050 • Letter: I
Question
In this assignment, you will write a java program which takes any of the following commands as command line argument (not as input), and executes them using JDBC: 1.) insert into relationname value1 value2 value3 ... Inserts a tuple into the specified relation with the specified values; test it with input containing single quotes (enclose those in double quotes so linux doesn't interpret the quotes) you can assume that all values are strings for simplicity 2.) select from relationnam Prints all tuples from the specified relation. 3.) select from relationname where "condition Executes a query with the specified condition. Note the use of double quotes so that the condition comes as a single command line parameter. 4.) select from relationname1 relationname Displays result of natural join of the two relations. Starter code is import java.sql.*; import java.io.*; public class jdb { public static void main(String[] args) throws Exception { final String DB_URL = "jdbc:postgresql://localhost/coastal"; Console input = System.console(); String password = new String(input.readPassword("[%s]", "Password:")); try { Connection conn = DriverManager.getConnection(DB_URL,"postgres",password); Statement stmt = conn.createStatement(); String query = "Select * from student"; stmt.executeUpdate(query); conn.close(); } catch(Exception e) { System.out.println("Error: " + e.getMessage()); } } }
PLEASE FINISH IT FOR ME!!!!
Explanation / Answer
/*
TABLES USED.
CREATE TABLE STUDENT11(
ID INT,
NAME VARCHAR (30),
CLASS VARCHAR(10)
);
CREATE TABLE SUBJECT
(
CLASS VARCHAR(10),
SUBJECT VARCHAR(15)
);
VALUE INSERTION.
INSERT INTO SUBJECT VALUES ('4th' , 'Math');*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.lang.*;
public class DB {
public static void main(String a[]) throws Exception
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","System","1234");
Statement stmt=con.createStatement();
int sid=Integer.parseInt(a[0]);
//String sid=a[0];
String sname=a[1];
String sclas=a[2];
PreparedStatement ps=con.prepareStatement("INSERT INTO STUDENT11 VALUES(?,?,?)"); /*insert the values in student table*/
ps.setInt(1, sid);
ps.setString(2,sname);
ps.setString(3,sclas);
ps.executeUpdate();
System.out.println("ID" + " " + "NAME" + " " + "CLASS");
ResultSet rs1=stmt.executeQuery("SELECT * FROM STUDENT11"); /*print records from student table*/
while(rs1.next())
{
System.out.println(rs1.getString("id")+" "+rs1.getString("name")+" "+rs1.getString("class"));
}
rs1.close();
ResultSet rs2=stmt.executeQuery("SELECT * FROM STUDENT11 where id=1"); /*selects particular record from student table*/
while(rs2.next())
{
System.out.println(rs2.getString("id")+" "+rs2.getString("name")+" "+rs2.getString("class"));
}
rs2.close();
System.out.println(" "+"ID" + " " + "NAME" + " " + "CLASS"+" "+"SUBJECT");
ResultSet rs3=stmt.executeQuery("SELECT * FROM STUDENT11 NATURAL JOIN SUBJECT"); //prints join of two tables*/
while(rs3.next()){
System.out.println(rs3.getString("id")+" "+rs3.getString("name")+" "+rs3.getString("class")+" "+rs3.getString("SUBJECT"));
}
con.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
/**********OUTPUT*******
ID NAME CLASS
1 Nikhil 4th
2 Nikhil 3th
2 Nikhil 3th
2 Nikhil 3th
2 Nikhil 3th
2 Nikhil 3th
2 Nikhil 3th
2 Nikhil 3th
2 Nikhil 3th
2 Nikhil 3th
2 Nikhil 3th
2 Nikhil 3th
1 Nikhil 4th
ID NAME CLASS SUBJECT
1 Nikhil 4th Math
************************/