Java Development Engineer (web direction)-03. Database Development-Final Exam

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.