存取数据库图片
来源:互联网 发布:大数据分析师工资 编辑:程序博客网 时间:2024/06/05 21:12
数据库存储图片的字段类型 bolb 数据大的图片为Longbolb
存:
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
try(Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?userUnicode=true&charsetEncoding=utf8", "root", "root")){
String sql="insert into tb_text(id,img,name) values(default,?,?)";
PreparedStatement ps = (PreparedStatement) connection.prepareStatement(sql);
InputStream inputStream = Test4.class.getResourceAsStream("123.jpg");
ps.setBinaryStream(1,inputStream);
ps.setString(2, "123.jpg");
if(ps.executeUpdate()==1){
System.out.println("存储成功");
}
}catch(Exception e){
e.printStackTrace();
}
取:
try(Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?userUnicode=true&charsetEncoding=utf-8", "root", "root")){
String sql="select * from tb_text where id=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, 1);
ResultSet rs = ps.executeQuery();
if(rs.next()){
try(InputStream input = rs.getBinaryStream("img")){
Path path = Paths.get("com.dp.a.jpg");
Files.copy(input, path);
}
}
}catch(Exception e){
e.printStackTrace();
}
存:
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
try(Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?userUnicode=true&charsetEncoding=utf8", "root", "root")){
String sql="insert into tb_text(id,img,name) values(default,?,?)";
PreparedStatement ps = (PreparedStatement) connection.prepareStatement(sql);
InputStream inputStream = Test4.class.getResourceAsStream("123.jpg");
ps.setBinaryStream(1,inputStream);
ps.setString(2, "123.jpg");
if(ps.executeUpdate()==1){
System.out.println("存储成功");
}
}catch(Exception e){
e.printStackTrace();
}
取:
try(Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?userUnicode=true&charsetEncoding=utf-8", "root", "root")){
String sql="select * from tb_text where id=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, 1);
ResultSet rs = ps.executeQuery();
if(rs.next()){
try(InputStream input = rs.getBinaryStream("img")){
Path path = Paths.get("com.dp.a.jpg");
Files.copy(input, path);
}
}
}catch(Exception e){
e.printStackTrace();
}
阅读全文
0 0
- 数据库存取图片
- 数据库中存取图片
- 数据库存取图片
- C#数据库存取图片
- 图片存取数据库(java)
- 数据库存取图片
- 数据库中存取图片
- 图片的数据库存取
- oracle数据库存取图片
- java数据库 存取图片
- 数据库中图片存取
- 数据库存取图片类
- 数据库存取图片类
- 数据库图片的存取
- Android 数据库存取图片
- Android 数据库存取图片
- Sqlite数据库图片存取
- Android 数据库存取图片
- art-template学习之路
- ROS功能包文件结构及命令
- HBase启动时util.NativeCodeLoader错误
- 2017-8-7
- 算法:寻找树中两个结点的最低公共祖先
- 存取数据库图片
- 布隆过滤器
- A. Key races
- JavaScript(7)__Js基础<构造>
- lamda表达式
- linux下date命令用法
- hdu 1698 Just a Hook
- I
- C++调用构造函数过程