Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a JAVA PROGRAM that allows user to insert, delete, update(SQL statements)

ID: 3598193 • Letter: C

Question

Create a JAVA PROGRAM that allows user to insert, delete, update(SQL statements). Allows the user to use/input select statements, from statements and where statements.

Focus on whether the program can recognise if the user has entered a VALID or an INVALID statement. Such as Select name, From Student.

Implement functionality for EXECUTE command and EXIT command.

PLEASE STRICTLY USE THIS STARTER CODE BELOW:

package identification;

import java.io.*;

import java.util.Scanner;

public class Main

{

public static void main( String args[] )

{

Scanner input = new Scanner(System.in);

Command comm = null;

String information = "";

while(!(comm instanceof ExitCommand))

{

System.out.print(">");

information = input.nextLine();

if(information.contains(";"))

{

String command = information.substring(0,information.indexOf(";"));

comm = Parser.parse(command);

comm.execute();

information = information.substring(information.indexOf(";")+1)

}

}

}

}

CREATETABLE CLASS

package identification;

import java.sql.*;

public class CreateTable implements Command

{

private String tableName;

private String[] columnNameType;

public CreateTable(String input)

{

//parse the string to see if it is valid.

}

public void execute()

{

System.out.println("Valid Create Syntax.");

}

}

COMMAND CLASS

package identification;

public interface Command {

public void execute();

}

EXIT COMMAND

package identification;

public class ExitCommand implements Command {

public void execute()

{

System.out.println("System exiting.");

}

}

CLASS PARSER

package identification;

public class Parser

{

public static Command parse(String userInput)

{

Command result;

if(userInput.equals("exit"))

result = new ExitCommand();

else if(userInput.startsWith("create table"))

result = new CreateTable(userInput);

return result;

}

}

CAN YOU PLEASE FINISH THE CODE FOR THE CLASSES; DELETE, INSERT,UPDATE,DROP TABLE AND SELECT. AND FINISH UP ON THE STARTER CODE, IF THERE IS ANYTHING MISSING.

USE ANY VALIDATION RULES.

Explanation / Answer

HI. Please find below program.

Command.java Program:

package identification;

public interface Command {
public void execute(); //Interface having only one method without definition
}

ExitCommand.java Program:

package identification;

public class ExitCommand implements Command {
public void execute() {
System.out.println("System exiting."); //Method with definition
}
}

CreateTable.java Program:

package identification;
import java.sql.*;

public class CreateTable implements Command {
private String tableName="Emps";
private String[] columnNameType={"eid","ename","location"};
Connection con;
Statement stmt;
  
public void createConnection(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver"); //Drier class definition
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr"); //Driermanger registration step. Need to give database details including username, password, hostname/localhost and Oracle ersion details like orcl/xe
}
catch(Exception e){
e.printStackTrace();
}
}
  
public CreateTable(String input) {
//parse the string to see if it is valid.
try{
createConnection(); //Calling to createConnetion method to create connetion
String sql = "CREATE TABLE "+tableName+" ("+columnNameType[0]+" integer,"+columnNameType[1]+" varchar2(25),"+columnNameType[2]+" varchar2(25))"; //SQL query to create new table in database
stmt.executeUpdate(sql); //Executing above statement to create new table in DB
System.out.println("CreateTable sucessfully"); //Just sample sop statement
}
catch(Exception e){
e.printStackTrace();  
}
}

public void execute() {
System.out.println("Valid Create Syntax."); //Method with definition
}
}

Parser.java Program:

package identification;

public class Parser {
public static Command parse(String userInput)
{
Command result=null; //Gien program by you
if(userInput.equals("exit"))
result = new ExitCommand();
else if(userInput.startsWith("create table"))
result = new CreateTable(userInput);
return result;
}
}

Main.java Program:

package identification;

import java.util.Scanner;

public class Main { //Sample class given by you
public static void main( String args[] )
{
Scanner input = new Scanner(System.in);
Command comm = null;
String information = "";
while(!(comm instanceof ExitCommand))
{
System.out.print(">");
information = input.nextLine();
if(information.contains(";"))
{
String command = information.substring(0,information.indexOf(";"));
comm = Parser.parse(command);
comm.execute();
information = information.substring(information.indexOf(";")+1);
}
}
}