Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a method that queries the database, retrieving and writing in a CSV file the crn, subject, course, section, days and time for all CSCI courses,

a method that queries the database, retrieving and writing in a CSV file the crn, subject, course, section, days and time for all CSCI courses, in order according to the course number. You should be able to open the CSV file in Microsoft Excel.


Below is the code that I have so far


import java.sql.*; public class Main { public static void main(String[] args) throws Exception { System.out.println("Connecting to the database... "); // Connect to a database by establishing a Connection object Connection conn = DriverManager.getConnection("jdbc:mysql://160.153.75.195/CWHDemo", "CWHDemo", "Cwhdemo%123"); System.out.println("Database connection established. "); // statement Object for this database connection Statement st = conn.createStatement(); // call a method wit a query showing the pet table's structure (metadata) showColumnsFall2014(st); showTables(st); // call a method that performs a query using Statement st query1(st); query2(st); query3(st); // Close the connection conn.close(); } // end main() /* This method performs as SQL query that returns the metadata for the pet table * The parameter must be a Statement object with an established connection * to an SQL database. */ public static void showColumnsFall2014(Statement s) throws SQLException, ClassNotFoundException { String queryString; // a String to hold an SQL query ResultSet rs; // the result set from an SQL query as a table // Create an SQL query as as String for this statement // this query returns all rows and all columns from the database queryString = "Describe fall2014;"; // Send a statement executing the query and saving the result set rs = s.executeQuery(queryString); // print headings for the output System.out.println("Columns in the fall2014 table:"); System.out.printf("%-10s%-10s%n", "Column", "Datatype"); System.out.println("*********************"); // Iterate the result set and print name, owner, and species attributes while (rs.next()) { System.out.printf("%-10s%-10s%n", rs.getString(1), rs.getString(2)); } System.out.println("********************* "); } // end showMetaData /* The following method performs an SQL query * The parameter must be a Statement object with an established connection * to an SQL database. */ public static void showTables(Statement s) throws SQLException, ClassNotFoundException { String queryString; // a String to hold an SQL query ResultSet rs; // the result set from an SQL query as a table // an SQL query as as String for this statement queryString = "SHOW tables"; // Send a statement executing the query and saving the result set rs = s.executeQuery(queryString); // print headings for the output System.out.println(queryString); System.out.println("Tables in the Database"); System.out.printf("%-10s%n", "Table Names"); System.out.println("*******************************************************"); // Iterate through the result set and print name, owner, and species attributes while (rs.next()) { System.out.printf("%-10s%n", rs.getString(1)); } System.out.println("*******************************************************"); } public static void query1(Statement s) throws SQLException, ClassNotFoundException { String queryString; // a String to hold an SQL query ResultSet rs; // the result set from an SQL query as a table // an SQL query as as String for this statement queryString = "Select crn, subject, course, campus From fall2014 Where credits = 4"; // Send a statement executing the query and saving the result set rs = s.executeQuery(queryString); // print headings for the output System.out.println(queryString); System.out.printf("%-20s%-20s%-20s%-20s%n", "crn", "subject", "course", "campus"); System.out.println("*****************************************************************"); // Iterate through the result set and print name, owner, and species attributes while (rs.next()) { System.out.printf("%-20s%-20s%-20s%-20s%n", rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4)); } System.out.println("*****************************************************************"); } public static void query2(Statement s) throws SQLException, ClassNotFoundException { String queryString; // a String to hold an SQL query ResultSet rs; // the result set from an SQL query as a table // an SQL query as as String for this statement queryString = "Select crn, course, term, room From fall2014 Where subject = 'CSCI' "; // Send a statement executing the query and saving the result set rs = s.executeQuery(queryString); // print headings for the output System.out.println(queryString); System.out.printf("%-20s%-20s%-20s%-20s%n", "CRN", "Course", "Term", "Room"); System.out.println("*********************************************************************"); // Iterate through the result set and print name, owner, and species attributes while (rs.next()) { System.out.printf("%-20s%-20s%-20s%-20s%n", rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4)); } System.out.println("*********************************************************************"); } public static void query3(Statement s) throws SQLException, ClassNotFoundException { String queryString; // a String to hold an SQL query ResultSet rs; // the result set from an SQL query as a table // an SQL query as as String for this statement queryString = "Select crn, course, section, credits, term, room From fall2014 Where term = '15A' "; // Send a statement executing the query and saving the result set rs = s.executeQuery(queryString); // print headings for the output System.out.println(queryString); System.out.printf("%-20s%-20s%-20s%-20s%n", "CRN", "Course", "Section", "Credits", "Term", "Room"); System.out.println("************************************************************************************************************************"); // Iterate through the result set and print name, owner, and species attributes while (rs.next()) { System.out.printf("%-20s%-20s%-20s%-20s%-20s%-20s%n", rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6)); } System.out.println("************************************************************************************************************************"); } }

Step by Step Solution

3.57 Rating (164 Votes )

There are 3 Steps involved in it

Step: 1

Hello Here is the answer hope you like it actual answer for the question is give below import javasql import javaioFileWriter import javautil import j... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial Management for Public Health and Not for Profit Organizations

Authors: Steven A. Finkler, Thad Calabrese

4th edition

133060411, 132805669, 9780133060416, 978-0132805667

More Books

Students also viewed these Programming questions

Question

What is the aggregation problem in variance reporting?

Answered: 1 week ago

Question

Describe the contents of the MD& A.

Answered: 1 week ago