The database CWHDemo on the server at IP address 68.178.217.12 has a table named
ID: 3703445 • Letter: T
Question
The database CWHDemo on the server at IP address 68.178.217.12 has a table named fall2014 with information about computer courses offered at Community College of Philadelphia in the Fall 2014 semester.
* host IP address: 68.178.217.12
* database: CWHDemo
* username: CWHDemo
* password: Cwhdemo%123 (Note: The password is case sensitive.)
Your task is to develop a Java application with two methods:
1. 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.
2. a method that queries the database with a query of your own design based on a question you will write. The results can be displayed neatly on the console.
Your query might answer a question such as What 4 credit courses are available on Tuesday and Thursday? or What sections of OA courses are being offered online? You should make up your own question, write the query to find the answer, and then create a method to get the answer from within a java application.
You can write a single application with methods for each of the two queries.
Explanation / Answer
import java.sql.*;
import java.io.FileWriter.*;
import java.util.*;
import java.sql.Date;
import java.lang.Object.*;
import java.lang.AutoCloseable.*;
import java.io.Closeable.*;
import java.io.FileOutputStream;
import java.util.*;
public class CWHDemo
{
public static void main(String[] args)
{
writetoCSVfile();
}
public static void writetoCSVfile()
{
Connection connection = null;
Statement stmt = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connecting to a selected database...");
connection = DriverManager.getConnection("jdbc:odbc:CWHDemo", "", "");
System.out.println(" database connected successfully...");
stmt = connection.createStatement();
String sql = "SELECT * FROM fall2014";
ResultSet rs = stmt.executeQuery(sql);
String fileheader="credit.subject,course,section,days,time";
String filename="example.csv";
FileWriter fw=new FileWriter(filename);
fw.append(fileheader.toString());
while(rs.next())
{
int credit = rs.getInt("crn");
String subject = rs.getString("subject");
String course = rs.getString("course");
String section = rs.getString("section");
Date date = rs.getDate("date");
Time time=rs.getTime("time");
fw.append(String.valueOf(credit));
fw.append(subject);
fw.append(course);
fw.append(section);
fw.write(date);
fw.write(time);
}
HSSFWorkbook wb=new HSSFWorkbook();
CreationHelper help=wb.getCreationHelper();
HSSFSheet sheet1=wb.createsheet("SHEET");
CSVReader reader=new CSVReader(new FileReader(filename));
String[] line;
int i=0;
while((line=reader.readNext())!=null)
{
Row r=sheet1.createRow(i++);
for(int j=0;j<line.length;j++)
{
r.createCell(j).setCellValue(help.createString(line[i]));
}
}
FileOutputStream out=new FileOutputStream("new.xls");
wb.write(out);
out.close();
rs.close();
}
catch(SQLException se)
{
System.out.println(se);
}
finally
{
try
{
if(stmt!=null)
connection.close();
}
catch(SQLException se)
{
System.out.println(se);
}
Try
{
if(connection!=null)
connection.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
}
public static void display()
{
Connection connection = null;
Statement stmt = null;
Try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connecting to a selected database...");
connection = DriverManager.getConnection("jdbc:odbc:CWHDemo", "", "");
System.out.println(" database connected successfully...");
stmt = connection.createStatement();
String credit = "SELECT course FROM fall2014 where credit=4 and days='tuesday' or days='thursday'";
ResultSet rs = stmt.executeQuery(credit);
System.out.println("course");
while(rs.next())
{
String course = rs.getString("course");
System.out.println(course);
}
String query = "SELECT section FROM fall2014 where course='OA'";
rs=stmt.executeQuery(query);
System.out.println("section");
while(rs.next())
{
String section = rs.getString("section");
System.out.println();
}
rs.close();
}
catch(SQLException se)
{
System.out.println(se);
}
finally
{
try
{
if(stmt!=null)
connection.close();
}
catch(SQLException se)
{
System.out.println(se);
}
try
{
if(connection!=null)
connection.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.