>데이터 베이스 >MySQL 튜토리얼 >Clob 데이터 유형의 값을 테이블에 삽입하는 JDBC 예제를 작성하시겠습니까?

Clob 데이터 유형의 값을 테이블에 삽입하는 JDBC 예제를 작성하시겠습니까?

王林
王林앞으로
2023-08-25 16:21:081316검색

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

아래에 설명된 대로 데이터베이스에 MyData라는 테이블이 이미 있다고 가정합니다.

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

JDBC 프로그램을 사용하여 clob 데이터 형식에 int 값을 삽입해야 하는 경우 파일 내용을 읽어서 데이터베이스 행으로 설정하는 방법을 사용해야 합니다. ReadyStatement 인터페이스는 이러한 메소드를 제공합니다.

void setCharacterStream(int parameterIndex, Reader reader) 메소드는 리더 객체의 내용을 주어진 인덱스의 매개변수 값으로 설정합니다. 이 방법의 다른 변형은 다음과 같습니다.

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

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

void setClob(intparameterIndex, Clob x ) 메소드는 주어진 java .sql.Clob 객체를 주어진 인덱스의 매개변수 값으로 설정합니다. 이 방법의 다른 변형은 다음과 같습니다.

  • void setClob(intparameterIndex, Reader reader)

  • void setClob (int parameterIndex, Reader reader, long length)

다음 두 가지 방법 중 하나를 사용하여 값을 얻을 수 있습니다. Clob 데이터 유형으로 설정됩니다.

Example h3>

다음 예제에서는 setClob() 메서드를 사용하여 Clob 데이터 유형에 값을 설정합니다.

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

MySQL Workbench를 사용하여 레코드의 clob 값을 보려고 하면 다음과 같이 텍스트 데이터가 삽입된 것을 볼 수 있습니다.

Clob 데이터 유형의 값을 테이블에 삽입하는 JDBC 예제를 작성하시겠습니까?

위 내용은 Clob 데이터 유형의 값을 테이블에 삽입하는 JDBC 예제를 작성하시겠습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제