大数据基础编程、实验和教程案例(实验四)
14.4 实验四:NoSQL 和关系数据库的操作比较
本实验对应第 6 章的内容。
14.4.1 实验目的
(1)理解四种数据库(MySQL、HBase、Redis 和 MongoDB)的概念以及不同点;
(2)熟练使用四种数据库操作常用的 Shell 命令;
(3)熟悉四种数据库操作常用的 Java API。
14.4.2 实验平台
操作系统 | Linux |
---|---|
Hadoop版本 | 3.1.3 |
MySQL 版本 | 5.6 |
HBase 版本 | 2.2.2 |
Redis 版本 | 5.0.5 |
MongoDB 版本 | 4.2.0 |
JDK 版本 | 1.8 |
Java IDE | Eclipse |
14.4.3 实验步骤
(一) MySQL 数据库操作
- 根据上面给出的 Student 表,在 MySQL 数据库中完成如下操作:
(1)在 MySQL 中创建 Student 表,并录入数据
(2)用 SQL 语句输出 Student 表中的所有记录
(3)查询 zhangsan 的 Computer 成绩
(4)修改 lisi 的 Math 成绩,改为 95
2.根据上面已经设计出的 Student 表,使用 MySQL 的 JAVA 客户端编程实现以下操作:
(1)向 Student 表中添加如下所示的一条记录:
scofield 45 89 100
import java.sql.*;
public class mysql_test {
/**
* @param args
*/
//JDBC DRIVER and DB
static final String DRIVER="com.mysql.jdbc.Driver";
static final String DB="jdbc:mysql://localhost/mysql";
//Database auth
static final String USER="root";
static final String PASSWD="hadoop";
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn=null;
Statement stmt=null;
try {
//加载驱动程序
Class.forName(DRIVER);
System.out.println("Connecting to a selected database...");
//打开一个连接
conn=DriverManager.getConnection(DB, USER, PASSWD);
//执行一个查询
stmt=conn.createStatement();
String sql="insert into student values('scofield',45,89,100)";
stmt.executeUpdate(sql);
System.out.println("Inserting records into the table successfully!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(conn!=null)
try {
conn.close();
}