期末考试
编程题
本编程题包含4个小题,覆盖知识点从基础的JDBC、连接池到MyBatis。
1(10分)
有一款在线教育产品“天天向上”主要实现了在手机上查看课程表的功能。该产品的后端系统有一张保存了所有客户课程信息的数据库表,表结构如下:
请使用JDBC编写一段程序,实现读取用户名为“ZhangSan”的同学的所有课程名称,输出到控制台终端。
答:
数据库:
/usr/local/mysql/bin ./mysql -u root -p mysql> CREATE DATABASE final_assignment; mysql> grant all privileges on final_assignment.* to matt@localhost; mysql> quit ./mysql -u matt -p; mysql> use final_assignment; mysql> CREATE TABLE Enrollment ( -> id int auto_increment primary key, -> UserName varchar(100) not null, -> courseName varchar(100) not null -> ); mysql> INSERT INTO Enrollment VALUES (null, "ZhangSan", "Math"); mysql> INSERT INTO Enrollment VALUES (null, "Lisi", "Math"); mysql> INSERT INTO Enrollment VALUES (null, "ZhangSan", "Graphics");
JDBC程序:
public class curriculum { static final String DRIVER_NAME = "com.mysql.jdbc.Driver"; static final String URL = "jdbc:mysql://localhost/final_assignment"; static final String USER_NAME = "matt"; static final String PASSWORD = "matt"; public static void curriculumDataProcessing() throws ClassNotFoundException { 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 void main(String[] args) throws ClassNotFoundException { curriculumDataProcessing(); } }
输出:
2(10分)
使用游标方式读取题目1中所有用户的所有课程的课程名称和用户名称,输出到控制台终端。
答:
在题1的代码中修改:
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"));
删除ptmt.setString(1, userName);
public class CurriculumFetchQ2 { static final String DRIVER_NAME = "com.mysql.jdbc.Driver"; static final String URL = "jdbc:mysql://localhost/final_assignment?useCursorFetch=true"; static final String USER_NAME = "matt"; static final String PASSWORD = "matt"; public static void curriculumDataProcessing() throws ClassNotFoundException { 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 void main(String[] args) throws ClassNotFoundException { curriculumDataProcessing(); } }
3(10分)
使用DBCP数据库连接池实现题目1中需要完成的功能。
答:
1. add external jar -> commons-dbcp2-2.1.1.jar
2.
package com.sheng.database.finalassignment; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.commons.dbcp2.BasicDataSource; public class CurriculumDBCPQ3 { public static BasicDataSource ds = null; public static final String DRIVER_NAME = "com.mysql.jdbc.Driver"; public static final String URL = "jdbc:mysql://localhost/final_assignment"; public static final String USER_NAME = "matt"; public static final String PASSWORD = "matt"; public static void dbpoolInit() throws ClassNotFoundException { // initialization ds = new BasicDataSource(); ds.setDriverClassName(DRIVER_NAME); ds.setUrl(URL); ds.setUsername(USER_NAME); ds.setPassword(PASSWORD); ds.setMaxTotal(2); } public static void curriculumDataProcessing() { Connection conn = null; PreparedStatement ptmt = null; ResultSet rs = null; String sql = "select CourseName from Enrollment where UserName = ?"; String userName = "ZhangSan"; try { conn = ds.getConnection(); ptmt = conn.prepareStatement(sql); ptmt.setString(0, userName); 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 void run() { long start = System.currentTimeMillis(); while(System.currentTimeMillis() - start < 10000) { curriculumDataProcessing(); } } public static void main (String[] args) throws ClassNotFoundException { dbpoolInit(); for (int i = 0; i < 10; i++) { curriculumDataProcessing(); } } }
4(10分)
由于录入课程错误,现在需要编写一段程序,实现将“ZhangSan”的“math”课程交给“LiSi”,
你需要完成将“ZhangSan”的“math”课程记录删除,然后新插入一个“LiSi”的“math”课程,且两个过程要作为一个事务执行。
答:
package com.sheng.database.finalassignment; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class CurriculumTransactionQ4 { static final String DRIVER_NAME = "com.mysql.jdbc.Driver"; static final String URL = "jdbc:mysql://localhost/final_assignment"; static final String USER_NAME = "matt"; static final String PASSWORD = "matt"; public static void updateDataInfoByTransaction () throws ClassNotFoundException { Connection conn = null; PreparedStatement ptmt1 = null; PreparedStatement ptmt2 = null; String sql1 = "DELETE FROM Enrollment WHERE userName=? AND courseName=?"; String sql2 = "INSERT INTO Enrollment VALUES (null, ?, ?)"; String userName1 = "ZhangSan"; String userName2 = "ZhangSan"; String courseName = "Math"; Class.forName(DRIVER_NAME); try { conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD); conn.setAutoCommit(false); ptmt1 = conn.prepareStatement(sql1); ptmt1.setString(1, userName1); ptmt1.setString(2, courseName); ptmt1.execute(); ptmt2 = conn.prepareStatement(sql2); ptmt2.setString(1, userName2); ptmt2.setString(2, courseName); ptmt2.execute(); conn.commit(); } catch (SQLException e) { if(conn!=null) { try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } finally { try { if (conn != null) conn.close(); if (ptmt1 != null) ptmt1.close(); if (ptmt2 != null) ptmt2.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) throws ClassNotFoundException { updateDataInfoByTransaction(); } }
运行后的数据库表内容: