联系我们: 手动添加方式: 微信>添加朋友>企业微信联系人>13262280223 或者 QQ: 1483266981
练习Java的高级编程用法,如JDBC,JSP,Security等。
Question 1 – Database
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.
CREATE TABLE Students (Name CHAR(30), StudentId CHAR(8), Major CHAR(20));
INSERT INTO Students (Name, StudentId, Major) VALUES (‘Peter Chan’, ‘11112222’, ‘Computer’);
INSERT INTO Students (Name, StudentId, Major) VALUES (‘David Lee’, ‘11113333’, ‘Computer’);
INSERT INTO Students (Name, StudentId, Major) VALUES (‘Amy Au’, ‘11114444’, ‘Computer’);
INSERT INTO Students (Name, StudentId, Major) VALUES (‘Mary Cheung’, ‘22221111’, ‘History’);
INSERT INTO Students (Name, StudentId, Major) VALUES (‘Johnson Yeung’, ‘22223333’, ‘History’);
INSERT INTO Students (Name, StudentId, Major) VALUES (‘Paul Leung’, ‘22224444’, ‘Arts’);
INSERT INTO Students (Name, StudentId, Major) VALUES (‘May Lee’, ‘22225555’, ‘Arts’);
INSERT INTO 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:
1
SELECT Name, StudentId, Major FROM Students WHERE Name LIKE ? 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.
Question 2 – JSP
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.
Question 3 – Security permission
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:Windowssystem
C:Windowssystem contains 2 files and directories.
> java a2.NumberOfFiles C:
Exception: access denied (“java.io.FilePermission” “C:” “”read””)


发表评论