Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program with concepts studied through Chapter 32 that meets the fol

ID: 3804052 • Letter: W

Question

Write a Java program with concepts studied through Chapter 32 that meets the following, requirements:
-
From Textbook
Chapter 32, assignment #32.8 – Populate Salary Table
Don’t forget comments including program description at the beginning of program and then any
necessary comments throughout the program

#32.8 – Populate Salary Table

(Populate Salary table) Create a table named Salary as follows:

create table Salary(

firstName varchar(100),

lastName varchar(100),

rank varchar(15),

salary float);

Obtain the data for salary from http://cs.armstrong.edu/liang/data/Salary.txt and populate it into the Salary table in the database.

Explanation / Answer

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.Scanner;

public class PopulateSalary {

   public static void main(String[] args) throws ClassNotFoundException, SQLException {
       Class.forName("com.mysql.jdbc.Driver");
       //System.out.println("Driver Loaded.");
      
       Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test",
               "drytuna", "Pa$$word");
       //System.out.println("Database Connected.");
      
       Statement stmt = conn.createStatement();

       stmt.executeUpdate("drop table if exists Salary");
      
       String tb_salary = "Create table Salary ("
               + "firstName varchar(100), "
               + "lastName varchar(100), "
               + "rank varchar(15), "
               + "salary decimal(10,2), "
               + "constraint first_name_pk primary key(firstName))";
       stmt.executeUpdate(tb_salary);
      
       try (Scanner sc = new Scanner(new FileInputStream("Salary.txt"))) {
           DecimalFormat df = new DecimalFormat("#.00");
          
           while (sc.hasNext()) {
               String[] record = sc.nextLine().trim().split("\s+");
               String insert_st = "insert into Salary values("
                       + "'" + record[0] + "',"
                       + "'" + record[1] + "',"
                       + "'" + record[2] + "',"
                       + "" + df.format(Double.parseDouble(record[3])) + ")";
               stmt.executeUpdate(insert_st);
           }
       }
       catch (FileNotFoundException fnfe) {
           System.out.println("'Salary.txt' can't be found on the first level"
                   + " of the project folder.");
       }

       tablePrint(stmt, "select * from Salary");
      
       conn.close();
   }
  
   /**
   * Name: tablePrint()
   * @param stmt - a Statement object
   * @param query - a String with select statement
   * @throws SQLException
   * Process: Print the query results with organized spacing
   */
   private static void tablePrint(Statement stmt, String query) throws SQLException {
       ResultSet results = stmt.executeQuery(query);
      
       // Printing the Attribute Names
       for (int i = 1; i <= results.getMetaData().getColumnCount(); i++)
           System.out.printf("%-20s",results.getMetaData().getColumnName(i).toUpperCase());
      
       System.out.println();
      
       // Printing the Rows
       while (results.next()) {
           for (int i = 1; i <= results.getMetaData().getColumnCount(); i++){
               System.out.printf("%-20s",results.getString(i));
           }
           System.out.println();
       }
      
       System.out.println();
       results.close();
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote