简体   繁体   中英

SQLException while running a query with JDBC

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:

  1. Resources aren't closed properly. Those should be done in individual try/catch blocks in a finally block.
  2. Badly written SQL. The DELETE could be done far more efficiently with a JOIN.
  3. Poor decomposition. There are 2 or 3 methods hiding in here.
  4. No transaction manager; no ACID.
  5. No connection pooling; should be passed into this method rather than instantiated in scope.
  6. Should use PreparedStatement and binding rather than concatenating String .

I'm not certain, but it looks like you have three SQL queries:

  1. SELECT all the payer IDs
  2. Get a COUNT of payer IDs
  3. DELETE all the payer IDs in the list you got from SELECT.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM