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