This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
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.*;

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 ( {
//Retrieve data from input stream
InputStream xmlInputStream = rs.getAsciiStream("Data");
int c;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((c = != 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(
at com.jdbc.JDBCTable.main(
Java Result: 1
Eva Chow

Joined: Oct 08, 2012
Posts: 5
Never mind. I figured it out. I have an infinite loop.
The should check for != -1. And I had !=1

while ((c = != -1) {
I agree. Here's the link:
subject: java.lang.OutOfMemoryError: Java heap space
Similar Threads
Need help with my first JDBC program
java.sql.SQLException: ORA-01006: bind variable does not exist
JDBC: What is the concrete class that implements the jdbc interfaces
Not to execute console based jdbc application
How to create SQL server2005 database on remote machine using jdbc?