wood burning stoves
The moose likes JDBC and Relational Databases and the fly likes java.lang.OutOfMemoryError: Java heap space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "java.lang.OutOfMemoryError: Java heap space " Watch "java.lang.OutOfMemoryError: Java heap space " New topic

java.lang.OutOfMemoryError: Java heap space

Eva Chow

Joined: Oct 08, 2012
Posts: 5
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
Eva Chow

Joined: Oct 08, 2012
Posts: 5
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) {
I agree. Here's the link: http://aspose.com/file-tools
subject: java.lang.OutOfMemoryError: Java heap space
It's not a secret anymore!