Pre-programs (50%): Create a file with your name as the data. Save the file usin
ID: 3853295 • Letter: P
Question
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%):
Database Access
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
import java.sql.*; import oracle.jdbc.*; import oracle.jdbc.pool.OracleDataSource; // We import java.io to be able to read from the command line import java.io.*; class JdbcCheckup { public static void main(String args[]) throws SQLException, IOException { // Prompt the user for connect information System.out.println("Please enter information to test connection to the database"); String user; String password; String database; user = readEntry("user: "); int slash_index = user.indexOf('/'); if (slash_index != -1) { password = user.substring(slash_index + 1); user = user.substring(0, slash_index); } else password = readEntry("password: "); database = readEntry("database(a TNSNAME entry): "); System.out.print("Connecting to the database..."); System.out.flush(); System.out.println("Connecting..."); // Open an OracleDataSource and get a connection OracleDataSource ods = new OracleDataSource(); ods.setURL("jdbc:oracle:oci:@" + database); ods.setUser(user); ods.setPassword(password); Connection conn = ods.getConnection(); System.out.println("connected."); // Create a statement Statement stmt = conn.createStatement(); // Do the SQL "Hello World" thing ResultSet rset = stmt.executeQuery("select 'Hello World' from dual"); while (rset.next()) System.out.println(rset.getString(1)); // close the result set, the statement and the connection rset.close(); stmt.close(); conn.close(); System.out.println("Your JDBC installation is correct."); } // Utility function to read a line from standard input static String readEntry(String prompt) { try { StringBuffer buffer = new StringBuffer(); System.out.print(prompt); System.out.flush(); int c = System.in.read(); while (c != ' ' && c != -1) { buffer.append((char)c); c = System.in.read(); } return buffer.toString().trim(); } catch(IOException e) { return ""; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.