mysql插入Clob字段的实例

数据库 MySQL
mysql插入Clob字段的方法未必人人都会,下面以实例的方式为您介绍mysql插入Clob字段是如何实现的,如果您对此有兴趣的话,不妨一看。

如果要实现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添加删除主键的方法

责任编辑:段燃 来源: 互联网
相关推荐

2009-06-26 10:19:00

Clob字段Hibernate

2009-07-15 17:01:29

iBATIS操作CLO

2017-09-05 09:18:54

OracleCLOB大数据

2009-08-27 16:00:03

C#静态字段C#实例字段

2010-06-28 09:59:47

SQL Server自

2010-11-22 11:55:23

MySQL字段

2010-09-13 14:57:29

2009-11-19 10:43:54

Oracle Clob

2010-11-23 15:27:00

MySQL添加字段

2010-11-22 12:59:30

MySQL字段

2010-11-22 12:14:55

MySQL字段

2010-09-03 11:16:54

SQL删除

2010-07-09 12:49:41

SQL Server自

2010-03-30 13:49:15

Oracle clob

2010-11-22 11:38:08

MySQL添加字段

2021-04-08 10:55:53

MySQL数据库代码

2010-11-22 10:29:42

MySQL字段自增长

2010-10-08 09:17:06

mysql修改字段

2010-10-08 14:59:00

MySql字段

2020-04-24 21:41:45

MySQL数据库插入数据
点赞
收藏

51CTO技术栈公众号