Streaming XML : XML Data « XML « Java






Streaming XML

    
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainClass {
  public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    
    String streamingDataSql = "CREATE TABLE XML_Data (id INTEGER, Data LONG)";
    try {
      stmt.executeUpdate("DROP TABLE XML_Data");
    } catch (SQLException se) {
      if (se.getErrorCode() == 942)
        System.out.println("Error dropping XML_Data table:" + se.getMessage());
    }
    stmt.executeUpdate(streamingDataSql);

    File f = new File("employee.xml");
    long fileLength = f.length();
    FileInputStream fis = new FileInputStream(f);
    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO XML_Data VALUES (?,?)");
    pstmt.setInt(1, 100);
    pstmt.setAsciiStream(2, fis, (int) fileLength);
    pstmt.execute();
    fis.close();
    ResultSet rset = stmt.executeQuery("SELECT Data FROM XML_Data WHERE id=100");
    if (rset.next()) {
      InputStream xmlInputStream = rset.getAsciiStream(1);
      int c;
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      while ((c = xmlInputStream.read()) != -1)
        bos.write(c);
      System.out.println(bos.toString());
    }
    conn.close();
  }
}

   
    
    
  








Related examples in the same category

1.extends DefaultHandler to create a XML binding
2.Your won XML binding
3.Parse and format xs:dateTime values
4.Replaces all XML character entities with the character they represent.
5.Sniffed Xml InputStream to find out the declaration and file encoding
6.Whether the given character is a valid XML space.
7.JAXP 1.3 Datatype API
8.Common XML constants.
9.Sniffed Xml Reader
10.Source To InputSource
11.Utility class for working with xml data
12.whether the given 32 bit character is a valid XML 1.1 character.