Final ExamsProgramming questions
This programming question contains 4 small questions, covering the knowledge points from the underlying JDBC, the connection pool to the MyBatis.
1 (10 points)
There is an online education product "Day up" mainly to achieve the ability to view the curriculum on the phone. The back-end system of the product has a database table with all the customer course information, which is structured as follows:
Use JDBC to write a program that reads all the course names of the students named "Zhangsan" and outputs them to the console terminal.
For:
Database:
/Usr/Local/Mysql/bin./Mysql-U root-Pmysql> CREATE DATABASEFinal_assignment;mysql> Grant All Privileges onFinal_assignment.* toMatt@localhost; MySQL>quit./Mysql-U Matt-P;mysql> UseFinal_assignment;mysql> CREATE TABLEEnrollment ( -IdintAuto_incrementPrimary Key, -UserNamevarchar( -) not NULL, -Coursenamevarchar( -) not NULL -); MySQL> INSERT intoEnrollmentVALUES(NULL, "Zhangsan", "Math"); MySQL> INSERT intoEnrollmentVALUES(NULL, "Lisi", "Math"); MySQL> INSERT intoEnrollmentVALUES(NULL, "Zhangsan", "Graphics");
JDBC Program:
Public classCurriculum {Static FinalString driver_name = "Com.mysql.jdbc.Driver"; Static FinalString URL = "Jdbc:mysql://localhost/final_assignment"; Static FinalString user_name = "Matt"; Static FinalString PASSWORD = "Matt"; Public Static voidCurriculumdataprocessing ()throwsclassnotfoundexception {Connection conn=NULL; PreparedStatement ptmt=NULL; ResultSet RS=NULL; String SQL= "Select Coursename from enrollment where UserName =?"; String UserName= "Zhangsan"; Class.forName (driver_name); Try{conn=drivermanager.getconnection (URL, user_name, PASSWORD); Ptmt=conn.preparestatement (SQL); Ptmt.setstring (1, userName);//System.out.println (ptmt.tostring ());rs =Ptmt.executequery (); while(Rs.next ()) {System.out.println (rs.getstring ("Coursename")); } } Catch(SQLException e) {e.printstacktrace (); } finally { Try { if(conn!=NULL) Conn.close (); if(ptmt!=NULL) Ptmt.close (); if(rs!=NULL) Rs.close (); } Catch(SQLException e) {e.printstacktrace (); } } } Public Static voidMain (string[] args)throwsclassnotfoundexception {curriculumdataprocessing (); }}
Output:
2 (10 points)
Use the cursor to read the course name and user name of all the courses of all users in topic 1 and output to the console terminal.
For:
In the code in question 1, modify:
Static final String URL = "Jdbc:mysql://localhost/final_assignment?usecursorfetch=true";
String sql = "Select UserName, coursename from enrollment";
Ptmt.setfetchsize (2);
System.out.println (rs.getstring ("userName") + ":" + rs.getstring ("Coursename");
Delete ptmt.setstring (1, userName);
Public classCurriculumFetchQ2 {Static FinalString driver_name = "Com.mysql.jdbc.Driver"; Static FinalString URL = "Jdbc:mysql://localhost/final_assignment?usecursorfetch=true"; Static FinalString user_name = "Matt"; Static FinalString PASSWORD = "Matt"; Public Static voidCurriculumdataprocessing ()throwsclassnotfoundexception {Connection conn=NULL; PreparedStatement ptmt=NULL; ResultSet RS=NULL; String SQL= "Select UserName, coursename from enrollment"; Class.forName (driver_name); Try{conn=drivermanager.getconnection (URL, user_name, PASSWORD); Ptmt=conn.preparestatement (SQL); Ptmt.setfetchsize (2); RS=Ptmt.executequery (); while(Rs.next ()) {System.out.println (rs.getstring ("UserName") + ":" + rs.getstring ("Coursename"))); } } Catch(SQLException e) {e.printstacktrace (); } finally { Try { if(conn!=NULL) Conn.close (); if(ptmt!=NULL) Ptmt.close (); if(rs!=NULL) Rs.close (); } Catch(SQLException e) {e.printstacktrace (); } } } Public Static voidMain (string[] args)throwsclassnotfoundexception {curriculumdataprocessing (); }}
Java Development Engineer (web direction)-03. Database Development-Final Exam