3

This is the first time I got this error. This code basically gets the sum of each item sold in a particular date.

Any tips on resolving this problem? Thanks.

       Statement statement = connection.createStatement();
       String query = "SELECT itemcode, SUM(quantity) AS 'Total Sales Per Day' "
               + "FROM sales "
               + "WHERE real_pur_date = '" + date + "' "
               + "GROUP BY itemcode ";
       ResultSet rs = statement.executeQuery( query ); // this line gets the error / exception
       while( rs.next() ){
           Vector row = new Vector();
           row.add( rs.getString( "itemcode" ) );
           row.add( rs.getInt( "Total Sales Per Day" ) );
           dailyData.add( row );
       }
       statement.close();
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at com.mysql.jdbc.Util.handleNewInstance(Util.java:431)
at com.mysql.jdbc.ResultSetImpl.getInstance(ResultSetImpl.java:383)
at com.mysql.jdbc.MysqlIO.buildResultSetWithRows(MysqlIO.java:3140)
at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:491)
at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:3118)
at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2288)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2709)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2627)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1556)
at posinventory.Controller.StatisticEngine.getDailyData(StatisticEngine.java:20)
at posinventory.Statistic.refreshTableDailyStat(Statistic.java:36)
at posinventory.Statistic.refreshTableDailyStat(Statistic.java:37)
at posinventory.Statistic.refreshTableDailyStat(Statistic.java:37)
at posinventory.Statistic.refreshTableDailyStat(Statistic.java:37)
Boon
  • 687
  • 4
  • 12
  • 25
  • 3
    First thing: don't use Vector. Ever! Use an ArrayList instead. – Alexis Dufrenoy Jun 25 '12 at 16:01
  • 1
    You didn't paste enough of stacktrace to see the cycle. SOE is almost always the consequence of infinite recursion. On the odd chance that this is not the case here, the solution would be very simple: pass the JVM a parameter that increases the stack size. – Marko Topolnik Jun 25 '12 at 16:02
  • Also, what's the `date` value that causes the `StackOverflowError`? – Buhake Sindi Jun 25 '12 at 16:03
  • 1
    @Traroth, use a `Vector` if you need a dynamically resizing array in a **multi-threaded** environment? – Alex Lockwood Jun 25 '12 at 16:04
  • @Traroth: What is wrong with a vector? `The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.` – Chris Dargis Jun 25 '12 at 16:04
  • [What is a Stackoverflow error?](http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error/214758#214758) – fireshadow52 Jun 25 '12 at 16:05
  • You also didn't paste enough code to tell what's going on. The stacktrace mentions "posinventory..." and nothing in the code (except maybe the mysterious, undeclared "dailyData" variable) should reference anything like that. – janoside Jun 25 '12 at 16:05
  • Another advise: __never__ use statement, it will lead to SQL Injection attacks. Instead, use [PreparedStatement](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html)s. – Luiggi Mendoza Jun 25 '12 at 16:05
  • 2
    Keep in mind, too, that since you have a SOE, there is absolutely no problem with the line that threw the error. It's just the final straw that broke the camel's back. – Marko Topolnik Jun 25 '12 at 16:07
  • @TheEliteGentleman The date value is 20120620 – Boon Jun 25 '12 at 16:07
  • @Alex Lockwood, VanDarg: Vector is a list. And the framework offers really better lists. – Alexis Dufrenoy Jun 25 '12 at 16:08
  • @VanDarg [differences between Vector and ArrayList](http://javapapers.com/core-java/java-collection/difference-between-vector-and-arraylist-in-java/). – Luiggi Mendoza Jun 25 '12 at 16:08
  • Please specify your table structure and the size of the table. This may be the mistakes in the SQL query. – Kalai Selvan Ravi Jun 25 '12 at 16:09
  • 2
    @AlexLockwood Vector is synchronized, but that by itself doesn't make it thread-safe. That's the crux of the argument against Vector: it wastes time on locking while providing little to no benefit – Marko Topolnik Jun 25 '12 at 16:09
  • @LuiggiMendoza: I am aware of the differences, there is no way to determine the **# of threads** from this code snippet. – Chris Dargis Jun 25 '12 at 16:11
  • @Alex Lockwood: if you need a synchronzed list, there are still better solutions than Vector, for example using Collections.synchronizedList() – Alexis Dufrenoy Jun 25 '12 at 16:13
  • 1
    Hmm, I didn't know that. Thanks. But I still question whether you should have said "never use `Vector`"... can someone confirm this? – Alex Lockwood Jun 25 '12 at 16:14
  • @VanDarg looks like you haven't read the "Is there an alternate available in java for Vector?" section of that link. – Luiggi Mendoza Jun 25 '12 at 16:14
  • @AlexLockwood see http://stackoverflow.com/a/1386288/1065197 – Luiggi Mendoza Jun 25 '12 at 16:15
  • @LuiggiMendoza, I refuse to accept this as the corre--ah, crap... it's Jon Skeet. :P (thanks for the link) – Alex Lockwood Jun 25 '12 at 16:17
  • @Alex Lockwood : Vector, like Hashtable, is a relic from the pre-Java 2 era and should be avoided. – Alexis Dufrenoy Jun 25 '12 at 16:17
  • @AlexLockwood don't worry. Also, instead of `HashTable` you should use [`java.util.concurrent.ConcurrentHashMap`](http://www.ibm.com/developerworks/java/library/j-jtp07233/index.html). – Luiggi Mendoza Jun 25 '12 at 16:18
  • @LuiggiMendoza, I answer questions here because I learn something new everyday :). Thanks for the info. – Alex Lockwood Jun 25 '12 at 16:19

3 Answers3

5

I don't think the code snippet you included is the problem. Looking at the bottom of your stack trace, I see Statistic.java:37 apparently calling itself several times. Recursive calls are allowed, but they lead to a stack overflow if they are not controlled. Looks like you have a loop...

Steve11235
  • 2,849
  • 1
  • 17
  • 18
4

You really should post the code from posinventory.Statistic.refreshTableDailyStat, but I'll guess at it: in that method, line 36 calls StatisticEngine.getDailyData; line 37 calls refreshTableDailyStat recursively -- so there's your problem, Statistic.java, line 37.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
2

Remove the ::

String '+' from the query, as the JVM uses string pooling, for every type of string it calls a service to check whether that string is present or not.

Use String Builder instead.

Remove the Vector and Replace it with ArrayList, as Steve has mentioned above, check you code snippet where an unknown recursion has occurred .

Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
  • 1
    In this case it makes no difference to use a StringBuilder instead. – Roger Lindsjö Jun 25 '12 at 16:14
  • String query = "SELECT itemcode, SUM(quantity) AS 'Total Sales Per Day' " + "FROM sales " + "WHERE real_pur_date = '" + date + "' " + "GROUP BY itemcode "; can be replaced with a stringBuilder.. – Sashi Kant Jun 25 '12 at 16:17
  • Wrong both of you. Even with a StringBuilder, that type of code leads to SQL Injection attacks. Instead, you should use [PreparedStatement](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) – Luiggi Mendoza Jun 25 '12 at 16:21
  • @SashiKant Adding strings as in the question is fine, it will not be improved by a StringBuilder. – Roger Lindsjö Jun 25 '12 at 16:21
  • @LuiggiMendoza Of course a PreparedStatement should be used for the query, I was referring to the String concatenation vs StringBuilder. – Roger Lindsjö Jun 25 '12 at 16:22
  • @RogerLindsjö when you want to improve an answer, try to give the better guidance, don't try to make it a chance to long discussions or just checking the basic cases. – Luiggi Mendoza Jun 25 '12 at 16:24