JDBC SQLException Handling with Programming Example

In this chapter you will learn

1. What is Exception Handling in JDBC?
2. How to fix SQLException in JDBC?
3. SQLException Programming Example

What is Exception Handling in JDBC?

Exception is such type of condition when program encounters problem in execution and quit with a problematic error message. In JDBC, when program gets trouble in data source, it throws SQLException. However, there are several many exceptions in Java but while we are studying JDBC, SQLException is most common and we will cover it through this chapter.

A SQLException can occur in JDBC Driver or inside database. In this chapter you will learn how to handle this exception.

SQLException Methods

Method Description
getErrorCode() Gets the error number associated with the exception.
getMessage() Gets the JDBC driver’s error message for an error, handled by the driver or gets the Oracle error number and message for a database error.
getSQLState() Gets the XOPEN SQLstate string. For a JDBC driver error, no useful information is returned from this method. For a database error, the five-digit XOPEN SQLstate code is returned. This method can return null.
getNextException() Gets the next Exception object in the exception chain.
printStackTrace() Prints the current exception, or throwable, and it’s backtrace to a standard error stream.
printStackTrace(PrintStream s) Prints this throwable and its backtrace to the print stream you specify.
printStackTrace(PrintWriter w) Prints this throwable and it’s backtrace to the print writer you specify.

Programming Example

package AdvanceJDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLException_Example
{
	   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	   static final String dburl = "jdbc:mysql://localhost/STOREDB";	   
	   static final String dbuser = "root";
	   static final String dbpass = "root";
	   
public static void main(String[] args) throws SQLException
{
	Connection con = null;
	Statement stmt = null;
	
	try 
	{
	    //Step 1 : Connecting to server and database
	    con = DriverManager.getConnection(dburl, dbuser, dbpass);	    
	    //Step 2 : Initialize Statement
	    stmt=con.createStatement();
	    //Step 3 : SQL Query
	    String query="SELECT * FROM ITEM";
	    
	    //Step 4 : Run Query In ResultSet
	    ResultSet rset = stmt.executeQuery(query);
	    while(rset.next())
	    {
	    	System.out.print("ID: " + rset.getInt(1));
	    	System.out.print(" Product : "+rset.getString(2));
	    	System.out.println(" Price : "+rset.getString(3));
	    }
	} 
	
	catch (SQLException e) 
	{
	    System.err.println("Cannot connect ! ");
	    e.printStackTrace();
	}
	
	finally {
	    System.out.println("Closing the connection.");
	    if (con != null) try { con.close(); } catch (SQLException ignore) {}
	}
	
}
}

Summary

In this chapter you learned what is SQLException in JDBC and how to handle it using exception handling.


Leave a Reply

Your email address will not be published. Required fields are marked *