In my program I execute some SQL queries with JDBC. When I run the program for this certain query I get the following error:
SQLException: Exception in thread "main" java.sql.SQLException:at TransformData.main(TransformData.java:213)
Here is this part of code:
try
{
dbcon = DriverManager.getConnection(url,"username","password");
stmt = dbcon.createStatement();
stmt1 = dbcon.createStatement();
stmt13 = dbcon.createStatement();
stmt14 = dbcon.createStatement();
String sql1 = "SELECT DISTINCT payer_id FROM transactions ORDER BY payer_id";
rs1 = stmt1.executeQuery(sql1);
while (rs1.next())
{
Integer payer_id = rs1.getInt("payer_id");
payer_ids.add(payer_id);
}
rs1.close();
stmt1.close();
for(int i = 0; i < payer_ids.size(); i++)
{
String sql13 = "SELECT COUNT(*) AS counter, isCOrporate FROM transformed_table WHERE payer_id = "+payer_ids.get(i)+" ";
rs5 = stmt13.executeQuery(sql13);
while(rs5.next())
{
int counter = rs5.getInt("counter");
int isCorporate = rs5.getInt("isCorporate");
if ((counter - payer_ids.get(i).intValue() - isCorporate) < 1)
{
String sql14 = "DELETE FROM transformed_table WHERE payer_id = "+payer_ids.get(i)+" ";
stmt14.executeUpdate(sql14);
}
}
}
rs5.close();
stmt13.close();
stmt14.close();
dbcon.close();
}
catch(SQLException e)
{
System.out.print("SQLException: ");
throw new SQLException(errorMessages);
}
Line 213 is this line: throw new SQLException(errorMessages);
in catch.
I am trying to find what might throw this exception. Can someone help?
There are lots of things wrong with this code, but here's the pertinent hint: Your catch block is wrong. Write it this way:
catch(SQLException e) {
e.printStackTrace();
}
Your way drains all the useful information out of the stack trace. You can't debug what else you've done wrong.
Make the change, rerun the code, and read the stack trace. It'll tell you what your real problem is.
So many things to correct:
PreparedStatement
and binding rather than concatenating String
. I'm not certain, but it looks like you have three SQL queries:
Do I read that correctly? If yes, why don't you do it in one query, like this?
DELETE
FROM transformed_table
WHERE payer_id IN (SELECT DISTINCT payer_id FROM transactions)