• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.OutOfMemoryError: Java heap space  RSS feed

Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write and read an XML file to and from database. The xml file has no problem written to the database, however, when it tried to read from the database, it said, "java.lang.OutOfMemoryError: Java heap space".

The XML file
<?xml version="1.0" encoding="UTF-8"?>
To change this template, choose Tools | Templates
and open the template in the editor.


The Code

import java.sql.*;
import java.io.*;

public class JDBCTable {

static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
static final String DB_URL = "jdbcracle:thin:@localhost:1521:XE";
static final String PASSWORD = "123";
static final String USER = "ccc";

public static void main(String[] args) {
Connection conn;
PreparedStatement pstmt;
Statement stmt;
ResultSet rs;

try {

conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);

stmt = conn.createStatement();

//Open a FileInputStream
File xml_file = new File("C:\\Documents and Settings\\cccc\\My Documents\\NetBeansProjects\\JDBC\\src\\com\\jdbc\\XML_Data.xml");
long fileLength = xml_file.length();
FileInputStream f = new FileInputStream(xml_file);

//Create PreparedStatment and stream data

String sql = "INSERT INTO XML_Data(id,Data) VALUES(?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 100);
pstmt.setAsciiStream(2, f, (int) fileLength);
System.out.println("Create PreparedStatment and stream data F");

//close input stream

// do a query to get the row
System.out.println("select data");
sql = "SELECT Data FROM XML_Data WHERE id=100";

rs = stmt.executeQuery(sql);
System.out.println("pass executeQuery");

//Get the first row
if (rs.next()) {
//Retrieve data from input stream
InputStream xmlInputStream = rs.getAsciiStream("Data");
int c;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((c = xmlInputStream.read()) != 1) {
System.out.println("Done reading print XML");
} catch (SQLException e) {
} catch (Exception e) {
} finally {

static public void createXMLTable(Statement stmt) throws SQLException {
System.out.println("Creating XML_Data table");
//Create SQL Statement
String streamingDataSql = "CREATE TABLE XML_Data (id INTEGER, Data LONG)";

//Drop table first if it exists.
try {
stmt.executeUpdate("DROP TABLE XML_Data");
} catch (SQLException e) {


The output error messages
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2271)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:122)
at com.jdbc.JDBCTable.main(JDBCTable.java:68)
Java Result: 1
Carissa Lew
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind. I figured it out. I have an infinite loop.
The xmlInputStream.read() should check for != -1. And I had !=1

while ((c = xmlInputStream.read()) != -1) {
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!