mysql大数据处理
这里说的大数据可不是高大上的大数据
只是大的数据而已
比较大的数据比如mp3
我们先试试把大数据写入到数据库中
说实话,有些人就只管复制粘贴
从来不自己写博客
我都是一行一行代码写
还加上注释
public static void main(String[] args) {
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
//地址
String url = "jdbc:mysql://localhost:3306/abc?useSSL=false";
//账号和密码
String username = "root";
String password = "123";
//连接数据库
Connection connection = DriverManager.getConnection(url, username, password);
//来一条sql语句
String sql = "insert into tb_music values(?,?,?)";
//获取statement对象
PreparedStatement statement = connection.prepareStatement(sql);
//设置参数替换?号
statement.setInt(1, 1);
statement.setString(2, "七里香.mp3");
//把文件转换为byte[]
byte[] bytes = IOUtils.toByteArray(new FileInputStream("F://七里香.mp3"));
//定义blob
Blob blob = new SerialBlob(bytes);
//设置blob
statement.setBlob(3, blob);
//执行更新
statement.executeUpdate();
//最后要关闭statement,connection
statement.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
然后是读取,一样的道理
public static void main(String[] args) {
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
//地址
String url = "jdbc:mysql://localhost:3306/abc?useSSL=false";
//账号和密码
String username = "root";
String password = "123";
//连接数据库
Connection connection = DriverManager.getConnection(url, username, password);
//来一条sql语句
String sql = "select * from tb_music";
//获取PreparedStatement对象
PreparedStatement statement = connection.prepareStatement(sql);
//获取结果集对象
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
InputStream stream = resultSet.getBinaryStream(3);
FileOutputStream outputStream = new FileOutputStream("D://hehe.mp3");
IOUtils.copy(stream, outputStream);
}
//最后要关闭rs,statement,connection
resultSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}