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

    importjava.io.*; importjava.sql.*; publicclassDBTest{ publicstaticvoidmain(String[]args){ Stringdriver="com.mysql.jdbc.Driver"; Stringurl="jdbc:mysql://localhost:3306/upload?useUnicode=true&characterEncoding=Big5"; Stringuser="caterpillar"; Stringpassword="123456"; try{ Class.forName(driver); Connectionconn=DriverManager.getConnection(url,user,password); Filefile=newFile("./logo_phpbb.jpg"); intlength=(int)file.length(); InputStreamfin=newFileInputStream(file); PreparedStatementpstmt=conn.prepareStatement( "INSERTINTOfilesVALUES(?,?)"); pstmt.setString(1,"Logo"); pstmt.setBinaryStream(2,fin,length); pstmt.executeUpdate(); pstmt.clearParameters(); pstmt.close(); fin.close(); Statementstmt=conn.createStatement(); ResultSetresult=stmt.executeQuery("SELECT*FROMfiles"); result.next(); Stringdescription=result.getString(1); Blobblob=result.getBlob(2); System.out.println("描述:"+description); FileOutputStreamfout=newFileOutputStream("./logo_phpbb_2.jpg"); fout.write(blob.getBytes(1,(int)blob.length())); fout.flush(); fout.close(); stmt.close(); conn.close(); } catch(ClassNotFoundExceptione){ System.out.println("找不到驱动"); e.printStackTrace(); } catch(SQLExceptione){ e.printStackTrace(); } catch(IOExceptione){ e.printStackTrace(); } } }

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

【编辑推荐】

常见MySql字段的默认长度

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

MySQL中INSERT的一般用法

修改mysql字段顺序的方法

mysql添加删除主键的方法