Home  >  Article  >  Database  >  Write a JDBC example to insert a value of Clob data type into a table?

Write a JDBC example to insert a value of Clob data type into a table?

王林
王林forward
2023-08-25 16:21:081229browse

编写一个 JDBC 示例来将 Clob 数据类型的值插入表中?

Suppose we already have a table named MyData in the database, which is described as follows.

+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| Name    | varchar(255) | YES  |     | NULL    |       |
| Article | longtext     | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+

If you need to insert the value int into the clob data type using a JDBC program, you need to use a method that reads the file content and sets it as a database row. The ReadyStatement interface provides such methods.

void setCharacterStream(int parameterIndex, Reader reader) Method sets the contents of the reader object to the value of the parameter at the given index. Other variations of this method are:

  • void setCharacterStream(intparameterIndex, Reader reader, int length)

  • ##void setCharacterStream(intparameterIndex, Reader reader , long length)

void setClob(intparameterIndex, Clob x) method sets the given java .sql.Clob object as the value of the parameter at the given index. Other variations of this method are:

  • void setClob(int parameterIndex, Reader reader)

  • ##void setClob (int parameterIndex, Reader reader, long length)
  • You can use one of these two methods to set a value to the Clob data type.

Example

The following example uses the setClob() method to set a value to the Clob data type. h3>
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class InsertingValueInClob {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Inserting values
      String query = "INSERT INTO MyData(Name, Article ) VALUES (?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "JavaFX");
      FileReader reader = new FileReader("E:\images\javafx.txt");
      pstmt.setClob(2, reader);
      pstmt.execute();
      System.out.println("Data inserted");
   }
}

Output

Connection established......
Table Created......
Contents of the table are:
JavaFX
E:\images\MyData_clob_output1.txt

If you try to use MySQL Workbench to view the clob value in the record, you can see the text data inserted as follows:

The above is the detailed content of Write a JDBC example to insert a value of Clob data type into a table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete