This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JDBC 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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java.lang.OutOfMemoryError: Java heap space " Watch "java.lang.OutOfMemoryError: Java heap space " New topic
Author

java.lang.OutOfMemoryError: Java heap space

Eva Chow
Greenhorn

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.
-->


<Employee>
<id>100</id>
<first>Zara</first>
<last>Ali</last>
<age>35</age>
</Employee>

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 {
Class.forName(JDBC_DRIVER);

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

stmt = conn.createStatement();
createXMLTable(stmt);

//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");
pstmt.execute();

//close input stream
f.close();

// 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.print((char)c);
bos.write(c);
}
System.out.println("Done reading print XML");
System.out.println(bos.toString());
}
} catch (SQLException e) {
e.printStackTrace();
} 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) {
e.printStackTrace();
}

stmt.executeUpdate(streamingDataSql);
}
}


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
Greenhorn

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) {
//System.out.print((char)c);
bos.write(c);
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.OutOfMemoryError: Java heap space