码迷,mamicode.com
首页 > 数据库 > 详细

Java开发工程师(Web方向) - 03.数据库开发 - 期末考试

时间:2017-09-08 19:34:13      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:sql   back   连接   while   table   exec   strong   nec   在线   

期末考试

 

编程题

本编程题包含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();
    }
}

 

 

 

 

 

 

Java开发工程师(Web方向) - 03.数据库开发 - 期末考试

标签:sql   back   连接   while   table   exec   strong   nec   在线   

原文地址:http://www.cnblogs.com/FudgeBear/p/7495985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!