In: Computer Science
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());
}
}
}
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