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...

  • Hello Can you help to fix the program. When running, it still show Exception in thread...

    Hello Can you help to fix the program. When running, it still show Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Contact location: class ContactMap    at ContactMap.main(ContactMap.java:40) C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) ************************ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ContactMap { public static void main(String args[]) throws IOException { Scanner input=new Scanner(System.in); //Create a TreeMap ,...

  • 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...

  • Could you help me pleas , this is my code I want change it to insert...

    Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...

  • 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...

  • Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException;...

    Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; class BSTTreeNode{    BSTTreeNode left, right;    String data; public BSTTreeNode(){    left = null;    right = null;    data = null; } public BSTTreeNode(String n){    left = null;    right = null;    data = n; } public void setLeft(BSTTreeNode n){    left = n; } public void setRight(BSTTreeNode n){   ...

  • Java need help with searching for a contact Question:Write a program that uses an ArrayList of...

    Java need help with searching for a contact Question:Write a program that uses an ArrayList of parameter type Contact to store a database of contacts. The Contact class should store the contact’s first and last name, phone number, and email address. Add appropriate accessor and mutator methods. Your database program should present a menu that allows the user to add a contact, display all contacts, search for a specific contact and display it, or search for a specific contact and...

  • 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...

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