Question

In: Computer Science

Using JAVA Resource: "Analyze and Document JDBC API Calls" text file ** PASTED BELOW** For this...

Using JAVA

Resource:

"Analyze and Document JDBC API Calls" text file ** PASTED BELOW**

For this assignment, you will analyze code that uses the JDBC API to access a database, retrieve data, and compose output based on that data. You will then comment the code to reflect the purpose and expected results of the code.

Download the linked TXT file, and read through the Java™ code carefully.

Add your name, instructor's name, and today's date to the header comment.

Replace the five comment placeholders with succinct comments that explain and predict the results of the Java™ statement(s) that directly follow the comment placeholders.

Submit the updated TXT file containing your comments.

Bottom of Form

***************************** CODE **************************************************

/**********************************************************************

*            Program:            SQLcode            

*            Purpose:             To analyze and document JDBC API calls.

*            Programmer:     TYPE YOUR NAME HERE                            

*            Class:                  PRG/421r13, Java Programming II                                       

*            Instructor:                                                    

*            Creation Date:   TYPE TODAY'S DATE HERE          

*

* Comments: The purpose of the JDBC API calls in this program

*           is to retrieve data from a relational database.

*           To complete this assignment, analyze the code and replace

*           the numbered comments as instructed below.

***********************************************************************/

package sqlcodeexample;

// 1. THE REASON FOR IMPORTING THE FOLLOWING LIBRARIES IS...

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class SQLCodeExample {

   public static void main (String[ ] args) {

      try {                                                                                                     

         String host = "jdbc:mysql://localhost/STUDENT";                           

         String uName = "bsmith";

         String uPass = "roxie";

         // 2. THE PURPOSE (RESULT) OF THE FOLLOWING API CALL IS...

         Connection conn = DriverManager.getConnection(host, uName, uPass);   

        

         // 3. THE PURPOSE (RESULT) OF THE FOLLOWING API CALLS IS...

         Statement stmt = conn.createStatement();                        

         String sql = "select Stu_id, Stu_Name from Stu_Class_1";                                      

         ResultSet rs = stmt.executeQuery (sql);                                  

                                         

         System.out.println("Displaying student information: ");    

   

        

         // 4. THE RESULT OF THE FOLLOWING API CALLS IS...

         while (rs.next()) {

             System.out.println ("Student id " + rs.getString("Stu_id");

             System.out.println (" is associated with student name " + rs.getString("Stu_Name");

         }                                                                   

  

      }

      // 5. THE PURPOSE OF THE FOLLOWING CATCH BLOCK IS...

      catch ( SQLException err ) {

         System.out.println(err.getMessage());

      }

   }

}

Solutions

Expert Solution

1. First segment is the aggregation of various imports which are required fetch data from a table in a database or manipulate the data present in it.To be more specific , DriverManager is responsible loading up the drivers , Connection handles all database related operations and SQLException is the generic Exception class by means of which we can catch exception which occur during db operations.

2.The result of the following API call is to acquire a db connection with host name , so that in subsequent call we can query the db using that.

3.Just as suggested in previous point , as we have gotten the connection between our code and database , next thing we need to do is to query it.This is done by means of statement class.We define our sql query as string and then execute it using Statement object.On executing we get result in a collection which is termed as ResultSet

4. rs.next() is used to iterate through ResultSet ,one object at a time.

5. During the execution of the following code snippet , some thing might go wrong and hence Exception can be thrown .Such Exception scenarios are handled by SQLException and error message is printed for purpose of debugging


Related Solutions

Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies on localization to format currencies and dates. In NetBeans, copy the linked code to a file named "Startercode.java". Read through the code carefully and replace all occurrences of "___?___" with Java™ code. Note: Refer to "Working with Dates and Times" in Ch. 5, "Dates, Strings, and Localization," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide for help. Run and debug...
using java, parse a text file to be able to list the word(s) with the highest...
using java, parse a text file to be able to list the word(s) with the highest frequency in a sentence across all sentences in the whole file, also print its frequency and the corresponding sentence. cannot use hash maps. assume text file will be multiple paragraphs long.
using java, parse a text file to answer the following question: -list sentences with the maximum...
using java, parse a text file to answer the following question: -list sentences with the maximum number of occurences of the word “the” in the whole file and also list the corresponding frequency. (cannot use hash maps) example output: the:3:The day had came to leave before the storm. What hit the back bumper of the car before the window cracked? The classroom doors where shut closed before the students open the project.
1. Write a Java program and Submit only "one .java file" calledNumbers that calls the...
1. Write a Java program and Submit only "one .java file" called Numbers that calls the following methods and displays the returned value:o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer.2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive.o Write a method called larger that accepts two double parameters and...
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
In linux , Using a simple text editor, create a text file with the following name...
In linux , Using a simple text editor, create a text file with the following name "Test" and content: GNU GENERAL PUBLIC LICENSE The GNU General Public License is a free, copy left license for the GNU General Public License is intended to guarantee your freedom to GNU General Public License for most of our software; it applies … 2-Write the command to create the text file. 3-Print the file permissions. 4-Create a directory named "361" 5-Copy file "Test" to...
Throwing Exceptions - JAVA File Factorials contains a program that calls the factorial method of theMathUtils...
Throwing Exceptions - JAVA File Factorials contains a program that calls the factorial method of theMathUtils class to compute the factorials of integers entered by the user. Save these files to your directory and study the code in both, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT