如果要實現mysql插入Clob字段,應該采用什麼方法呢?下面這個例子就將為您演示如何實現mysql插入Clob字段的方法,供您參考。
- 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.executeUpdate();
- pstmt.clearParameters();
- pstmt.close();
- fin.close();
- Statement stmt = conn.createStatement();
- ResultSet result = stmt.executeQuery("SELECT * FROM files");
- result.next();
- String description = result.getString(1);
- Blob blob = result.getBlob(2);
- System.out.println("描述:" + 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("找不到驅動");
- e.printStackTrace();
- }
- catch(SQLException e) {
- e.printStackTrace();
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
mysql插入Clob字段的實例介紹。
常見MySql字段的默認長度
mysql中int數據類型長度的問題
MySQL中INSERT的一般用法
修改mysql字段順序的方法
mysql添加刪除主鍵的方法