//新的一年祝各人开年好运
- //创建数据库:CREATE TABLE imags ( id int(11) NOT NULL, name varchar(255) DEFAULT NULL, imag longblob, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=latin1;//举行代码演示:package com.memory.imags.cckj;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.sql.Blob;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class DButil { private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/test";//test指的是数据库名 private static String user = "root";//毗连数据库账号 private static String password = "sa123456";//毗连数据库暗码 private static Connection conn; private static PreparedStatement ps; private static ResultSet rs; /** * 得到毗连 这里单独封装一个获取数据库毗连的方法 */ public static void getConnection() { try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed())//判断是否毗连到数据库 System.out.println("Succeeded connecting to the Database!"); } catch (SQLException e) { System.out.println("jdbc:"+e.getMessage() ); } catch (ClassNotFoundException e) { System.out.println("jdbc:"+e.getMessage()); } } /** * 关闭毗连 这里单独封装了一个close方法用来关闭资源 */ public static void close() { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { System.out.println("毗连关闭异常:"+e.getMessage()); } }public void add() { //sql插入语句 String sql = "insert into imags values(?,?,?) "; //将本舆图片插入到数据库中 File file=new File("C:\\Users\\mldn\\Desktop\\login1.jpg"); getConnection();//调用方法获取数据库毗连 try { FileInputStream fi = new FileInputStream(file); ps = conn.prepareStatement(sql);//执行sql语句 ps.setInt(1, 2); ps.setString(2, "image1"); ps.setBlob(3, fi); int f = ps.executeUpdate(); System.out.println("打印输出:"+f); if (f > 0) { System.out.println("插入乐成"); } else { System.out.println("插入失败"); } //养成好习惯,关闭资源 ps.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); }} public void select() { //获取数据的毗连 getConnection(); Blob get_image; //吸收从数据获取到的图片范例的数据 String sql = "select * from imags"; try {// 将读取到的图片存放到指定的路径中,并定名为bb.jpg FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\mldn\\Desktop\\img\\bb.jpg"); ps = conn.prepareStatement(sql); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()) { get_image = resultSet.getBlob("imag"); //将读取到的Blob对象转成字节省 InputStream inputStream = get_image.getBinaryStream(); int a; byte b[] = new byte[1024]; while ((a = inputStream.read(b)) != -1) { fileOutputStream.write(b, 0, a); } } ps.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}//测试方法public static void main(String[] args) { DButil dbutil = new DButil(); //调用方法向数据中存入图片 dbutil.add(); //调用方法将数据库中的图片读出,并放到当地。 dbutil.select(); }}
复制代码 图片乐成插入到数据库中:
图片乐成有数据读取到当地:
来源:https://blog.csdn.net/weixin_46609492/article/details/112079876
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |