Re: Problems with Windows Memory executing java program
Sal wrote:
The class that I use for execute a query on a mysql database is:
<CODE>
package tk_data_3_0b;
import java.sql.*;
public class ExecuteQueryDB {
String Val_min;
String Val_max;
String S_max;
String param1;
String param2;
String param3;
String param4;
String param5;
String param6;
String param7;
String param8;
String param9;
String param10;
String param11;
String param12;
String param13;
String param14;
I'd replace the above with
static final int MAXPARAMS = 14;
String[] param = new String[MAXPARAMS];
ResultSet resultset;
public ExecuteQueryDB (String sql, int nparam){
if (nparam > MAXPARAMS)
throw new IllegalArgumentException(
"Max " + NPARAMS + " params!");
String DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/db";
try {
Class.forName(DRIVER);
Connection connection = null;
try {
connection = DriverManager.getConnection( DB_URL, "user", "pw");
Statement statement = connection.createStatement();
resultset = statement.executeQuery(sql);
while (resultset.next()) {
switch (nparam){
case 1:{
param1 = resultset.getString(1);
break;
}
case 2:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
break;
}
case 3:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
break;
}
case 4:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
break;
}
case 5:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
break;
}
case 6:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
param6 = resultset.getString(6);
break;
}
case 7:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
param6 = resultset.getString(6);
param7 = resultset.getString(7);
break;
}
case 8:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
param6 = resultset.getString(6);
param7 = resultset.getString(7);
param8 = resultset.getString(8);
break;
}
case 9:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
param6 = resultset.getString(6);
param7 = resultset.getString(7);
param8 = resultset.getString(8);
param9 = resultset.getString(9);
break;
}
case 10:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
param6 = resultset.getString(6);
param7 = resultset.getString(7);
param8 = resultset.getString(8);
param9 = resultset.getString(9);
param10 = resultset.getString(10);
break;
}
case 11:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
param6 = resultset.getString(6);
param7 = resultset.getString(7);
param8 = resultset.getString(8);
param9 = resultset.getString(9);
param10 = resultset.getString(10);
param11 = resultset.getString(11);
break;
}
case 12:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
param6 = resultset.getString(6);
param7 = resultset.getString(7);
param8 = resultset.getString(8);
param9 = resultset.getString(9);
param10 = resultset.getString(10);
param11 = resultset.getString(11);
param12 = resultset.getString(12);
break;
}
case 13:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
param6 = resultset.getString(6);
param7 = resultset.getString(7);
param8 = resultset.getString(8);
param9 = resultset.getString(9);
param10 = resultset.getString(10);
param11 = resultset.getString(11);
param12 = resultset.getString(12);
param13= resultset.getString(13);
break;
}
case 14:{
param1 = resultset.getString(1);
param2 = resultset.getString(2);
param3 = resultset.getString(3);
param4 = resultset.getString(4);
param5 = resultset.getString(5);
param6 = resultset.getString(6);
param7 = resultset.getString(7);
param8 = resultset.getString(8);
param9 = resultset.getString(9);
param10 = resultset.getString(10);
param11 = resultset.getString(11);
param12 = resultset.getString(12);
param13= resultset.getString(13);
param14 = resultset.getString(14);
break;
}
I'd replace that 100+ line humungous switch statement with
// note: array indexed from 0, resultset from 1
for (int i = 0; i<nparam; i++) {
param[i] = resultset.getString(i+1)
}
}
}
} catch (SQLException e) {
e.toString() ;
e.toString() does nothing useful! Try removing it.
System.out.println(e);
} finally {
try{
connection.close();
}catch (SQLException e) {e.toString();
System.out.println(e);}
}
}catch (ClassNotFoundException e) {
e.toString();
System.out.println(e);}
}
}
</CODE>
and i call this class with:
<CODE>
ExecuteQueryDB exqimp =new ExecuteQueryDB("SELECT column FROM table)",
1);
</CODE>
The problem can be here?
I'd guess elsewhere. If you can trim it down to an SSCCE* that shows the
problem, I'm sure more people will make the effort to help you.
*SSCCE defintions:
http://homepage1.nifty.com/algafield/sscce.html
http://mindprod.com/jgloss/sscce.html