'Read CLOBs data from database'에 해당되는 글 1건

  1. 2010.02.19 Read CLOBs data from database
01.JAVA/Java2010. 2. 19. 17:45
반응형
import java.io.File;
import java.io.FileWriter;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Main {
  private static String url = "jdbc:oracle:thin:@localhost:1521:javaDemo";

  private static String username = "java";

  private static String password = "welcome";

  public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);
    PreparedStatement stmt = conn.prepareStatement("SELECT name, description, data FROM documents ");
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
      String name = resultSet.getString(1);
      String description = resultSet.getString(2);
      File data = new File("C:\\a.txt");
      Reader reader = resultSet.getCharacterStream(3);
      FileWriter writer = new FileWriter(data);
      char[] buffer = new char[1];
      while (reader.read(buffer) > 0) {
        writer.write(buffer);
      }
      writer.close();
    }
    conn.close();
  }
}

   
   
Posted by 1010