sql

Handle SQL warning example

In this example we shall show you how to handle SQL Warnings in Java. To handle SQL Warnings one should perform the following steps:

  • Load the JDBC driver, using the forName(String className) API method of the Class. In this example we use the Oracle JDBC driver.
  • Create a Connection to the database. Invoke the getConnection(String url, String user, String password) API method of the DriverManager to create the connection.
  • Get the SQLWarning risen while connecting to the database, using the getWarnings() API method of the Connection.
  • Check the connectionWarning, with getMessage(), getSQLState(), getErrorCode() and then get the next warning with getNextWarning() API methods of the SQLWarning.
  • Execute an SQL Statement, which returns a ResultSet object. For each row of the ResultSet get the SQLWarnings, using the getWarnings() API method of the ResultSet.
  • Check on the resultsetWarning with the getMessage(), getSQLState(), getErrorCode() and then get the next warning with getNextWarning() API methods of the ResultSet,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

public class SQLWarning {
 
  public static void main(String[] args) {

    Connection connection = null;
    try {

  // Load the Oracle JDBC driver

  String driverName = "oracle.jdbc.driver.OracleDriver";

  Class.forName(driverName);


  // Create a connection to the database

  String serverName = "localhost";

  String serverPort = "1521";

  String sid = "mySchema";

  String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + ":" + sid;

  String username = "username";

  String password = "password";

  connection = DriverManager.getConnection(url, username, password);

  

  System.out.println("Successfully Connected to the database!");

  
    } catch (ClassNotFoundException e) {

  System.out.println("Could not find the database driver " + e.getMessage());
    } catch (SQLException e) {

  System.out.println("Could not connect to the database " + e.getMessage());
    }

    try {

  // Get warnings risen while connecting to the database

  SQLWarning connectionWarning = connection.getWarnings();

  

  while (connectionWarning != null) {


String warningMessage = connectionWarning.getMessage();


String warningSQLState = connectionWarning.getSQLState();


int warningErrorCode = connectionWarning.getErrorCode();





System.out.println("Connection warning : " + warningErrorCode +" Message : " + warningMessage + " SQL state " + warningSQLState);





connectionWarning = connectionWarning.getNextWarning();

  }


  // Create a statement

  Statement statement = connection.createStatement();


  // Use the statement...


  // Get warnings risen while using the statement

  SQLWarning statementWarning = statement.getWarnings();

  if (statementWarning != null) {


String warningMessage = statementWarning.getMessage();


String warningSQLState = statementWarning.getSQLState();


int warningErrorCode = statementWarning.getErrorCode();





System.out.println("Statement warning : " + warningErrorCode +" Message : " + warningMessage + " SQL state " + warningSQLState);





statementWarning = statementWarning.getNextWarning();

  }


  // Get the result set from the statement

  ResultSet resultSet = statement.executeQuery("SELECT * FROM test_table");

  while (resultSet.next()) {


    


// Use result set ...



// Get warnings on the current row of the result set


SQLWarning resultsetWarning = resultSet.getWarnings();


if (resultsetWarning != null) {



  String warningMessage = resultsetWarning.getMessage();



  String warningSQLState = resultsetWarning.getSQLState();



  int warningErrorCode = resultsetWarning.getErrorCode();



  



  System.out.println("Resultset warning : " + warningErrorCode +" Message : " + warningMessage + " SQL state " + warningSQLState);



  



  resultsetWarning = resultsetWarning.getNextWarning();


}

  }

  
    } catch (SQLException e) {
    }

 }

}

Output:

Successfully Connected to the database!

 
This was an example of how to handle SQL Warnings in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button