Search code examples
javasqljdbcprepared-statementsqlexception

java SQLException Analysis


       Connection con;
       PreparedStatement ps,p2;


       try
       {
           sqlquery = "...";
           ps=con.prepareStatement(sqlquery);
           ps.executeUpdate();

          // using ps2...some similar process.......
       }
       catch(SQLException sqlex)
       {
              System.out.print(sqlex.getMessage());
       }

I've got a doubt that whether is

Is it possible to find out the cause of the specific error made out by a query or update such that i can handle the situation other than just displaying the error message at output console (as here) displaying the cause ?

for eg: if if ps preparedStatement makes a mistake because of duplicate entry or ps because of some foreign key issues etc..then i should be able to analyze the sql error specifically using SQLException object so that i can handle accordingly rather than just displaying error message in the output console

The reason for which i need this is because i want to display the error made by a user working through GUI scenario (in case : here java swing) any error entries he made through an interactive manner by analyzing the SQLException instance....

In short , how to classify errors using a single SQLException instance

Sorry if this question finds ambigous or stupid..!!!


Solution

  • If I understand correctly, you need the methods getSQLState and getErrorCode of class SQLException.

    Look here for further information.

    I hope this helps!