jdbc 3.0

1.将Blob、Clob类型数据保存到数据库

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileReader;
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.PreparedStatement;
 7 
 8 
 9 public class Test4 {
10     /**
11      * 将图片、文本保存在数据库
12      * Blob、Clob
13      * @author 容杰龙
14      */
15     public static void main(String[] args) {
16         try {
17             Class.forName("com.mysql.jdbc.Driver");
18             String url="jdbc:mysql://localhost:3306/rjl";
19             String user="rjl";
20             String password="123";
21             Connection conn = DriverManager.getConnection(url, user, password);
22             String sql1="insert into pic(img,msg) values(?,?)";
23             
24             File file=new File("C:/Users/Rong/Desktop/qq.png");
25             FileInputStream inputStream = new FileInputStream(file);
26             
27             FileReader reader = new FileReader("C:/Users/Rong/Desktop/msg.txt");
28             
29             PreparedStatement ps = conn.prepareStatement(sql1);
30             //字节流-二进制图像
31             ps.setBlob(1, inputStream);
32             //字符流
33             ps.setClob(2, reader);
34             int count = ps.executeUpdate();
35             if (count>0) {
36                 System.out.println("添加成功!");
37             }
38         } catch (Exception e) {
39             e.printStackTrace();
40         }
41 
42     }
43 
44 }
View Code

2.从数据库表里读取Blob、Clob类型数据

 1 package com.rong.web;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.FileWriter;
 5 import java.io.InputStream;
 6 import java.io.Reader;
 7 import java.sql.Connection;
 8 import java.sql.DriverManager;
 9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 
12 public class Test5 {
13 
14 
15     /**
16      * 读取MySQL数据库Blob、Clob类型的数据到硬盘
17      * @author 容杰龙
18      */
19     public static void main(String[] args) {
20         try {
21             Class.forName("com.mysql.jdbc.Driver");
22             String url="jdbc:mysql://localhost:3306/rjl?user=root&password=123123";
23             Connection conn = DriverManager.getConnection(url);
24             String sql="select * from pic where id=?";
25             PreparedStatement ps = conn.prepareStatement(sql);
26             ps.setInt(1, 1);
27             ResultSet rs = ps.executeQuery();
28             while(rs.next()){
29                 //获取字节输入流
30                 InputStream inputStream = rs.getAsciiStream("img");
31                 //获取字符输入流
32                 Reader reader = rs.getCharacterStream("msg");
33                 byte[] bytes=new byte[1024];
34                 char[] chars=new char[1024];
35                 int len=0;
36                 //创建文件输出流,设置文件存储的硬盘位置
37                 FileOutputStream fos = new FileOutputStream("C:/Users/Administrator/Desktop/jdbc.png");
38                 FileWriter fw = new FileWriter("C:/Users/Administrator/Desktop/jdbc.txt");
39                 //字节输出
40                 while ((len=inputStream.read(bytes))!=-1) {
41                     fos.write(bytes, 0, len);
42                 }
43                 fos.close();
44                 inputStream.close();
45                 len=0;
46                 //字符输出
47                 while ((len=reader.read(chars))!=-1) {
48                     fw.write(chars, 0, len);
49                 }
50                 fw.close();
51                 reader.close();
52                 
53             }
54         } catch (Exception e) {
55             e.printStackTrace();
56         }
57 
58     }
59 
60 }
 1 package com.rong.web;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.FileWriter;
 5 import java.io.InputStream;
 6 import java.io.Reader;
 7 import java.sql.Blob;
 8 import java.sql.Clob;
 9 import java.sql.Connection;
10 import java.sql.DriverManager;
11 import java.sql.PreparedStatement;
12 import java.sql.ResultSet;
13 
14 public class Test6 {
15 
16     /**
17      * 读取MySQL数据库Blob、Clob类型的数据到硬盘
18      * @author 容杰龙
19      */
20     public static void main(String[] args) {
21         try {
22             Class.forName("com.mysql.jdbc.Driver");
23             String url="jdbc:mysql://localhost:3306/rjl?user=root&password=123123";
24             Connection conn = DriverManager.getConnection(url);
25             String sql="select * from pic where id=?";
26             PreparedStatement ps = conn.prepareStatement(sql);
27             ps.setInt(1, 1);
28             ResultSet rs = ps.executeQuery();
29             while(rs.next()){
30                 //获取blob对象
31                 Blob blob = rs.getBlob("img");
32                 //获取二进制字节输入流
33                 InputStream binaryStream = blob.getBinaryStream();
34                 //////////////////////////////////////////////////
35                 
36                 //获取clob对象
37                 Clob clob = rs.getClob("msg");
38                 //获取字符输入流
39                 Reader characterStream = clob.getCharacterStream();
40                 
41                 byte[] bytes=new byte[1024];
42                 char[] chars=new char[1024];
43                 int len=0;
44                 //创建文件输出流,设置文件存储的硬盘位置
45                 FileOutputStream fos = new FileOutputStream("C:/Users/Administrator/Desktop/jdbc.png");
46                 FileWriter fw = new FileWriter("C:/Users/Administrator/Desktop/jdbc.txt");
47                 //字节输出
48                 /////////////////////////////////
49 //                byte[] bs = rs.getBytes("img");
50 //                fos.write(bs);
51                 ////////////////////////////////////
52                 while ((len=binaryStream.read(bytes))!=-1) {
53                     fos.write(bytes, 0, len);
54                 }
55                 fos.close();
56                 binaryStream.close();
57                 len=0;
58                 //字符输出
59                 while ((len=characterStream.read(chars))!=-1) {
60                     fw.write(chars, 0, len);
61                 }
62                 fw.close();
63                 characterStream.close();
64                 
65             }
66         } catch (Exception e) {
67             e.printStackTrace();
68         }
69 
70     }
71 
72 }
View Code
原文地址:https://www.cnblogs.com/57rongjielong/p/7775294.html