发布网友 发布时间:2022-04-07 17:43
共2个回答
懂视网 时间:2022-04-07 22:04
来两个Demo看看JDBC是怎么操作文本和二进制文件的。
CLOB:
package com.wxisme.jdbcclob; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.sql.Clob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; /** * 数据库中对文本对象的操作。 * @author wxisme * */ public class CLOBTest { /** * 标准时间对象转换成long * @param date * @return long */ public static long dateTolong(String date) { DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); long time = 0; try { time = format.parse(date).getTime(); } catch (ParseException e) { e.printStackTrace(); } return time; } /** * 向数据库中插入一条带有Clob文本对象字段的记录 */ public void test1() { Connection conn = null; PreparedStatement ps = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String c = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; try { conn = DriverManager.getConnection(c, "root", "1234"); String sql = "insert into student (name,pswd,time,stime,myImg) values(?,?,?,?,?)"; ps = conn.prepareStatement(sql); java.sql.Date date = new java.sql.Date(dateTolong("2015-5-14 9:25:32")); Timestamp stime = new Timestamp(dateTolong("2015-5-14 9:25:32")); ps.setString(1, "张三"); ps.setString(2, "123456"); ps.setDate(3, date); ps.setTimestamp(4, stime); ps.setClob(5, new FileReader("e:" +File.separator + "a.txt")); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static void main(String[] args) { //从数据库中读取Clob文本对象并输出 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; Reader r = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String c = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; try { conn = DriverManager.getConnection(c, "root", "1234"); String sql = "select myImg from student where id=?"; ps = conn.prepareStatement(sql); ps.setInt(1, 1); rs = ps.executeQuery(); while(rs.next()) { Clob clob = rs.getClob("myImg"); r = clob.getCharacterStream(); int t = 0; while((t=r.read()) != -1) { System.out.println((char)t); } } } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { r.close(); } catch (IOException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
BLOB:
package com.wxisme.jdbcblob; 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.io.OutputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * 数据库操作二进制文件 Blob类的使用 * @author wxisme * */ public class BLOBTest { /** * 向数据库中插入二进制 */ public void test1() { Connection conn = null; PreparedStatement ps = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String c = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; try { conn = DriverManager.getConnection(c, "root", "1234"); String sql = "insert into student (img) values(?)"; ps = conn.prepareStatement(sql); ps.setBlob(1, new FileInputStream("e:" + File.separator + "b.jpg")); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); }finally { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { //从数据库中读取二进制文件并恢复 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; InputStream r = null; OutputStream os = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String c = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; try { conn = DriverManager.getConnection(c, "root", "1234"); String sql = "select img from student where id=?"; ps = conn.prepareStatement(sql); ps.setInt(1, 3); rs = ps.executeQuery(); os = new FileOutputStream(new File("e:/e.jpg")); while(rs.next()) { Blob blob = rs.getBlob("img"); r = blob.getBinaryStream(); int t = 0; while((t=r.read()) != -1) { os.write(t); } } } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { r.close(); } catch (IOException e) { e.printStackTrace(); } try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }
JDBC处理文本和二进制文件
标签:
热心网友 时间:2022-04-07 19:12
用流读入图片,再用byte[]数组读取转换流中的图片数据转成二进制.接着用JDBC把二进制插入数据库存 oFileInputStream fis = null;// 处理硬盘的低端,输入流 o try { o fis = new FileInputStream( "图片路径" );// 低端流绑定硬盘上的文件 o o byte [] buffer = new byte[ 1024 ]; o while ( true ) {// 循环读取流里的数据 o int len = fis.read( buffer ); o if ( -1 == len ) {// 如果已经读到文件末尾 o break; o } o ........//JDBC插入代码只要把上面的buffer循环插入数据库存o } o } catch ( Exception e ) { o e.printStackTrace(); o } finally { o try { o fis.close(); o } catch ( Exception e ) { o e.printStackTrace(); o } o } o}