标签:sql back 连接 while table exec strong nec 在线
本编程题包含4个小题,覆盖知识点从基础的JDBC、连接池到MyBatis。
有一款在线教育产品“天天向上”主要实现了在手机上查看课程表的功能。该产品的后端系统有一张保存了所有客户课程信息的数据库表,表结构如下:
请使用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(); } }
输出:
使用游标方式读取题目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