Please Help with Java Assignment// Also Please provide Output Pre-programs (50%)
ID: 3910077 • Letter: P
Question
Please Help with Java Assignment// Also Please provide Output
Pre-programs (50%):
Create a file with your name as the data. Save the file using your first initial and last name as part of the file name. Include code to copy the file. Check if the file exists first and throw a message if the file already exists. Include code to read the file using a data stream. Display the file's data on screen.
Using JFileChooser, build a dialog box to open and save files. The dialog box should allow you to browse directories and type in a file name. You need to consider what you will do with the file once you open it and code accordingly.
Main Program (50%):
The objective of this assignment is to allow you to work with JDBC via an ODBC-connected Access database.
Your first task for this assignment is to locate and set up the "exampleMDB.mdb" file in the supplemental chapters section of the CD-rom that came with your Liang text. Copy this file to your hard drive. Next, create an ODBC connection to this database called "assignment8".
Next, create a simple Java program that connects to this database using JDBC. Your program should then query this database's "student" table and display a list of all the students' names in alphabetic order (based upon last name).
If you implement your program correctly, this is the output you should see:
Rick R. Carter
Frank E. Jones
Joy P. Kennedy
Toni R. Peterson
Josh R. Smith
Jean K. Smith
George K. Smith
Jacob R. Smith
John K. Stevenson
Patrick R. Stoneman
Josh R. Woo
Explanation / Answer
Answer:
here, I have following code as shown below
CODE:
import java.util.Scanner;
import java.net.URL;
import java.sql.*;
import java.io.PrintWriter;
// Implement a class
class TestDatabase
{
// Main function
public static void main (String args[])
{
// to get driver
try
{
Class.forName("org.gjt.mm.mysql.Driver");
}
// display
catch (ClassNotFoundException e)
{
System.out.println("Unable to load driver class");
return;
}
// set the null
ResultSet Result = null;
try
{
DriverManager.setLogWriter(new PrintWriter(System.out) );
// create a connection
Connection cont = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/civil2","","");
GiveanWarnings(cont.getWarnings() );
DatabaseMetaData dmd = cont.getMetaData();
System.out.println( " Connected to " + dmd.getURL()
+ " using driver = " + dmd.getDriverName() + ", version = "
+ dmd.getDriverVersion() );
// print the catalog
System.out.println(
" The Vendor Product Name is " + dmd.getDatabaseProductName()
+ ". Database Software Version is "
+ dmd.getDatabaseProductVersion()
+ ". User Name is " + dmd.getUserName()
+ ". Catalog is called " + dmd.getCatalogTerm()
+ ". Schema is called " + dmd.getSchemaTerm()
+ ". Procedure is called " + dmd.getProcedureTerm()
+ "." );
System.out.println(
" Numeric Functions are: " + dmd.getNumericFunctions()
+ " String Functions are: " + dmd.getStringFunctions()
+ " Date and Time functions are: " + dmd.getTimeDateFunctions()
+ " System Functions are: " + dmd.getSystemFunctions()
+ " " );
// print the string
System.out.println(
"Use the Escape String "" + dmd.getSearchStringEscape()
+ "" to escape wildcard characters." );
System.out.println(
"Is the database in read only mode? Answer: " + dmd.isReadOnly()
+ "" );
// writing the statement
Statement state = cont.createStatement();
String sqlQury;
// drop the table
sqlQury = "DROP TABLE users";
try
{
state.executeUpdate(sqlQury);
System.out.println("Dropped table "users"!");
}
catch(SQLException ex)
{
System.out.println("Could not drop table "users"!");
printException(ex);
}
GiveanWarnings(state.getWarnings() );
// creating a table
sqlQury = "CREATE TABLE users ("
+ "login VARCHAR(30) PRIMARY KEY NOT NULL,"
+ "password VARCHAR(100),"
+ "first VARCHAR(50),"
+ "middle VARCHAR(50),"
+ "last VARCHAR(50),"
+ "nickname VARCHAR(30),"
+ "email VARCHAR(100),"
+ "city VARCHAR(50),"
+ "state VARCHAR(50),"
+ "country VARCHAR(50),"
+ "zip VARCHAR(30)"
+ ")";
try
{
state.executeUpdate(sqlQury);
System.out.println("Created table "users"!");
}
catch(SQLException ex)
{
printException(ex);
}
GiveanWarnings(state.getWarnings() );
Result = dmd.getTableTypes();
ShownResult(Result);
Result.close();
Result = dmd.getCatalogs();
ShownResult(Result);
Result.close();
Result = dmd.getSchemas();
ShownResult(Result);
Result.close();
Result = dmd.getTables(null, null, null, null);
ShownResult(Result);
Result.close();
Result = dmd.getIndexInfo(null, null, "users", false, true);
ShownResult(Result);
Result.close();
Result = dmd.getPrimaryKeys(null, null, "users");
ShownResult(Result);
Result.close();
Result = dmd.getProcedures(null, null, null);
ShownResult(Result);
Result.close();
Result = dmd.getTablePrivileges(null, null, null);
ShownResult(Result);
Result.close();
Result = dmd.getColumnPrivileges(null, null, null, null);
ShownResult(Result);
Result.close();
Result = dmd.getColumns(null, null, null, null);
ShownResult(Result);
Result.close();
// INSERT query statement (UPDATE is similar)
sqlQury = "INSERT INTO users ("
+ "login, password, nickname, "
+ "last, first, middle, email,"
+ "city, state, country, zip"
+ ") VALUES ("
+ "'superman', 'foo has a meaning', 'S',"
+ "NULL, NULL, NULL, 'iam@your.place',"
+ "NULL, NULL, NULL, '56735-6453'"
+ ")";
state.executeUpdate(sqlQury);
GiveanWarnings(state.getWarnings() );
sqlQury = "SELECT * FROM users WHERE login IS NOT NULL";
Result = state.executeQuery(sqlQury);
GiveanWarnings(state.getWarnings() );
ShownResult(Result);
Result.close();
sqlQury = "DELETE FROM users WHERE login IS NOT NULL";
state.executeUpdate(sqlQury);
GiveanWarnings(state.getWarnings() );
state.close();
cont.close();
}
catch(SQLException ex){printException(ex); }
}
private static void ShownResult(ResultSet rs) throws SQLException
{
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
// Display column titles
System.out.println("-----------------------------------------");
for(int i=1; i<=numCols; i++)
{
if(i > 1) System.out.print(",");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
System.out.println("-----------------------------------------");
while(rs.next() )
{
for(int i=1; i<=numCols; i++)
{
if(i > 1) System.out.print(",");
System.out.print(rs.getString(i));
GiveanWarnings(rs.getWarnings() );
}
System.out.println("");
}
}
private static boolean GiveanWarnings(SQLWarning warn) throws SQLException
{
boolean boo = false;
if(warn != null)
{
System.out.println(" *** Warning *** ");
boo = true;
while(warn != null)
{
System.out.println("SQLState: " + warn.getSQLState() );
System.out.println("Message: " + warn.getMessage() );
System.out.println("Vendor: " + warn.getErrorCode ());
System.out.println ("");
warn = warn.getNextWarning ();
}
}
return boo;
}
public static void printException(SQLException ex)
{
// print the Exception
System.out.println(" *** SQLException caught *** ");
while(ex != null)
{
System.out.println("SQLState: " + ex.getSQLState() );
System.out.println("Message: " + ex.getMessage() );
System.out.println("Vendor: " + ex.getErrorCode ());
ex.printStackTrace(System.out);
System.out.println ("");
// Get next error object in chain. null if none.
ex = ex.getNextException();
}
}
}