Question
: 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started