Share on Google+Share on Google+

Understanding JDBC warning Methods

In this tutorial you will learn about JDBC getWarnings() and clearWarnings() methods

JDBC getWarnings() And clearWarnings()

In JDBC getWarnings() method is used get the warnings reported by the call of Connection, ResultSet, and Statement objects.

clearWarrning() method is used to clear all the warning reported by the above object.

These methods are called along with object.




An example of these method is given below.


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

public class JDBCWarningsExample {
	Connection connection = null;

	public JDBCWarningsExample() {
		try {
		} catch (ClassNotFoundException e) {

	public Connection createConnection() {
		Connection con = null;
		if (connection != null) {
			System.out.println("Cant create a connection");
		} else {
			try {
				con = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/student", "root",
				System.out.println("Connection created Successfully");
				DatabaseMetaData dbMetaData = con.getMetaData();
				ResultSet dbInfo = dbMetaData.getCatalogs();
				System.out.println("Getting Concurrency of MetaData");
			} catch (SQLException e) {
		return con;

	public static void main(String[] args) throws SQLException {
		JDBCWarningsExample jdbccOnnectionExample = new JDBCWarningsExample();
		Statement statement = null;
		ResultSet resultSet = null;
		Connection conn = jdbccOnnectionExample.createConnection();
		try {
			statement = conn.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM student");
		} catch (Exception e) {
		SQLWarning conWarning = conn.getWarnings();
		System.out.println("Connection warning " + conWarning);
		conn.clearWarnings();// clearing warning
		SQLWarning stmtWarning = statement.getWarnings();
		System.out.println("Statement warning " + stmtWarning);
		SQLWarning resWarning = resultSet.getWarnings();
		System.out.println("Result Set warningd " + resWarning);
		conn.close();// closing a connection
When you run this application it will display message as shown below:

Connection created Successfully
Getting Concurrency of MetaData
Connection warning null
Statement warning null
Result Set warningd null

Download this example code


Posted on: October 20, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.