A1

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.

/**
* ********************************************************************
* 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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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());
}
}
}

A2

For this assignment, you will create Java code that accesses a relational database, requests data, and then analyzes and displays a portion of that data.

Imagine a MySQL relational database schema named COMPANY_DB containing two tables, employee_table and payroll_table, such that the records in each of the tables is as follows:

employee_table:

Emp id FName LName Addr City State Zip
100 Jack Smith 123 North Topeka KS 66603
101 Joe Apple 4 Street Denver CO 80202
111 Nancy Good 45 SW Hartford CT 06103
121 Tom Whatever 89 NE Dover DE 19901
122 Jim Thompson 789 W 95 Albany NY 12207
123 Tommy Boyson 154 Bolt Boston MA 02201
125 John Jones 47 West Lincoln NE 68502

payroll_table:

Emp id Paysch 401k Spouse
100 BiWk yes yes
101 BiWk yes yes
111 Monthly no no
121 Wkly pending yes
122 Wkly yes no
123 Monthly pending no
125 Monthly no yes

The credentials you will need to access the database which holds both of the tables are as follows:

  • Host string = localhost:3306
  • Username = student
  • Password = prg421

Copy and paste the linked Java "starter" code into the NetBeans editor and save as a JAVA file.

Add Java statements to the file to accomplish the following:

  • Establish a connection to the database
  • Query the database, joining the two tables on the Emp_id field
  • Display your name and today's date on the console along with the following returned database results:
    • employee identification number
    • first and last name
    • state
    • payroll schedule
    • 401k plan
  • Close the database connection

Identify and correct any compile-time errors that exist in the starter code.

/**
* ********************************************************************
* Program: PRG282Week5CodeAssignment_startercode.java
* Purpose: Starter code to access two tables via JDBC.
* Programmer: YOUR NAME GOES HERE
* Class: PRG/421r13, Java Programming II
* Instructor: YOUR INSTRUCTOR'S NAME GOES HERE
* Creation Date: TODAY'S DATE GOES HERE
*
* CommentS: The purpose of this code is to access a relational
* database (MySQL), query two joined tables, and
* process the results of the query. For this assignment,
* no heavy-duty processing is required; we will simply
* examine the returned data and display a selected
* portion of it on the console.
**********************************************************************
*/
package SQLcommand;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class SQLstarter {

public static void main(String[] args) {
try { // start of the try/catch block of code

String host = "jdbc:mysql://localhost:3306/COMPANY_DB"; // This value is provided in the instructions for this assignment.
String uName = "student"; // This value is provided in the instructions for this assignment.
String uPass = "prg421"; // This value is provided in the instructions for this assignment.

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

// Select values from the tables
System.out.println("YOUR NAME");
System.out.println(new Date());
System.out.println();

System.out.println("Emp_id, FName, LName, State, Paysch, 401k"); // display values from the tables


} catch (SQLException err) {
System.out.println(err.getMessage());
}
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.