Home Tutorial Java Jdbc Understanding JDBC warning Methods

 
 

Understanding JDBC warning Methods
Posted on: October 20, 2010 at 12:00 AM
Advertisement
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.

Example-

connection.getWarnings();

connection.clearWarnings();

An example of these method is given below.

 

JDBCWarningsExample.java

package roseindia.net;

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 {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
	}

	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",
						"root");
				System.out.println("Connection created Successfully");
				DatabaseMetaData dbMetaData = con.getMetaData();
				ResultSet dbInfo = dbMetaData.getCatalogs();
				System.out.println("Getting Concurrency of MetaData");
				System.out.println(dbInfo.getConcurrency());
			} catch (SQLException e) {
				System.out.println(e.toString());
			}
		}
		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) {
			System.out.println(e.toString());
		}
		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);
		statement.close();
		resultSet.close();
		conn.close();// closing a connection
	}
}
When you run this application it will display message as shown below:

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

Download this example code

Advertisement

Related Tags for Understanding JDBC warning Methods:


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.

Posted on: October 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com