Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

: Modify the method showResult() to display the number of students in the result. After successfully completing this exercise, your program should generate the following

: Modify the method showResult() to display the number of students in the result. After successfully completing this exercise, your program should generate the following output:

package dbaccessinjava;

import java.sql.DriverManager;

import java.sql.Connection;

import java.sql.SQLException;

import java.sql.*;

import java.io.*;

public class DBAccessInJava{

public static void main(String[] argv) {

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

Connection connection = null;

try {

Statement s=null;

ResultSet rs=null;

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

connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system","123");

System.out.println("Oracle JDBC Driver Registered!");

String retrieveStatement = "Select * from STUDENT";

s = connection.createStatement();

rs = s.executeQuery (retrieveStatement);

ResultSetMetaData RM = rs.getMetaData();

showResult(RM,rs);

String updateStatement = "update Student set Grade = 'B+'";

System.out.println("B+ to all");

System.out.println(updateStatement);

int count = s.executeUpdate(updateStatement);

System.out.println("Number of students affected=" + count);

String deleteStatement = "delete from Student where ID='620013'";

System.out.println(deleteStatement);

System.out.println("Deleting 620013");

s.execute(deleteStatement);

String insertStatement = "insert into Student values('620013','Peter','Angelo','C+')";

System.out.println(insertStatement);

System.out.println("Inserting 620013");

s.execute(insertStatement);

retrieveStatement = "Select * from STUDENT";

rs = s.executeQuery (retrieveStatement);

showResult(RM,rs);

rs.close();

s.close();

connection.close();

}

catch (Exception e) {

System.out.println("Connection Failed! Check output console");

e.printStackTrace();

return;

}

}

static String leftText( String s , int newLen )

{

while ( s.length() < newLen )

{

s = s+" ";

}

return s ;

}

static void showResult( ResultSetMetaData RM , ResultSet rs )

{

try {

for(int i = 1; i <= RM.getColumnCount(); i++)

if (i==1)

System.out.print(RM.getColumnName(i) + " ");

else System.out.print(RM.getColumnName(i) + " ");

System.out.println();

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

while ( rs.next ()){

String nextRec = "";

for ( int x = 1; x <= RM.getColumnCount(); x++ )

{

nextRec += leftText(rs.getString ( x ),10);

}

System.out.println( nextRec );

}

}

catch (Exception e) {

System.out.println("Connection Failed! Check output console");

e.printStackTrace();

return;

}

}

}

Output:

-------- Oracle JDBC Connection Testing ------

Oracle JDBC Driver Registered!

ID FNAME LNAME GRADE

620010 Louis Legakis B+

620011 Deoraj Narine B+

620012 Jian Wang B+

620013 Peter Angelo C+

There are 4 students in the result

B+ to all

update Student set Grade = 'B+'

Number of students affected=4

delete from Student where ID='620013'

Deleting 620013

insert into Student values('620013','Peter','Angelo','C+')

Inserting 620013

ID FNAME LNAME GRADE

620010 Louis Legakis B+

620011 Deoraj Narine B+

620012 Jian Wang B+

620013 Peter Angelo C+

There are 4 students in the result

Note: Your solution should work all the time and without modification regardless of the number of students in the result set.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions

Question

Which of the following is a Website query?

Answered: 1 week ago

Question

Make efficient use of your practice time?

Answered: 1 week ago