Ver Mensaje Individual
  #1 (permalink)  
Antiguo 07/12/2009, 06:49
Avatar de ARGFA
ARGFA
 
Fecha de Ingreso: noviembre-2009
Ubicación: Ciudad Guayana, Venezuela
Mensajes: 55
Antigüedad: 15 años, 4 meses
Puntos: 0
Leer campo clob de sql con java

MUY BUENOS DIAS.... ESToY COmenzando a programar con java y tengo un proyecto que realizar.... tengo que extraer un archivo xml de un url y almacenarlo tipo clob en un campo de una tabla en sql server 2000.... y el otro paso es leer ese campo clob de esa tabla y sacar un archivo xml exactamente igual como el q obtuve del url; osea exactamente lo contrario a lo anterior.
les agradaceria con el corazon quien me pueda ayudar la segunda parte; no he logrado comprender como sacar un .xml

aqui les dejo el codigo que uso para almacenar el xml en el campo clob.... tambien me serviria y no estaria demas unas opiniones acerca del codigo....saludos GRACIAS!!!!

P.D

Código:
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


public class newprueba {
  public static void main(String[] argv) throws Exception {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    Connection con = DriverManager.getConnection("jdbc:sqlserver://VEM-2936;databaseName=bdprueba",
        "sa", "1234");
     
    
    String url = "http ://150.0.56.31/ssc/wbs/Ws_Tiempo_Real.php";
    int id=1;  
    
    String resultado = getClobsContentAsString(url);
    
    insertCLOB(con, id, resultado);
        
  }
    
   public static void insertCLOB(Connection con, int id,  String fileConten)
    throws Exception {

  PreparedStatement pstmt = null;
  try {
    pstmt = con.prepareStatement("insert into prueba(id, texto) values (?, ?)");
    pstmt.setInt(1, id);
    pstmt.setString(2, fileConten);
    pstmt.executeUpdate();
  } finally {
    pstmt.close();
  }
}

public static String getClobsContentAsString(String urlAsString) throws Exception {
  InputStream content = null;
  try {
    URL url = new URL(urlAsString);
    URLConnection urlCon = url.openConnection();
    urlCon.connect();
    content = urlCon.getInputStream();

    int BUFFER_SIZE = 1024;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int length;
    byte[] buffer = new byte[BUFFER_SIZE];

    while ((length = content.read(buffer)) != -1)
    {
      output.write(buffer, 0, length);
    }
    return new String(output.toByteArray());
  } finally {
    content.close();
  }
}
}