Develop a JDBC program called Students that finds and displays student records in a database table. The table, called Students, has three columns: Name CHAR(30), StudentId CHAR(8), and Major CHAR(20). The following SQL file creates and populates the table.
CREATETABLE Students (NameCHAR(30), StudentId CHAR(8), Major CHAR(20));
INSERTINTO Students (Name, StudentId, Major) VALUES ('Peter Chan', '11112222', 'Computer');
INSERTINTO Students (Name, StudentId, Major) VALUES ('David Lee', '11113333', 'Computer');
INSERTINTO Students (Name, StudentId, Major) VALUES ('Amy Au', '11114444', 'Computer');
INSERTINTO Students (Name, StudentId, Major) VALUES ('Mary Cheung', '22221111', 'History');
INSERTINTO Students (Name, StudentId, Major) VALUES ('Johnson Yeung', '22223333', 'History');
INSERTINTO Students (Name, StudentId, Major) VALUES ('Paul Leung', '22224444', 'Arts');
INSERTINTO Students (Name, StudentId, Major) VALUES ('May Lee', '22225555', 'Arts');
INSERTINTO Students (Name, StudentId, Major) VALUES ('John Lee', '22226666', 'Arts');
The program should prompt the user for part of student names or majors, query the database table, and display the details of the matched records. If the user does not input anything (just press Enter), all records are displayed. You may use this SQL command in the program:
SELECTName, StudentId, Major FROM Students WHERENameLIKE ? OR Major LIKE ?
Use the JDBC connection data in Unit 4, i.e. database COREJAVA, user name dbuser, and password secret. These are sample outputs of running the program (assuming the above data in the table).
> java a2.Students Enter part of name or major: Compu Peter Chan (11112222) Computer David Lee (11113333) Computer Amy Au (11114444) Computer > java a2.Students Enter part of name or major: John Johnson Yeung (22223333) History John Lee (22226666) Arts > java a2.Students Enter part of name or major: Peter Chan (11112222) Computer David Lee (11113333) Computer Amy Au (11114444) Computer Mary Cheung (22221111) History Johnson Yeung (22223333) History Paul Leung (22224444) Arts May Lee (22225555) Arts John Lee (22226666) Arts
You should eliminate leading and trailing whitespaces of the results retrieved from database with method trim of class String.
Develop a JSP page that is a web version of the JDBC program in question 1. Specifically, the JSP page generates a table for displaying the data in the Students database table. When an HTTP request parameter called “nameOrMajor” exists, only those student records with their names or majors containing the parameter value are displayed. When the request parameter does not exist, all student records are displayed.
This screenshot shows the output of the JSP page when there is no nameOrMajor parameter.
These two screenshots show the output of the JSP page when there is a nameOrMajor parameter.
Develop a program that counts the files and subdirectories in a directory, using a security manager and an associated security policy file.
The program, called NumberOfFiles, programmatically sets the policy file (which is in the working directory), installs a security manager, obtains and displays the number of files and subdirectories in a directory. The directory is specified as a command-line argument to the program. If there is any exception, the program should display the exception message and end its execution. (Hint: To obtain the files and subdirectories in a directory, you may use the list method of the File class.)
The security policy file, called NumberOfFiles.policy, grants read access to the directory
C:\Windows and all its subdirectories (recursively).
Submit both the program and the policy file. These are sample outputs of running the program.
> java a2.NumberOfFiles C:\Windows C:\Windows contains 29 files and directories. > java a2.NumberOfFiles C:\Windows\system C:\Windows\system contains 2 files and directories. > java a2.NumberOfFiles C:\ Exception: access denied ("java.io.FilePermission" "C:\" "read") > java a2.NumberOfFiles C:\temp Exception: access denied ("java.io.FilePermission" "C:\temp" "read")
Develop a program, called AESBase64, that performs AES encryption and Base64 encoding. Base64 is an encoding scheme that represents binary data in an ASCII string format. The output of AES encryption is binary and contains non-printable characters; with Base64 encoding, the result can be displayed like textual data on the screen.
The program should take three command-line arguments. The first argument, which is either -encrypt or -decrypt, determines whether to perform encryption or decryption. The second argument is the encryption key. The third argument, which is optional, is the data to be encrypted or decrypted; if it is not specified, the data is read from System.in.
For both encryption and decryption, the program computes the MD5 message digest of the encryption key (i.e. the second command-line argument) and uses the digest as the key for AES operations. This is because AES keys must be 128, 192, or 256 bits in length, and MD5 digests are 128 bits in length.
To perform encryption, the program encrypts the data using AES, encodes the encrypted data using Base64, and then displays the encoded data. To perform decryption, the program trims the data as a string, decodes the trimmed data using Base64, decrypts the decoded data using AES, and then displays the decrypted data. (Hint: Create a SecretKeySpec object of the “AES” algorithm as the AES key. For Base64 encoding and decoding, refer to the API documentation of the java.util.Base64 class.)
These are sample outputs of running the program.
> java a2.AESBase64 Usage: java -encrypt key [data] java -decrypt key [data] > java a2.AESBase64 -encrypt pass COMPS311 jDr03rQwChVLvKOokwvLgg== > java a2.AESBase64 -decrypt pass jDr03rQwChVLvKOokwvLgg== COMPS311 > java a2.AESBase64 -encrypt pass COMPS311 | java a2.AESBase64 - decrypt pass COMPS311