mysql插入Clob字段的实例
创始人
2024-07-15 14:20:44
0

如果要实现mysql插入Clob字段,应该采用什么方法呢?下面这个例子就将为您演示如何实现mysql插入Clob字段的方法,供您参考。

  1. import java.io.*;  
  2. import java.sql.*;  
  3.                                                                                  
  4. public class DBTest {  
  5.     public static void main(String[] args) {  
  6.         String driver = "com.mysql.jdbc.Driver";  
  7.         String url = "jdbc:mysql://localhost:3306/upload?useUnicode=true&characterEncoding=Big5";  
  8.         String user = "caterpillar";  
  9.         String password = "123456";  
  10.         try {  
  11.             Class.forName(driver);  
  12.             Connection conn = DriverManager.getConnection(url, user, password);  
  13.  
  14.               
  15.             File file = new File("./logo_phpbb.jpg");  
  16.             int length = (int) file.length();  
  17.             InputStream fin = new FileInputStream(file);  
  18.               
  19.             PreparedStatement pstmt = conn.prepareStatement(  
  20.                        "INSERT INTO files VALUES(?, ?)");  
  21.             pstmt.setString(1, "Logo");  
  22.             pstmt.setBinaryStream (2, fin, length);  
  23.             pstmt.executeUpdate();  
  24.             pstmt.clearParameters();  
  25.             pstmt.close();  
  26.             fin.close();  
  27.               
  28.             Statement stmt = conn.createStatement();  
  29.             ResultSet result = stmt.executeQuery("SELECT * FROM files");  
  30.             result.next();  
  31.             String description = result.getString(1);  
  32.             Blob blob = result.getBlob(2);  
  33.                
  34.             System.out.println("描述:" + description);  
  35.             FileOutputStream fout = new FileOutputStream("./logo_phpbb_2.jpg");                
  36.             fout.write(blob.getBytes(1, (int)blob.length()));  
  37.             fout.flush();  
  38.             fout.close();  
  39.                                                                                  
  40.             stmt.close();  
  41.             conn.close();  
  42.         }  
  43.         catch(ClassNotFoundException e) {  
  44.             System.out.println("找不到驱动");  
  45.             e.printStackTrace();  
  46.         }  
  47.         catch(SQLException e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.         catch(IOException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.     }  
  54. }  
  55.  

mysql插入Clob字段的实例介绍。

 

 

【编辑推荐】

常见MySql字段的默认长度

mysql中int数据类型长度的问题

MySQL中INSERT的一般用法

修改mysql字段顺序的方法

mysql添加删除主键的方法

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...