Question

There is a problem in the update code. I need to be able to update one...

There is a problem in the update code.

I need to be able to update one attribute of the students without updating the other attributes (keeping them empty).

package dbaccessinjava;

import java.sql.DriverManager;

import java.sql.Connection;

import java.sql.SQLException;

import java.sql.*;

import java.io.*;

import java.util.Scanner;

public class DBAccessInJava {

public static void main(String[] argv) {

   System.out.println("-------- Oracle JDBC Connection Testing ------");

Connection connection = null;

try{

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con= DriverManager.getConnection("jdbc:oracle:thin:@MF057PC16:1521:ORCL", "u201303908","pmu");

String sql="select * from student";

Statement st;

PreparedStatement ps;

ResultSet rs;

do{

System.out.println("Enter 0 to terminate>");

System.out.println("Enter 1 to view the table>");

System.out.println("Enter 2 to insert>");

System.out.println("Enter 3 to delete>");

System.out.println("Enter 4 to update>");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int choice;

choice = Integer.parseInt(br.readLine());

if(choice==0){

break;

}

else if(choice==1){

st = con.createStatement();

rs = st.executeQuery(sql);

System.out.println("*******Data from the Table*******");

while(rs.next()){

System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));

}

System.out.println("\n");

}

else if(choice==2){

ps = con.prepareStatement("insert into student values(?,?,?,?)");

System.out.println("Enter ID:");

String sid = br.readLine();

System.out.println("Enter First Name:");

String fName = br.readLine();

System.out.println("Enter Last Name:");

String lName = br.readLine();

System.out.println("Enter Grade:");

String grade = br.readLine();

  

ps.setString(1, sid);ps.setString(2, fName);

ps.setString(3, lName);ps.setString(4, grade);

  

int row = ps.executeUpdate();

if(row==1){

System.out.println("Student Data Updated...");

}

}

else if(choice==3){

System.out.println("Enter Student ID to be deleted:");

String sid = br.readLine();

st = con.createStatement();

st.executeUpdate("delete from STUDENT where id='"+sid+"'");

System.out.println("Student deleted...");

}

else if(choice==4){

System.out.println("Enter ID to decide which student to update:");

String sid = br.readLine();

System.out.println("Enter First Name:");

String fName = br.readLine();

System.out.println("Enter Last Name:");

String lName = br.readLine();

System.out.println("Enter Grade:");

String grade = br.readLine();

st = con.createStatement();

st.executeUpdate("update STUDENT set fname='"+fName+"', lname='"+lName+"', grade='"+grade+"' where id= '"+sid+"'");

  

System.out.println("Student Updated....");

}

else {

System.out.println("please choose a number from the list");

System.out.println("\n");

}

}

while(true);

  

}catch(Exception e){

e.printStackTrace();

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package dbaccessinjava;

import java.sql.DriverManager;

import java.sql.Connection;

import java.sql.SQLException;

import java.sql.*;

import java.io.*;

import java.util.Scanner;

public class DBAccessInJava {

public static void main(String[] argv) {

   System.out.println("-------- Oracle JDBC Connection Testing ------");

Connection connection = null;

try{

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con= DriverManager.getConnection("jdbc:oracle:thin:@MF057PC16:1521:ORCL", "u201303908","pmu");

String sql="select * from studentdetails";

Statement st;

PreparedStatement ps;

ResultSet rs;

do{

System.out.println("Enter 0 to terminate>");

System.out.println("Enter 1 to view the table>");

System.out.println("Enter 2 to insert>");

System.out.println("Enter 3 to delete>");

System.out.println("Enter 4 to update>");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int choice;

choice = Integer.parseInt(br.readLine());

if(choice==0){

break;

}

else if(choice==1){

st = con.createStatement();

rs = st.executeQuery(sql);

System.out.println("*******Data from the Table*******");

while(rs.next()){

System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));

}

System.out.println("\n");

}

else if(choice==2){

ps = con.prepareStatement("insert into student values(?,?,?,?)");

System.out.println("Enter ID:");

String sid = br.readLine();

System.out.println("Enter First Name:");

String fName = br.readLine();

System.out.println("Enter Last Name:");

String lName = br.readLine();

System.out.println("Enter Grade:");

String grade = br.readLine();

  

ps.setString(1, sid);ps.setString(2, fName);

ps.setString(3, lName);ps.setString(4, grade);

  

int row = ps.executeUpdate();

if(row==1){

System.out.println("Student Data Updated...");

}

}

else if(choice==3){

System.out.println("Enter Student ID to be deleted:");

String sid = br.readLine();

st = con.createStatement();

st.executeUpdate("delete from STUDENT where id='"+sid+"'");

System.out.println("Student deleted...");

}

else if(choice==4){

System.out.println("Enter ID to decide which student to update:");

String sid = br.readLine();

System.out.println("Enter First Name:");

String fName = br.readLine();

System.out.println("Enter Last Name:");

String lName = br.readLine();

System.out.println("Enter Grade:");

String grade = br.readLine();

st = con.createStatement();

//here you need to update a query to update single or multiple attributes.if you mention single it will update single remianing will be same

st.executeUpdate("update STUDENT set fname='"+fName+"' where id= '"+sid+"'");

  

System.out.println("Student Updated....");

}

else {

System.out.println("please choose a number from the list");

System.out.println("\n");

}

}

while(true);

  

}catch(Exception e){

e.printStackTrace();

}

}

}

Add a comment
Know the answer?
Add Answer to:
There is a problem in the update code. I need to be able to update one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • I need to update the code listed below to meet the criteria listed on the bottom....

    I need to update the code listed below to meet the criteria listed on the bottom. I worked on it a little bit but I could still use some help/corrections! import java.util.Scanner; public class BankAccount { private int accountNo; private double balance; private String lastName; private String firstName; public BankAccount(String lname, String fname, int acctNo) { lastName = lname; firstName = fname; accountNo = acctNo; balance = 0.0; } public void deposit(int acctNo, double amount) { // This method is...

  • What is the problem with my Program ? also I need a Jframe that desplays the...

    What is the problem with my Program ? also I need a Jframe that desplays the original input on the left side and the sorted input of the left side. my program is supose to read the number for basketball players, first name, last name, and float number that is less than 0 . on the left sideit is supposed to sort all this input based on last name. This is my demo class : package baseball; import java.io.*; import...

  • Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well. you will also need to able...

    Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well. you will also need to able to modify the code so it will accept name such as "john kenny " and the progrom should also ask for the student id number. #include<iostream> #include<stdlib.h> using namespace std; struct st { int roll; char name[50]; char grade; struct...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • I am working on this code and keep getting errors like "ERROR: Syntax error: Encountered "<EOF>"...

    I am working on this code and keep getting errors like "ERROR: Syntax error: Encountered "<EOF>" at line 1, column 169." and "ERROR: Table/View 'STUDENT' does not exist. ERROR: Table/View 'STUDENT' does not exist. ERROR: Table/View 'STUDENT' does not exist." I do not know why this isn't working. Here is my code: DTCCDatabase.java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * This program creates the CoffeeDB database. */ public class DTCCDatabase {    public static void main(String[] args)...

  • For this code below, I need to add the form information to mysql when I click...

    For this code below, I need to add the form information to mysql when I click on submit, at the same time when I click on submit I need to move to another page like Welcome.php WITH NOTE THAT THE ENTERED INFORMATION NOW ALREADY IN DATABASE how can I male that with my code below? THANKS ................................................................................................................................................   <form method="POST"> <div class="container">    <label for="fname"><b>First Name</b></label> <input type="text" placeholder="Enter First Name" name="fname" required> <label for="lname"><b>Last Name</b></label> <input type="text" placeholder="Enter Last Name"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT