What method should I use to insert a Clob field in mysql? The following example shows how to insert the Clob field in mysql for your reference.
- Import java. io .*;
- Import java. SQL .*;
- Public class DBTest {
- Public static void main (String [] args ){
- String driver = "com. mysql. jdbc. Driver ";
- String url = "jdbc: mysql: // localhost: 3306/upload? UseUnicode = true & characterEncoding = Big5 ";
- String user = "caterpillar ";
- String password = "123456 ";
- Try {
- Class. forName (driver );
- Connection conn = DriverManager. getConnection (url, user, password );
-
- File file = new File ("./logo_phpbb.jpg ");
- Int length = (int) file. length ();
- InputStream fin = new FileInputStream (file );
- PreparedStatement pstmt = conn. prepareStatement (
- "Insert into files VALUES (?, ?) ");
- Pstmt. setString (1, "Logo ");
- Pstmt. setBinaryStream (2, fin, length );
- Pstmt.exe cuteUpdate ();
- Pstmt. clearParameters ();
- Pstmt. close ();
- Fin. close ();
- Statement stmt = conn. createStatement ();
- ResultSet result = stmt.exe cuteQuery ("SELECT * FROM files ");
- Result. next ();
- String description = result. getString (1 );
- Blob blob = result. getBlob (2 );
- System. out. println ("description:" + description );
- FileOutputStream fout = new FileOutputStream ("./logo_phpbb_2.jpg ");
- Fout. write (blob. getBytes (1, (int) blob. length ()));
- Fout. flush ();
- Fout. close ();
- Stmt. close ();
- Conn. close ();
- }
- Catch (ClassNotFoundException e ){
- System. out. println ("driver not found ");
- E. printStackTrace ();
- }
- Catch (SQLException e ){
- E. printStackTrace ();
- }
- Catch (IOException e ){
- E. printStackTrace ();
- }
- }
- }
-
Introduction to the instance for inserting the Clob field in mysql.
Default length of common MySql Fields
Length of int data type in mysql
General INSERT usage in MySQL
How to modify the order of mysql Fields
How to add or delete a primary key in mysql